Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 56a2576

Browse files
skip
2 parents e62c1af + ec997f3 commit 56a2576

29 files changed

+480
-2
lines changed

‎Kangli/Arrays/3SumClosest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def threeSumClosest(self, nums, target):
3+
nums.sort()
4+
res, ans = [], nums[0]+nums[1]+nums[2]
5+
for i in range(len(nums)):
6+
l, r = i+1, len(nums)-1
7+
while l < r:
8+
s = nums[i] + nums[l] + nums[r]
9+
if abs(target-s) < abs(target-ans):
10+
while abs(target-s) < abs(target-ans):
11+
ans = s
12+
if ans > target:
13+
r -= 1
14+
else:
15+
l += 1
16+
else:
17+
if s > target:
18+
r -= 1
19+
else:
20+
l+=1
21+
return ans

‎Kangli/Arrays/3sum.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def threeSum(self, nums):
3+
res = []
4+
nums.sort()
5+
for i in xrange(len(nums)-2):
6+
if i > 0 and nums[i] == nums[i-1]:
7+
continue
8+
l, r = i+1, len(nums)-1
9+
while l < r:
10+
s = nums[i] + nums[l] + nums[r]
11+
if s < 0:
12+
l +=1
13+
elif s > 0:
14+
r -= 1
15+
else:
16+
res.append((nums[i], nums[l], nums[r]))
17+
while l < r and nums[l] == nums[l+1]:
18+
l += 1
19+
while l < r and nums[r] == nums[r-1]:
20+
r -= 1
21+
l += 1; r -= 1
22+
return res
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def findKthLargest(self, nums, k):
3+
j = len(nums)-1
4+
t = 1
5+
nums.sort()
6+
while j >= 0:
7+
if t ==k:
8+
return nums[j]
9+
else:
10+
j -= 1
11+
t +=1

‎Kangli/Arrays/findAllDuplicates

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ Could you do it without extra space and in O(n) runtime?
55
"""
66
#O(1) extra space and O(n) time. Exploit the fact that the elements of nums all correspond to positions within nums and
77
# find duplicates by marking visited indices.
8+
#1/8/18, reviewed this problem, same solution using same "trick"
9+
class Solution(object):
10+
def findDuplicates(self, nums):
11+
res = []
12+
for n in nums:
13+
if nums[abs(n)-1] < 0:
14+
res.append(abs(n))
15+
else:
16+
nums[abs(n)-1 ] = -nums[abs(n)-1]
17+
return res
18+
819
class Solution(object):
920
def findDuplicates(self, nums):
1021
"""
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def findLengthOfLCIS(self, nums):
3+
if not nums:
4+
return 0
5+
count, ans = 1, 1
6+
for i in range(1, len(nums)):
7+
if nums[i] > nums[i-1]:
8+
count += 1
9+
else:
10+
if ans < count:
11+
ans = count
12+
count = 1
13+
return max(ans, count)

‎Kangli/Arrays/pascalTriangle.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# not very elegant, but accepted. Created Pascal's directly from definition
2+
class Solution(object):
3+
def generate(self, numRows):
4+
ans = [[1], [1, 1]]
5+
if numRows == 0:
6+
return []
7+
elif numRows == 1:
8+
return [[1]]
9+
elif numRows == 2:
10+
return [[1], [1, 1]]
11+
else:
12+
for i in range(3, numRows +1):
13+
temp = [1]*i
14+
for j in range(i):
15+
if j ==0 or j == i-1:
16+
continue
17+
else:
18+
temp[j] = ans[i-2][j-1] + ans[i-2][j]
19+
ans.append(temp)
20+
return ans
21+
122
def generate(self, numRows):
223
res = [[1]]
324
for i in range(1, numRows):

‎Kangli/Arrays/spiralMatrix.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def spiralOrder(self, matrix):
3+
if not matrix or not matrix[0]:
4+
return []
5+
res = []
6+
colBegin, colEnd, rowBegin, rowEnd = 0, len(matrix[0])-1, 0, len(matrix)-1
7+
8+
while colBegin <= colEnd and rowBegin <= rowEnd:
9+
for i in range(colBegin, colEnd+1):
10+
res.append(matrix[rowBegin][i])
11+
rowBegin += 1
12+
13+
for i in range(rowBegin, rowEnd+1):
14+
res.append(matrix[i][colEnd])
15+
colEnd -= 1
16+
if rowBegin <= rowEnd:
17+
for i in range(colEnd, colBegin-1, -1):
18+
res.append(matrix[rowEnd][i])
19+
rowEnd -= 1
20+
21+
if colBegin <= colEnd:
22+
for i in range(rowEnd, rowBegin-1, -1):
23+
res.append(matrix[i][colBegin])
24+
colBegin += 1
25+
26+
return res
27+
28+
29+
30+
File renamed without changes.
File renamed without changes.

‎Kangli/DP/climbStairs.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ def _climbStairs(self, n):
3131
return nw[n]
3232
climbStairs(5)
3333
"""
34+
35+
# 1/14/18 Pure recursion solution, TLE for large n.
36+
class Solution(object):
37+
def climbStairs(self, n):
38+
if n == 0:
39+
return 0
40+
elif n == 1:
41+
return 1
42+
elif n ==2:
43+
return 2
44+
else:
45+
return self.climbStairs(n-2) + self.climbStairs(n-1)
46+
# memoized recursion
47+
class Solution(object):
48+
def climbStairs(self, n):
49+
if n == 1:
50+
return 1
51+
elif n == 2:
52+
return 2
53+
dp = [0]*(n+1)
54+
dp[1], dp[2] = 1, 2
55+
for i in range(3, n+1):
56+
dp[i] = dp[i-1] + dp[i-2]
57+
return dp[n]
3458

3559

36-

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /