Skip to content

Commit 53670e9

Browse files
committed
update solutions
1 parent 5b7a584 commit 53670e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4492
-0
lines changed

11.container-with-most-water.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# @lc app=leetcode id=11 lang=python
3+
#
4+
# [11] Container With Most Water
5+
#
6+
# https://leetcode.com/problems/container-with-most-water/description/
7+
#
8+
# algorithms
9+
# Medium (42.47%)
10+
# Total Accepted: 320.1K
11+
# Total Submissions: 751.3K
12+
# Testcase Example: '[1,8,6,2,5,4,8,3,7]'
13+
#
14+
# Given n non-negative integers a1, a2, ..., an , where each represents a point
15+
# at coordinate (i, ai). n vertical lines are drawn such that the two endpoints
16+
# of line i is at (i, ai) and (i, 0). Find two lines, which together with
17+
# x-axis forms a container, such that the container contains the most water.
18+
#
19+
# Note: You may not slant the container and n is at least 2.
20+
#
21+
#
22+
#
23+
#
24+
#
25+
# The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In
26+
# this case, the max area of water (blue section) the container can contain is
27+
# 49.
28+
#
29+
#
30+
#
31+
# Example:
32+
#
33+
#
34+
# Input: [1,8,6,2,5,4,8,3,7]
35+
# Output: 49
36+
#
37+
#
38+
class Solution(object):
39+
def maxArea(self, height):
40+
"""
41+
:type height: List[int]
42+
:rtype: int
43+
"""
44+
max_area = 0
45+
head = 0
46+
tail = len(height) - 1
47+
# Calculating the area by moving two pointers in bi-direction.
48+
while (tail > head):
49+
area = min([height[head], height[tail]])*(tail-head)
50+
if area > max_area:
51+
max_area = area
52+
if height[head] > height[tail]:
53+
tail = tail - 1
54+
else:
55+
head = head + 1
56+
57+
return max_area
58+
59+

12.integer-to-roman.py

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#
2+
# @lc app=leetcode id=12 lang=python
3+
#
4+
# [12] Integer to Roman
5+
#
6+
# https://leetcode.com/problems/integer-to-roman/description/
7+
#
8+
# algorithms
9+
# Medium (49.70%)
10+
# Total Accepted: 204.4K
11+
# Total Submissions: 410.8K
12+
# Testcase Example: '3'
13+
#
14+
# Roman numerals are represented by seven different symbols: I, V, X, L, C, D
15+
# and M.
16+
#
17+
#
18+
# Symbol Value
19+
# I 1
20+
# V 5
21+
# X 10
22+
# L 50
23+
# C 100
24+
# D 500
25+
# M 1000
26+
#
27+
# For example, two is written as II in Roman numeral, just two one's added
28+
# together. Twelve is written as, XII, which is simply X + II. The number
29+
# twenty seven is written as XXVII, which is XX + V + II.
30+
#
31+
# Roman numerals are usually written largest to smallest from left to right.
32+
# However, the numeral for four is not IIII. Instead, the number four is
33+
# written as IV. Because the one is before the five we subtract it making four.
34+
# The same principle applies to the number nine, which is written as IX. There
35+
# are six instances where subtraction is used:
36+
#
37+
#
38+
# I can be placed before V (5) and X (10) to make 4 and 9.
39+
# X can be placed before L (50) and C (100) to make 40 and 90.
40+
# C can be placed before D (500) and M (1000) to make 400 and 900.
41+
#
42+
#
43+
# Given an integer, convert it to a roman numeral. Input is guaranteed to be
44+
# within the range from 1 to 3999.
45+
#
46+
# Example 1:
47+
#
48+
#
49+
# Input: 3
50+
# Output: "III"
51+
#
52+
# Example 2:
53+
#
54+
#
55+
# Input: 4
56+
# Output: "IV"
57+
#
58+
# Example 3:
59+
#
60+
#
61+
# Input: 9
62+
# Output: "IX"
63+
#
64+
# Example 4:
65+
#
66+
#
67+
# Input: 58
68+
# Output: "LVIII"
69+
# Explanation: L = 50, V = 5, III = 3.
70+
#
71+
#
72+
# Example 5:
73+
#
74+
#
75+
# Input: 1994
76+
# Output: "MCMXCIV"
77+
# Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
78+
#
79+
#
80+
class Solution(object):
81+
def intToRoman(self, num):
82+
"""
83+
:type num: int
84+
:rtype: str
85+
"""
86+
tbl = {1:'I', 5:'V', 10:'X', 50:'L', 100:'C', 500:'D', 1000:'M'}
87+
num_lst = []
88+
while num != 0:
89+
tmp = num % 10
90+
num_lst.append(tmp)
91+
num = num / 10
92+
x = 1
93+
res = []
94+
for num in num_lst:
95+
if num >3 and num <9:
96+
key_1 = 5
97+
else:
98+
key_1 = 1
99+
prim_key = key_1 * x
100+
prim_val = tbl[prim_key]
101+
minor_val = tbl[x] # I, X, C, M
102+
tmp_str = ''
103+
cmp_key = num
104+
if cmp_key==4 or cmp_key==9:
105+
if cmp_key == 9:
106+
prim_val = tbl[prim_key*10]
107+
tmp_str = minor_val + prim_val # minor, primal
108+
elif cmp_key<=3:
109+
for i in range(cmp_key):
110+
tmp_str = tmp_str + minor_val # primal, primal, primal
111+
112+
elif cmp_key>5 and cmp_key<=8:
113+
# primal, minor,...
114+
tmp_str = prim_val
115+
for i in range(cmp_key-5):
116+
tmp_str = tmp_str + minor_val
117+
elif cmp_key == 5:
118+
tmp_str = prim_val
119+
res.append(tmp_str)
120+
x = x * 10
121+
res = res[::-1]
122+
res = ''.join(res)
123+
return res
124+
125+
126+
127+

15.3-sum.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#
2+
# @lc app=leetcode id=15 lang=python
3+
#
4+
# [15] 3Sum
5+
#
6+
# https://leetcode.com/problems/3sum/description/
7+
#
8+
# algorithms
9+
# Medium (23.34%)
10+
# Total Accepted: 485.6K
11+
# Total Submissions: 2.1M
12+
# Testcase Example: '[-1,0,1,2,-1,-4]'
13+
#
14+
# Given an array nums of n integers, are there elements a, b, c in nums such
15+
# that a + b + c = 0? Find all unique triplets in the array which gives the sum
16+
# of zero.
17+
#
18+
# Note:
19+
#
20+
# The solution set must not contain duplicate triplets.
21+
#
22+
# Example:
23+
#
24+
#
25+
# Given array nums = [-1, 0, 1, 2, -1, -4],
26+
#
27+
# A solution set is:
28+
# [
29+
# ⁠ [-1, 0, 1],
30+
# ⁠ [-1, -1, 2]
31+
# ]
32+
#
33+
#
34+
#
35+
'''
36+
class Solution(object):
37+
def threeSum(self, nums):
38+
"""
39+
:type nums: List[int]
40+
:rtype: List[List[int]]
41+
"""
42+
nums_len = len(nums)
43+
visible_ids = []
44+
for i in range(nums_len):
45+
46+
j = i + 1
47+
while j < nums_len:
48+
arr_col = nums[i] + nums[j]
49+
if -arr_col in nums[j+1:]:
50+
ids_set = [nums[i], nums[j], -arr_col]
51+
ids_set.sort()
52+
if ids_set not in visible_ids:
53+
visible_ids.append(ids_set)
54+
j = j + 1
55+
return visible_ids
56+
'''
57+
class Solution(object):
58+
59+
def threeSum(self, nums):
60+
"""
61+
:type nums: List[int]
62+
:rtype: List[List[int]]
63+
"""
64+
nums_len = len(nums)
65+
if nums_len < 3:
66+
return []
67+
nums.sort()
68+
visible_ids = []
69+
for i in range(nums_len-2):
70+
if i >0 and nums[i] == nums[i-1]:
71+
continue
72+
head, tail = i+1, nums_len-1
73+
74+
while head < tail:
75+
sum_res = nums[head] + nums[tail] + nums[i]
76+
if sum_res < 0:
77+
head += 1
78+
elif sum_res > 0:
79+
tail -= 1
80+
else:
81+
visible_ids.append((nums[head], nums[tail], nums[i]))
82+
while head<tail and nums[head] == nums[head+1]:
83+
head += 1
84+
while tail>head and nums[tail] == nums[tail-1]:
85+
tail -= 1
86+
head += 1
87+
tail -= 1
88+
return visible_ids
89+
90+

