Movatterモバイル変換


[0]ホーム

URL:


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

Commitf3a3912

Browse files
committed
Update
Update
1 parent0cd3c5f commitf3a3912

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

‎Python3/035_Search_Insert_Position.py

Lines 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+
classSolution(object):
17+
defsearchInsert(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+
whilestart<end:
27+
mid= (start+end)//2
28+
ifnums[mid]==targetor (nums[mid]>targetand (mid==0ornums[mid-1]<target)):
29+
returnmid
30+
ifmid==length-1andnums[mid]<target:
31+
returnmid+1
32+
ifnums[mid]<target:
33+
start=mid+1
34+
else:
35+
end=mid
36+
37+
38+
if__name__=="__main__":
39+
assertSolution().searchInsert([1,3,5,6],5)==2
40+
assertSolution().searchInsert([1,3,5,6],2)==1
41+
assertSolution().searchInsert([1,3,5,6],7)==4
42+
assertSolution().searchInsert([1,3,5,6],0)==0

‎Python3/036_Valid_Sudoku.py

Whitespace-only changes.

‎Python3/039_Combination_Sum.py

Lines 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+
classSolution(object):
20+
defcombinationSum(self,candidates,target):
21+
"""
22+
:type candidates: List[int]
23+
:type target: int
24+
:rtype: List[List[int]]
25+
"""
26+
ifnotcandidates:
27+
return []
28+
candidates.sort()
29+
result= []
30+
self.combination(candidates,target, [],result)
31+
returnresult
32+
33+
defcombination(self,candidates,target,current,result):
34+
s=sum(current)ifcurrentelse0
35+
ifs>target:
36+
return
37+
elifs==target:
38+
result.append(current)
39+
return
40+
else:
41+
fori,vinenumerate(candidates):
42+
self.combination(candidates[i:],target,current+ [v],result)
43+
44+
45+
if__name__=="__main__":
46+
assertSolution().combinationSum([2,3,6,7],7)== [[2,2,3], [7]]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp