Skip to content

Commit ca75633

Browse files
committed
Leetcode basecode all renamed
1 parent bb2ec94 commit ca75633

File tree

64 files changed

+492
-774
lines changed

Some content is hidden

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

64 files changed

+492
-774
lines changed

src/app/common/tree_node.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ def __init__(self, val=0, left=None, right=None):
1111
self.val = val
1212
self.left = left
1313
self.right = right
14+
self.next = None
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
3+
"""
4+
Median of Two Sorted Arrays | https://leetcode.com/problems/median-of-two-sorted-arrays/
5+
"""
6+
class MedianOfTwoSortedArray:
7+
8+
def find_median_sorted_arrays(self, nums1, nums2):
9+
ls1, ls2 = len(nums1), len(nums2)
10+
if ls1 < ls2:
11+
return self.find_median_sorted_arrays(nums2, nums1)
12+
l, r = 0, ls2 * 2
13+
while l <= r:
14+
mid2 = (l + r) >> 1
15+
mid1 = ls1 + ls2 - mid2
16+
L1 = -sys.maxint - 1 if mid1 == 0 else nums1[(mid1 - 1) >> 1]
17+
L2 = -sys.maxint - 1 if mid2 == 0 else nums2[(mid2 - 1) >> 1]
18+
R1 = sys.maxint if mid1 == 2 * ls1 else nums1[mid1 >> 1]
19+
R2 = sys.maxint if mid2 == 2 * ls2 else nums2[mid2 >> 1]
20+
if L1 > R2:
21+
l = mid2 + 1
22+
elif L2 > R1:
23+
r = mid2 - 1
24+
else:
25+
return (max(L1, L2) + min(R1, R2)) / 2.0
Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
class Solution:
2-
def myAtoi(self, str):
1+
"""
2+
String to Integer (atoi) | https://leetcode.com/problems/string-to-integer-atoi/
3+
"""
4+
class StringToIntegerAtoi:
5+
6+
def myAtoi(self, str: str) -> int:
37
"""
48
:type str: str
59
:rtype: int
@@ -24,20 +28,3 @@ def myAtoi(self, str):
2428
result = result * 10 + num
2529
pos += 1
2630
return sign * result
27-
28-
# def myAtoi(self, s):
29-
# #https://leetcode.com/discuss/83626/line-python-solution-eafp-instead-lbyl-easier-logic-beats-24%25
30-
# try:
31-
# s = s.lstrip() + '$' # remove leading spaces and append an end mark
32-
# for i, ch in enumerate(s):
33-
# if not (ch in '+-' or '0' <= ch <= '9'):
34-
# result = int(s[:i])
35-
# return -2 ** 31 if result < -2 ** 31 else 2 ** 31 - 1 if result > 2 ** 31 - 1 else result
36-
# except:
37-
# return 0
38-
39-
"""
40-
s = Solution()
41-
print s.myAtoi("+-2")
42-
"""
43-

src/app/leetcode/merge_two_sorted_lists.py renamed to src/app/leetcode/ltc_0021_merge_two_sorted_lists.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from common.list_node import ListNode
2+
13
"""
24
Merge Two Sorted Lists | https://leetcode.com/problems/merge-two-sorted-lists/
35
@@ -7,9 +9,6 @@
79
810
Return the head of the merged linked list.
911
"""
10-
11-
from common.list_node import ListNode
12-
1312
class MergeTwoSortedLists:
1413

1514
def solution_one(self, l1, l2):

src/app/leetcode/reverse_nodes_i_ k-group.py renamed to src/app/leetcode/ltc_0025_reverse_nodes_i_ k-group.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
# Definition for singly-linked list.
2-
# class ListNode
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
1+
from app.common.list_node import ListNode
62

7-
# class Solution:
8-
# def reverseKGroup(self, head, k):
9-
# """
10-
# :type head: ListNode
11-
# :type k: int
12-
# :rtype: ListNode
13-
# """
14-
class Solution:
15-
def reverseKGroup(self, head, k):
3+
"""
4+
Reverese Nodes in k-Group | https://leetcode.com/problems/reverse-nodes-in-k-group/
5+
"""
6+
class ReverseNodesInKGroup:
7+
8+
def reverse_k_group(self, head, k):
169
if head is None:
1710
return None
1811
index = 0
@@ -25,13 +18,13 @@ def reverseKGroup(self, head, k):
2518
while pos is not None:
2619
if index % k == k - 1:
2720
last = pos.next
28-
start = self.reverseList(start, last)
21+
start = self.reverse_list(start, last)
2922
pos = start
3023
pos = pos.next
3124
index += 1
3225
return head.next
3326