16.3-sum-closest.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#
2+
# @lc app=leetcode id=16 lang=python
3+
#
4+
# [16] 3Sum Closest
5+
#
6+
# https://leetcode.com/problems/3sum-closest/description/
7+
#
8+
# algorithms
9+
# Medium (38.77%)
10+
# Total Accepted: 286K
11+
# Total Submissions: 704.1K
12+
# Testcase Example: '[-1,2,1,-4]\n1'
13+
#
14+
# Given an array nums of n integers and an integer target, find three integers
15+
# in nums such that the sum is closest to target. Return the sum of the three
16+
# integers. You may assume that each input would have exactly one solution.
17+
#
18+
# Example:
19+
#
20+
#
21+
# Given array nums = [-1, 2, 1, -4], and target = 1.
22+
#
23+
# The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
24+
#
25+
#
26+
#
27+
class Solution(object):
28+
def threeSumClosest(self, nums, target):
29+
"""
30+
:type nums: List[int]
31+
:type target: int
32+
:rtype: int
33+
"""
34+
nums_len = len(nums)
35+
if nums_len < 3:
36+
return []
37+
nums.sort()
38+
visible_ids = []
39+
closest = target
40+
margin = 9999
41+
sum_res = 0
42+
for i in range(nums_len-2):
43+
if i >0 and nums[i] == nums[i-1]:
44+
continue
45+
head, tail = i+1, nums_len-1
46+
47+
while head < tail:
48+
sum_res = nums[head] + nums[tail] + nums[i]
49+
tmp_margin = max(sum_res, target) - min(sum_res, target)
50+
if tmp_margin < margin:
51+
margin = tmp_margin
52+
closest = sum_res
53+
if sum_res < target:
54+
head += 1
55+
elif sum_res > target:
56+
tail -= 1
57+
else:
58+
return sum_res
59+
return closest
60+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
# @lc app=leetcode id=17 lang=python
3+
#
4+
# [17] Letter Combinations of a Phone Number
5+
#
6+
# https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
7+
#
8+
# algorithms
9+
# Medium (40.29%)
10+
# Total Accepted: 348.2K
11+
# Total Submissions: 862K
12+
# Testcase Example: '"23"'
13+
#
14+
# Given a string containing digits from 2-9 inclusive, return all possible
15+
# letter combinations that the number could represent.
16+
#
17+
# A mapping of digit to letters (just like on the telephone buttons) is given
18+
# below. Note that 1 does not map to any letters.
19+
#
20+
#
21+
#
22+
# Example:
23+
#
24+
#
25+
# Input: "23"
26+
# Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
27+
#
28+
#
29+
# Note:
30+
#
31+
# Although the above answer is in lexicographical order, your answer could be
32+
# in any order you want.
33+
#
34+
#
35+
class Solution(object):
36+
def letterCombinations(self, digits):
37+
"""
38+
:type digits: str
39+
:rtype: List[str]
40+
"""
41+
42+

0 commit comments

Comments
 (0)