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

Commit90ad977

Browse files
committed
Update to 016
Update to 016
1 parent58e0fb0 commit90ad977

File tree

8 files changed

+98
-51
lines changed

8 files changed

+98
-51
lines changed

‎.idea/Leetcode-Python3.iml

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎.idea/misc.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎.idea/vcs.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎.idea/workspace.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎Python3/014_Longest_Common_Prefix.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
#!usr/bin/env python3
22
# -*- coding:utf-8 -*-
3+
'''
4+
Write a function to find the longest common prefix string amongst an array of strings.
5+
'''
36

47

8+
classSolution(object):
9+
deflongestCommonPrefix(self,strs):
10+
"""
11+
:type strs: List[str]
12+
:rtype: str
13+
"""
14+
ifnotstrs:
15+
return""
16+
longest=strs[0]
17+
foriinrange(len(strs[0])):
18+
forstrinstrs:
19+
iflen(str)<=iorstrs[0][i]!=str[i]:
20+
returnstrs[0][:i]
21+
returnstrs[0]
522

623

24+
if__name__=="__main__":
25+
assertSolution().longestCommonPrefix(["","heabc","hell"])==""
26+
727

‎Python3/015_3Sum.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
11
#!usr/bin/env python3
22
# -*- coding:utf-8 -*-
3+
'''
4+
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
35
6+
Note:
7+
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
8+
The solution set must not contain duplicate triplets.
9+
For example, given array S = {-1 0 1 2 -1 -4},
410
11+
A solution set is:
12+
(-1, 0, 1)
13+
(-1, -1, 2)
14+
'''
515

616

17+
classSolution:
18+
defthreeSum(self,nums):
19+
"""
20+
:type nums: List[int]
21+
:rtype: List[List[int]]
22+
"""
23+
result= []
24+
nums.sort()
25+
26+
foriinrange(len(nums)-2):
27+
ifi==0ornums[i]>nums[i-1]:
28+
left=i+1
29+
right=len(nums)-1
30+
whileleft<right:
31+
# 注意这里不要用sum()
32+
li=nums[i]+nums[left]+nums[right]
33+
ifli==0:
34+
result.append([nums[i],nums[left],nums[right]])
35+
left+=1
36+
right-=1
37+
whileleft<rightandnums[left]==nums[left-1]:
38+
left+=1
39+
whileleft<rightandnums[right]==nums[right+1]:
40+
right-=1
41+
elifli>0:
42+
right-=1
43+
else:
44+
left+=1
45+
returnresult
46+
47+
48+
if__name__=="__main__":
49+
assertSolution().threeSum([-1,0,1,0])== [[-1,0,1]]
50+
751

‎Python3/016_3Sum_Closest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
11
#!usr/bin/env python3
22
# -*- coding:utf-8 -*-
3+
'''
4+
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
35
6+
For example, given array S = {-1 2 1 -4}, and target = 1.
47
8+
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
9+
'''
510

611

12+
classSolution(object):
13+
defthreeSumClosest(self,nums,target):
14+
"""
15+
:type nums: List[int]
16+
:type target: int
17+
:rtype: int
18+
"""
19+
nums.sort()
20+
result=0
21+
# Init the distance between result and target with a very large number
22+
distance=pow(2,32)-1
23+
foriinrange(len(nums)-2):
24+
left=i+1
25+
right=len(nums)-1
26+
whileleft<right:
27+
li=nums[i]+nums[left]+nums[right]
28+
ifli==target:
29+
returntarget
30+
ifabs(li-target)<distance:
31+
result=li
32+
distance=abs(li-target)
33+
elifli>target:
34+
right-=1
35+
else:
36+
left+=1
37+
returnresult
738

39+
40+
if__name__=="__main__":
41+
assertSolution().threeSumClosest([1,1,1,1],-100)==3

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp