Skip to content

Commit bdd0174

Browse files
committed
ltc basecode updated
1 parent 0fce9b2 commit bdd0174

File tree

76 files changed

+515
-834
lines changed

Some content is hidden

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

76 files changed

+515
-834
lines changed

src/app/codility/arrays/cyclic_rotation.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
66
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.
77
8-
Write a function:
9-
10-
class Solution { public int[] solution(int[] A, int K); }
11-
12-
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.
8+
Write a function that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.
139
1410
For example, given
1511
@@ -39,7 +35,6 @@ class Solution { public int[] solution(int[] A, int K); }
3935
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
4036
4137
"""
42-
4338
class CyclicRotation:
4439
def rotate(self, a: list[int], k: int) -> list[int]:
4540
"""

src/app/common/interval.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Interval:
2+
def __init__(self, s=0, e=0):
3+
self.start = s
4+
self.end = e

src/app/common/list_node.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ class ListNode(object):
77
def __init__(self, x):
88
self.val = x
99
self.next = None
10+
11+
class RandomListNode:
12+
def __init__(self, x):
13+
self.label = x
14+
self.next = None
15+
self.random = None

src/app/leetcode/degree_of_an_array.py

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Reverse Integer | https://leetcode.com/problems/reverse-integer/
3+
"""
4+
class RevereseInteger:
5+
6+
def reverse(self, x: int) -> int:
7+
# Note that in Python -1 / 10 = -1
8+
res, is_pos = 0, 1
9+
if x < 0:
10+
is_pos = -1
11+
x = -1 * x
12+
while x != 0:
13+
res = res * 10 + x % 10
14+
if res > 2147483647:
15+
return 0
16+
x /= 10
17+
return res * is_pos

src/app/leetcode/regular_expression_matching.py renamed to src/app/leetcode/ltc_0010_regular_expression_matching.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
"""
2-
2+
Regular Expression Matching | https://leetcode.com/problems/regular-expression-matching/
33
"""
4-
54
class RegularExpressionMatching:
6-
def isMatch(self, s, p):
5+
6+
def is_match(self, s: str, p: str) -> bool:
77
"""
88
:type s: str
99
:type p: str
1010
:rtype: bool
1111
"""
1212
# bottom up o(m*n)
13-
# https://leetcode.com/discuss/93024/easy-dp-java-solution-with-detailed-explanation
1413
if s == p:
1514
return True
1615
m, n = len(s), len(p)
@@ -30,6 +29,3 @@ def isMatch(self, s, p):
3029
else:
3130
dp[i + 1][j + 1] = dp[i + 1][j] or dp[i][j + 1] or dp[i + 1][j - 1]
3231
return dp[m][n]
33-
34-
# s = Solution()
35-
# print(s.isMatch("", ".*"))
Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
2-
https://leetcode.com/problems/3sum/description/
2+
Three Sum | https://leetcode.com/problems/3sum/description/
3+
problem #
34
45
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
56
Notice that the solution set must not contain duplicate triplets.
@@ -39,59 +40,9 @@
3940
4041
"""
4142

42-
class ThreeSum
43-
# def threeSum(self, nums):
44-
# # skip duplicate
45-
# # O(n^3)
46-
# if len(nums) < 3:
47-
# return []
48-
# nums.sort()
49-
# ls = len(nums)
50-
# result = []
51-
# for i in range(0, ls - 2):
52-
# if nums[i] > 0:
53-
# break
54-
# if i > 0 and nums[i] == nums[i - 1]:
55-
# continue
56-
# j = i + 1
57-
# k = ls - 1
58-
# while(j < k):
59-
# temp = [nums[i], nums[j], nums[k]]
60-
# s = sum(temp)
61-
# jtemp = nums[j]
62-
# ktemp = nums[k]
63-
# if s <= 0:
64-
# j += 1
65-
# while(j < k and jtemp == nums[j]):
66-
# j += 1
67-
# if s == 0:
68-
# result.append(temp)
69-
# else:
70-
# k -= 1
71-
# while(j < k and ktemp == nums[k]):
72-
# k -= 1
73-
# return result
43+
class ThreeSum:
7444

