This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Reverse digits of an integer. | |
#Return 0 if the result overflows and does not fit in a 32 bit signed integer | |
def reverse(self, A): | |
rev = 0 | |
flag = False | |
if A < 0 : | |
flag = True | |
A = abs(A) | |
while A != 0 : | |
rev = rev * 10 + (A%10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Given 2 non negative integers m and n, find gcd(m, n) | |
def gcd(A, B): | |
while B != 0 : | |
A , B = B , A%B | |
return A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Given an integer n, return the number of trailing zeroes in n!. | |
def trailingZeroes(self, A): | |
numOfZero = 0 | |
i = 1 | |
while 5**i <= A : | |
numOfZero += A//(5 ** i) | |
i = i + 1 | |
return numOfZero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
def binarySearch(A, B, flagSearchFirst) : | |
""" | |
Modified version of Binary Search to find first or last index of an element when it is repeated | |
:param A: List of sorted integers | |
:param B: integer to be searched | |
:param flagSearchFirst: Whether to search for first or last index of integer B in array | |
:return: index (if nott found then -1) | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
#Given a sorted array and a target value, return the index if the target is found. | |
# If not, return the index where it would be if it were inserted in order. | |
#You may assume no duplicates in the array. | |
def searchInsert(A, B): | |
""" | |
:param: A List of integers , B integer to be inserted | |
:return: return index if B is already present in A , otherwise index at which B will be inserted | |
""" | |
low = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
#Find Square root of an inetger , if Square root is not integer returns floor of sqrt | |
def sqrt(A) : | |
if 2 > A : | |
return A | |
low = 0 | |
high = A | |
while high > low + 1 : | |
num = (low + high)/2 | |
if num**2 < A : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
def strStr(self, haystack, needle): | |
""" | |
:param: | |
:haystack: A string | |
:needle: A string | |
:return: index of the first ocurrence of needle in haystack | |
""" | |
if len(needle) == 0 or len(haystack)==0 : | |
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
def lengthOfLastWord(A): | |
start =0 | |
end = 0 | |
if len(A) == 0 : | |
return 0 | |
lastLength = 0 | |
curLength = 0 | |
for i in range(len(A)) : | |
if A[i] != ' ' : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
#reverse the string word by word. | |
def reverseWords(A): | |
""" | |
Reverse the string word by word, removes leading and trailing spaces, | |
:param: string to be reversed | |
:return: reversed string word by word | |
""" | |
curWord = '' | |
listOfWords = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
#Find starting and ending index of an element in sorted list of elements | |
def binarySearch(A, B, searchFirst) : | |
""" | |
Modified version of binary search | |
:param: Sorted List of integers, target element, boolean value (True for first index, False for last index) | |
:return: List of first index and last index, if target element not found returns [-1, -1] | |
""" | |
low = 0 | |
high = len(A) - 1 |