Skip to content

Commit 892f849

Browse files
author
yangdi
committed
2 parents 71d9ea1 + 345cd62 commit 892f849

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

src2/2Sum/2sum.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,18 @@
1515
class Solution:
1616
# @return a tuple, (index1, index2)
1717
def twoSum(self, num, target):
18+
match = dict()
19+
20+
for i in range(len(num)):
21+
v = match.get(num[i], None)
22+
if v != None:
23+
return v + 1, i + 1
24+
25+
match[target - num[i]] = i
26+
27+
return None
28+
29+
if __name__ == '__main__':
30+
s = Solution();
31+
print s.twoSum([3,2,4], 6)
1832

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'''
2+
There are two sorted arrays A and B of size m and n respectively.
3+
4+
Find the median of the two sorted arrays.
5+
6+
The overall run time complexity should be O(log (m+n)).
7+
'''
8+
9+
class Solution:
10+
# get media of A --> ma
11+
# get count of number in B that smaller than ma
12+
def getNumBiggerThanValue(self, Array, Value):
13+
# lower bounder is better
14+
for i in range(len(Array)):
15+
if Array[i] > Value:
16+
return i + 1
17+
return len(Array)
18+
19+
# return iA, iB
20+
def moveFoward(self, A, B, iA, iB):
21+
# min of A[iA + 1], B[iB]
22+
if iB == len(B):
23+
return iA + 1, iB
24+
elif iA == len(A):
25+
return iB + 1, iA
26+
else
27+
28+
# @return a float
29+
def findMedianSortedArrays(self, A, B):
30+
lenA = len(A)
31+
lenB = len(B)
32+
mid = (lenA + lenB) / 2
33+
if lenA >= lenB:
34+
iA = len(A) / 2
35+
iB = self.getNumBiggerThanValue(B, A[iA])
36+
else:
37+
iB = len(B) / 2
38+
iA = self.getNumBiggerThanValue(A, B[iB])
39+
A.append(0x8ffffffff)
40+
B.append(0x8ffffffff)
41+
42+
while True:
43+
## step
44+
left = iA + iB
45+
#print mid, left, iA, iB, ".."
46+
if left == mid:
47+
return min(A[iA], B[iB])
48+
elif left < mid:
49+
# step forward
50+
if A[iA - 1] > B[iB - 1]:
51+
iB+=1
52+
else:
53+
iA+=1
54+
elif left > mid:
55+
# step backward
56+
if A[iA] > B[iB]:
57+
iA-=1
58+
else:
59+
iB-=1
60+
#print "A:%d, B:%d" %(A[iA], B[iB])
61+
62+
63+
if __name__ == '__main__':
64+
slu = Solution()
65+
print slu.findMedianSortedArrays([1], [2])
66+
print slu.findMedianSortedArrays([1, 12, 15, 26, 38], [2, 13, 17, 30, 45, 50])
67+
print slu.findMedianSortedArrays([1, 12, 15, 26, 38], [2, 13, 17, 30, 45])
68+
print slu.findMedianSortedArrays([1, 2, 5, 6, 8], [13, 17, 30, 45, 50])
69+
print slu.findMedianSortedArrays([1, 2, 5, 6, 8, 9, 10], [13, 17, 30, 45, 50])
70+
print slu.findMedianSortedArrays([1, 2, 5, 6, 8, 9], [13, 17, 30, 45, 50])
71+
print slu.findMedianSortedArrays([1, 2, 5, 6, 8], [11, 13, 17, 30, 45, 50])
72+
print slu.findMedianSortedArrays([1], [2,3,4])
73+
## [1,2,3] --> 2
74+
## [1,2,3,4] --> 3
75+
## [1,2,4,5,5,8] ==> 5
76+
## [1,2,4,5,5,6,8] ==> 5
77+
78+
79+
80+

src2/MinStack/MinStack.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
3+
4+
push(x) -- Push element x onto stack.
5+
pop() -- Removes the element on top of the stack.
6+
top() -- Get the top element.
7+
getMin() -- Retrieve the minimum element in the stack.
8+
'''
9+
class MinStack:
10+
# @param x, an integer
11+
# @return an integer
12+
def __init__(self):
13+
self.m_nums = list()
14+
self.m_minValue = 0x8fffffff
15+
self.m_minIndex = 0
16+
17+
def push(self, x):
18+
self.m_nums.append(x)
19+
if self.m_minValue > x:
20+
self.m_minValue, self.m_minIndex = x, len(self.m_nums)
21+
22+
# @return nothing
23+
def pop(self):
24+
if self.m_minIndex == len(self.m_nums):
25+
self.m_minValue = min(self.m_nums)
26+
self.m_minIndex = self.m_nums.index(self.m_minValue)
27+
28+
self.m_nums.pop()
29+
30+
# @return an integer
31+
def top(self):
32+
if len(self.m_nums) == 0:
33+
return None
34+
return self.m_nums[0]
35+
36+
# @return an integer
37+
def getMin(self):
38+
return self.m_minValue
39+
#return min(self.m_nums)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
# @return an integer
3+
def reverse(self, x):
4+
strX = str(x)
5+
#strX = list(strX)
6+
prefix = ''
7+
if strX[0] == '-':
8+
strX = strX[1:]
9+
prefix = '-'
10+
11+
strX = list(strX)
12+
strX.reverse()
13+
strX = prefix + ''.join(strX)
14+
x = int(strX)
15+
if x > 2147483647:
16+
return 0
17+
if x < -2147483648:
18+
return 0
19+
return x
20+
21+
22+
if __name__ == '__main__':
23+
print Solution().reverse(2147483647)
24+
print Solution().reverse(0)

0 commit comments

Comments
 (0)