Skip to content

Commit 954d4fc

Browse files
committed
[20180410] easy problems
1 parent e7b2255 commit 954d4fc

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| [0009](https://leetcode.com/problems/palindrome-number/) | Palindrome Number | easy | [python](🙂easy/0009.Palindrome-Number/palindrome_number.py) | |
1515
| [0011](https://leetcode.com/problems/container-with-most-water/) | Container With Most Water | medium | [python](🤔medium/0011.Container-With-Most-Water/container_with_most_water.py) | |
1616
| [0013](https://leetcode.com/problems/roman-to-integer/description/) | Roman to Integer | easy | python | |
17+
| [0014](https://leetcode.com/problems/longest-common-prefix/) | Longest Common Prefix | easy | [python](🙂easy/0014.Longest-Common-Prefix/longest_common_prefix.py) | |
1718
| [0020](https://leetcode.com/problems/valid-parentheses/description/) | Valid Parentheses | easy | python | |
1819
| [0021](https://leetcode.com/problems/merge-two-sorted-lists/description/) | Merge Two Sorted Lists | easy | python | |
1920
| [0022](https://leetcode.com/problems/generate-parentheses/description/) | Generate Parentheses | medium | python | |
@@ -41,6 +42,7 @@
4142
| [0169](https://leetcode.com/problems/majority-element/description/) | Majority Element | easy | python | |
4243
| [0179](https://leetcode.com/problems/largest-number/description/) | Largest Number | medium | python | |
4344
| [0182](https://leetcode.com/problems/duplicate-emails/) | Duplicate Emails | easy | [sql](🙂easy/0182.Duplicate-Emails/duplicate_emails.sql) | |
45+
| [0189](https://leetcode.com/problems/rotate-array/) | Rotate Array | easy | [python](🙂easy/0189.Rotate-Array/rotate_array.py) | |
4446
| [0199](https://leetcode.com/problems/binary-tree-right-side-view/description/) | Binary Tree Right Side View | medium | python | |
4547
| [0200](https://leetcode.com/problems/number-of-islands/description/) | Number of Islands | medium | java | |
4648
| [0203](https://leetcode.com/problems/remove-linked-list-elements/) | Remove Linked List Elements | easy | [python](🙂easy/0203.Remove-Linked-List-Elements/remove_linked_list_elements.py) | |
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 14. Longest Common Prefix
2+
# https://leetcode.com/problems/longest-common-prefix/
3+
4+
from typing import List
5+
6+
7+
class Solution:
8+
# 문자열 리스트에서 가장 긴 prefix 를 찾는 문제다.
9+
def longestCommonPrefix(self, strs: List[str]) -> str:
10+
11+
# 문자열을 제일 긴 순서대로 정렬해서 전체 순회하여 가장 긴 prefix 를 찾았다.
12+
strs = sorted(strs, key=lambda x: len(x), reverse=True)
13+
14+
if len(strs) == 0:
15+
return ""
16+
elif len(strs) == 1:
17+
return strs[0]
18+
19+
prefix = strs[0]
20+
for i in range(1, len(strs)):
21+
p, q = 0, 0
22+
res = ""
23+
while p < len(prefix) and q < len(strs[i]) and prefix[p] == strs[i][q]:
24+
res += prefix[p]
25+
p += 1
26+
q += 1
27+
28+
if res != "":
29+
prefix = res
30+
else:
31+
return ""
32+
33+
return prefix
34+
35+
36+
if __name__ == '__main__':
37+
sol = Solution()
38+
assert sol.longestCommonPrefix(["flower", "flow", "flight"]) == "fl"
39+
assert sol.longestCommonPrefix(["a", "a", "b"]) == ""
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 189. Rotate Array
2+
# https://leetcode.com/problems/rotate-array/
3+
4+
from typing import List
5+
6+
7+
class Solution:
8+
def rotateTLE(self, nums: List[int], k: int) -> None:
9+
"""
10+
Do not return anything, modify nums in-place instead.
11+
"""
12+
for _ in range(k):
13+
nums[0], nums[1:len(nums)] = nums[len(nums) - 1], nums[:len(nums) - 1]
14+
15+
def rotate(self, nums: List[int], k: int) -> None:
16+
"""
17+
Do not return anything, modify nums in-place instead.
18+
"""
19+
k = k % len(nums)
20+
nums[:] = nums[-k:] + nums[:-k]
21+
22+
23+
if __name__ == '__main__':
24+
sol = Solution()
25+
print(sol.rotate([1, 2, 3, 4, 5, 6, 7, 8], 4))
26+
print(sol.rotate([1, 2, 3, 4, 5], 3))

0 commit comments

Comments
 (0)