34-
def reverseList(self, head, end):
27+
def reverse_list(self, head, end):
3528
pos = head.next
3629
last = end
3730
next_start = pos

src/app/leetcode/remove_duplicates_from_sorted_array.py renamed to src/app/leetcode/ltc_0026_remove_duplicates_from_sorted_array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from ast import List
2+
13
"""
24
Remove Duplicates from Sorted Array | https://leetcode.com/problems/remove-duplicates-from-sorted-array/
35
@@ -10,10 +12,8 @@
1012
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
1113
1214
"""
13-
14-
from ast import List
15-
1615
class RemoveDuplicatesFromSortedArray:
16+
1717
def solution_one(self, nums: List[int]) -> int:
1818
if len(nums) == 0:
1919
return 0

src/app/leetcode/substring_with_concatenation_of_all_words.py renamed to src/app/leetcode/ltc_0030_substring_with_concatenation_of_all_words.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
class Solution:
2-
def findSubstring(self, s, words):
1+
"""
2+
Substring with Concatenation of All Words | httrps://leetcode.com/problems/substring-with-concatenation-of-all-words/
3+
"""
4+
class SubstringWithConcatenationOfAllWords:
5+
6+
def find_substring(self, s: str, words: list[str]) -> list[int]:
37
"""
48
:type s: str
59
:type words: List[str]
@@ -31,10 +35,3 @@ def findSubstring(self, s, words):
3135
# all word in target dict
3236
res.append(start)
3337
return res
34-
35-
# def findSubstring(self, s, words):
36-
# # https://leetcode.com/discuss/87745/3-line-python-solution-sorted-hash-112ms
37-
# wLen, wtLen, wSet, sortHash, sLen = len(words[0]), len(words[0]) * len(words), set(words), sorted(
38-
# [hash(w) for w in words]), len(s)
39-
# h = [hash(s[i:i + wLen]) if s[i:i + wLen] in wSet else None for i in range(sLen - wLen + 1)]
40-
# return [i for i in range(sLen - wtLen + 1) if h[i] and sorted(h[i: i + wtLen: wLen]) == sortHash]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Next Permutation | https://leetcode.com/problems/next-permutation/
3+
"""
4+
class NextPermutation(object):
5+
6+
def find_next_permutation(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: void Do not return anything, modify nums in-place instead.
10+
"""
11+
ls = len(nums)
12+
if ls <= 1:
13+
return
14+
pair = []
15+
for i in range(ls):
16+
for j in range(i + 1, ls):
17+
# append ascending order pair
18+
if nums[i] < nums[j]:
19+
pair.append([i,j])
20+
pos = 0
21+
if len(pair) > 0:
22+
self.swap(nums, pair[-1][0], pair[-1][1])
23+
pos = pair[-1][0] + 1
24+
# sort from pos
25+
for i in range(pos, ls):
26+
for j in range(i + 1, ls):
27+
if nums[i] > nums[j]:
28+
self.swap(nums, i, j)
29+
30+
def swap(self, nums: list[int], index1: int, index2: int):
31+
if index1 == index2:
32+
return
33+
nums[index1], nums[index2] = nums[index2], nums[index1]

src/app/leetcode/search_in_rotated_sorted_array.py renamed to src/app/leetcode/ltc_0033_search_in_rotated_sorted_array.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Search in Rotated Sorted Array | https://leetcode.com/problems/search-in-rotated-sorted-array/
3+
"""
14
class SearchInRotatedSortedArray:
25

36
def search(self, nums: list[int], target: int) -> int:

src/app/leetcode/search_for_a_range.py renamed to src/app/leetcode/ltc_0034_search_for_a_range.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
class Solution:
2-
def searchRange(self, nums, target):
1+
"""
2+
Search for a Range | https://leetcode.com/problems/search-for-a-range/
3+
"""
4+
class SearchForARange:
5+
6+
def find_range(self, nums: list[int], target: int) -> list[int]:
37
"""
48
:type nums: List[int]
59
:type target: int
@@ -25,4 +29,4 @@ def searchRange(self, nums, target):
2529
min = i
2630
max = i
2731
return [min, max]
28-
return [-1, -1]
32+
return [-1, -1]

0 commit comments

Comments
 (0)