Problem 29:divide two integers
does not use division, multiplication, and remainder operations to complete two-digit division, and returns Max_int when the value overflows.
Solution: The first idea, directly with dividend cycle minus divisor, each reduction of the result is greater than 0, the result plus 1, less than 0 o'clock cycle end. The solution is fine, but the complexity is too high, and when you remove a small number with a large number, it times out.
So we thought, instead of subtracting the divisor, I would subtract the divisor by 2 of the exponent, and see how much of the result would be less than 0, and then we would record how many times this was 2 and repeat the operation for the remaining values:
For example, 13 divided by 4,13>4,4<<1 for 8;13>8,8<<1 as 16;13<16, recording res=2=4, remaining 13-8=7, and then 7 as 13, repeat operation.
Note The handling of cross-border operations.
Def divide (self, dividend, divisor): ""
: Type dividend:int
: Type divisor:int
: Rtype:int
""
sign = (Dividend > 0) = = (Divisor > 0)
dividend = ABS (dividend)
divisor = ABS (divisor)
res = 0
while Dividend >= Divisor:
T, C = divisor, 1 while
dividend >= t:
t <<= 1
C <<= 1
t >>= 1
C >>= 1
dividend-t
res + = C
If not sign:
res =-res return
min (m Ax (res,-2147483648), 2147483647)
Problem 33:search in rotated Sorted Array
in a rotated sorted array, locate the subscript for the target value if it does not exist. Returns-1, such as a bleed array [1,2,3,4,5,6,7],rotated may later become [5,6,7,1,2,3,4], and there are no duplicate elements in the array .
Solution : Obviously, the problem is to use a similar binary lookup method to get an O (LOGN) solution, unlike in a sorted array, our judgment conditions are slightly more complex, because they may not be in a monotone interval.
Consider that the array [4,6,7,0,1,2,3],l=0,r=6,target is 6, at which point Nums[mid]=0<6 should be updated to 3 before rotating, but this is obviously not true. We divided the situation: when Num[0]>target, at this point, target may only appear in the rotated part, which is the example above ... The 0,1,2,3 section, if at this time Num[0]<num[mid], represents mid before it is rotated (at which point the target must be less than num[mid), so L want to update to mid+1; if num[0]> num[mid at this time], Represents mid has been rotated before, if target>num[mid],l to be updated to mid+1, otherwise, r=mid
When Num[0]<target, the same
In summary, we found that L need to be updated to mid+1, there are the following:
Num[0]>target and Num[0]<num[mid] and Target<num[mid] Num[0]>target and Num[0]>num[mid] and target> Num[mid] Num[0]<target and Num[0]<num[mid] and Target>num[mid]
That is, when the above three conditions are different or true, update the l=mid+1, otherwise update the R=mid
In fact, there are 8 kinds of permutations and combinations of three conditions, of which three are updated l=mid+1, three are updated r=mid, and two are non-existent.
def search (self, nums, target): ""
: Type Nums:list[int]
: type target:int: Rtype:int "" "
L, R = 0, Len (nums)-1 while
L < r:
mid = (L + r)/2
if (Nums[0] > target) ^ (Nums[0] > Nums[mid]) ^ ( Target > Nums[mid]):
L = mid + 1
else:
r = Mid return
L-if Target in nums[l:l+1] else-1
Problem 34:search for a Range
given an ascending number, find the range where target appears, and if not, return [ -1,-1], such as [1,2,3,4,6,6,8],target=6, return [4,5], requiring a time complexity of O (Logn).
problem-Solving ideas:(heart tired, before writing did not save, here is about to say it) time complexity requirements for two points to find, in turn to find the left and right boundaries, according to the following two-point search strategy, the final result must be the left edge (if target exists), Find the right boundary in the same way, simply swap all L and R for the following strategies to see: l=mid+1, replacing R=mid-1;r=mid with L=mid
find left Boundary :
L,r = 0, Len (nums)-1 while
L < r:
mid = (L + r)//2
if Target > Nums[mid]:
L = mid + 1
else:
r = mid
L and R are the left edges.
Find right Border :
L,r = L, Len (nums)-1 while
L < r:
mid = (L + R + 1)//2
if Target < Nums[mid]:
r = Mid-1
El SE:
L = mid
The L and R here are the left edges, and you need to be aware that the plus 1 operation of mid because the divide is rounded down, so look for the last loop on the left, L is fixed, I'm moving from R to L, it's the opposite of the right boundary, we need r not to move, l'm going to r, so we need to add 1. Problem 36:valid Sudoku
given an initialized Sudoku board, the judge is valid, that is, whether each number appears only once in each row, in each column, and in each 3*3 cell, and the cell value not populated is '. '.
problem-Solving ideas:(heart tired, before writing did not save, here is about to say it) began to understand the wrong, in fact, only to traverse the chessboard, record the location of each number ('. ') Without the tube), after the end of the traversal to determine whether to meet the requirements, the Python line to fix
def isvalidsudoku (self, Board): ""
: Type BOARD:LIST[LIST[STR]]
: Rtype:bool ""
"
pos = SUM ([(D, R), (c, D), (d, R/3, C/3)] for
R, row in enumerate (board) for
C, D in enumerate (row) if D!= '. '), [])
return Len (pos) = = Len (set (POS))
Note that the first two two tuples cannot be written (D,r), (d,c) or the row or column cannot be identified