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

Appearance settings

Commit f3a3912

Browse filesBrowse files
committed
Update
Update
1 parent 0cd3c5f commit f3a3912
Copy full SHA for f3a3912

File tree

Expand file treeCollapse file tree

3 files changed

+88
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+88
-0
lines changed

‎Python3/035_Search_Insert_Position.py

Copy file name to clipboard
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
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.
5+
6+
You may assume no duplicates in the array.
7+
8+
Here are few examples.
9+
[1,3,5,6], 5 → 2
10+
[1,3,5,6], 2 → 1
11+
[1,3,5,6], 7 → 4
12+
[1,3,5,6], 0 → 1
13+
'''
14+
15+
16+
class Solution(object):
17+
def searchInsert(self, nums, target):
18+
"""
19+
:type nums: List[int]
20+
:type target: int
21+
:rtype: int
22+
"""
23+
length = len(nums)
24+
start = 0
25+
end = length
26+
while start < end:
27+
mid = (start + end) // 2
28+
if nums[mid] == target or (nums[mid] > target and (mid == 0 or nums[mid - 1] < target)):
29+
return mid
30+
if mid == length - 1 and nums[mid] < target:
31+
return mid + 1
32+
if nums[mid] < target:
33+
start = mid + 1
34+
else:
35+
end = mid
36+
37+
38+
if __name__ == "__main__":
39+
assert Solution().searchInsert([1, 3, 5, 6], 5) == 2
40+
assert Solution().searchInsert([1, 3, 5, 6], 2) == 1
41+
assert Solution().searchInsert([1, 3, 5, 6], 7) == 4
42+
assert Solution().searchInsert([1, 3, 5, 6], 0) == 0

‎Python3/036_Valid_Sudoku.py

Copy file name to clipboardExpand all lines: Python3/036_Valid_Sudoku.py
Whitespace-only changes.

‎Python3/039_Combination_Sum.py

Copy file name to clipboard
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
5+
6+
The same repeated number may be chosen from C unlimited number of times.
7+
8+
Note:
9+
All numbers (including target) will be positive integers.
10+
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
11+
The solution set must not contain duplicate combinations.
12+
For example, given candidate set 2,3,6,7 and target 7,
13+
A solution set is:
14+
[7]
15+
[2, 2, 3]
16+
'''
17+
18+
19+
class Solution(object):
20+
def combinationSum(self, candidates, target):
21+
"""
22+
:type candidates: List[int]
23+
:type target: int
24+
:rtype: List[List[int]]
25+
"""
26+
if not candidates:
27+
return []
28+
candidates.sort()
29+
result = []
30+
self.combination(candidates, target, [], result)
31+
return result
32+
33+
def combination(self, candidates, target, current, result):
34+
s = sum(current) if current else 0
35+
if s > target:
36+
return
37+
elif s == target:
38+
result.append(current)
39+
return
40+
else:
41+
for i, v in enumerate(candidates):
42+
self.combination(candidates[i:], target, current + [v], result)
43+
44+
45+
if __name__ == "__main__":
46+
assert Solution().combinationSum([2, 3, 6, 7], 7) == [[2, 2, 3], [7]]

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.