Skip to content

Commit 90ad977

Browse files
committed
Update to 016
Update to 016
1 parent 58e0fb0 commit 90ad977

8 files changed

+98
-51
lines changed

.idea/Leetcode-Python3.iml

-11
This file was deleted.

.idea/misc.xml

-7
This file was deleted.

.idea/modules.xml

-8
This file was deleted.

.idea/vcs.xml

-6
This file was deleted.

.idea/workspace.xml

-19
This file was deleted.

Python3/014_Longest_Common_Prefix.py

+20
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+
class Solution(object):
9+
def longestCommonPrefix(self, strs):
10+
"""
11+
:type strs: List[str]
12+
:rtype: str
13+
"""
14+
if not strs:
15+
return ""
16+
longest = strs[0]
17+
for i in range(len(strs[0])):
18+
for str in strs:
19+
if len(str) <= i or strs[0][i] != str[i]:
20+
return strs[0][:i]
21+
return strs[0]
522

623

24+
if __name__ == "__main__":
25+
assert Solution().longestCommonPrefix(["", "heabc", "hell"]) == ""
26+
727

Python3/015_3Sum.py

+44
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+
class Solution:
18+
def threeSum(self, nums):
19+
"""
20+
:type nums: List[int]
21+
:rtype: List[List[int]]
22+
"""
23+
result = []
24+
nums.sort()
25+
26+
for i in range(len(nums) - 2):
27+
if i == 0 or nums[i] > nums[i - 1]:
28+
left = i + 1
29+
right = len(nums) - 1
30+
while left < right:
31+
# 注意这里不要用sum()
32+
li = nums[i] + nums[left] + nums[right]
33+
if li == 0:
34+
result.append([nums[i], nums[left], nums[right]])
35+
left += 1
36+
right -= 1
37+
while left < right and nums[left] == nums[left - 1]:
38+
left += 1
39+
while left < right and nums[right] == nums[right + 1]:
40+
right -= 1
41+
elif li > 0:
42+
right -= 1
43+
else:
44+
left += 1
45+
return result
46+
47+
48+
if __name__ == "__main__":
49+
assert Solution().threeSum([-1,0,1,0]) == [[-1,0,1]]
50+
751

Python3/016_3Sum_Closest.py

+34
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+
class Solution(object):
13+
def threeSumClosest(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+
for i in range(len(nums) - 2):
24+
left = i + 1
25+
right = len(nums) - 1
26+
while left < right:
27+
li = nums[i] + nums[left] + nums[right]
28+
if li == target:
29+
return target
30+
if abs(li - target) < distance:
31+
result = li
32+
distance = abs(li - target)
33+
elif li > target:
34+
right -= 1
35+
else:
36+
left += 1
37+
return result
738

39+
40+
if __name__ == "__main__":
41+
assert Solution().threeSumClosest([1, 1, 1, 1], -100) == 3

0 commit comments

Comments
 (0)