75-
# def threeSum(self, nums):
76-
# """
77-
# :type nums: List[int]
78-
# :rtype: List[List[int]]
79-
# """
80-
# # using multiple 2 sum
81-
# nums.sort()
82-
# result, visited = set(), {}
83-
# for i in range(len(nums) - 2):
84-
# table, target = {}, -nums[i]
85-
# if nums[i] not in visited:
86-
# for j in range(i + 1, len(nums)):
87-
# if nums[j] not in table:
88-
# table[target - nums[j]] = j
89-
# else:
90-
# result.add((nums[i], target - nums[j], nums[j]))
91-
# visited[nums[i]] = 1
92-
# return list(result)
93-
94-
def solution_one(self, nums):
45+
def solution_one(self, nums: list[int]) -> list[list[int]]:
9546
res = []
9647
nums.sort()
9748
ls = len(nums)
@@ -138,4 +89,4 @@ def solution_two(self, nums: list[int]) -> list[list[int]]:
13889
while nums[ptrL] == nums[ptrL - 1] and ptrL < ptrR:
13990
ptrL += 1
14091

141-
return triplets
92+
return triplets
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from common.list_node import ListNode
2+
3+
"""
4+
Merge k sorted linked lists | https://leetcode.com/problems/merge-k-sorted-lists/
5+
"""
6+
class MergeKSortedLists(object):
7+
8+
def merge_k_lists(self, lists):
9+
# recursive
10+
if lists is None:
11+
return None
12+
elif len(lists) == 0:
13+
return None
14+
return self.merge_k(lists, 0, len(lists) - 1)
15+
16+
def merge_k(self, lists, low, high):
17+
if low == high:
18+
return lists[low]
19+
elif low + 1 == high:
20+
return self.merge_two_lists(lists[low], lists[high])
21+
mid = (low + high) / 2
22+
return self.merge_two_lists(self.mergeK(lists, low, mid), self.mergeK(lists, mid + 1, high))
23+
24+
def merge_two_lists(self, l1, l2):
25+
if l1 is None:
26+
return l2
27+
if l2 is None:
28+
return l1
29+
head = curr = ListNode(-1)
30+
while l1 is not None and l2 is not None:
31+
if l1.val <= l2.val:
32+
curr.next = l1
33+
l1 = l1.next
34+
else:
35+
curr.next = l2
36+
l2 = l2.next
37+
curr = curr.next
38+
if l1 is not None:
39+
curr.next = l1
40+
if l2 is not None:
41+
curr.next = l2
42+
return head.next

src/app/leetcode/trapping_rain_water.py renamed to src/app/leetcode/ltc_0042_trapping_rain_water.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
class Solution:
2-
def trap(self, height):
1+
"""
2+
Trapping Rain Water | https://leetcode.com/problems/trapping-rain-water/description/
3+
"""
4+
class TrappingRainWater(object):
5+
6+
def trap(self, height: list) -> int:
37
"""
48
:type height: List[int]
59
:rtype: int
@@ -42,35 +46,3 @@ def rain_water(self, height, start, end):
4246
if height[index] > 0:
4347
step += height[index]
4448
return res - step
45-
46-
# def trap(self, height):
47-
# ls = len(height)
48-
# if ls == 0:
49-
# return 0
50-
# height.append(0)
51-
# height.insert(0, 0)
52-
# left = [0] * ls
53-
# right = [0] * ls
54-
# cur_left, cur_right = 0, 0
55-
# for i in range(1, ls + 1):
56-
# cur_left = max(cur_left, height[i - 1])
57-
# # left[i] store max bar from left
58-
# left[i - 1] = cur_left
59-
# for i in reversed(range(ls)):
60-
# cur_right = max(cur_right, height[i + 1])
61-
# # right[i] store max bar from right
62-
# right[i] = cur_right
63-
# res = 0
64-
# for i in range(ls):
65-
# curr = min(left[i], right[i])
66-
# if curr > height[i]:
67-
# res += curr - height[i]
68-
# return res
69-
70-
71-
if __name__ == '__main__':
72-
# begin
73-
s = Solution()
74-
print s.trap([2,6,3,8,2,7,2,5,0])
75-
76-
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Permutations | https://leetcode.com/problems/permutations/
3+
"""
4+
class Permutations(object):
5+
6+
def permute(self, nums):
7+
# DPS with swapping
8+
res = []
9+
if len(nums) == 0:
10+
return res
11+
self.get_permute(res, nums, 0)
12+
return res
13+
14+
def get_permute(self, res, nums, index):
15+
if index == len(nums):
16+
res.append(list(nums))
17+
return
18+
for i in range(index, len(nums)):
19+
nums[i], nums[index] = nums[index], nums[i]
20+
# s(n) = 1 + s(n-1)
21+
self.get_permute(res, nums, index + 1)
22+
nums[i], nums[index] = nums[index], nums[i]

0 commit comments

Comments
 (0)