-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
731 changed files
with
12,838 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution(object): | ||
def addTwoNumbers(self, l1, l2): | ||
""" | ||
:type l1: ListNode | ||
:type l2: ListNode | ||
:rtype: ListNode | ||
""" | ||
if self.getLength(l1) < self.getLength(l2): | ||
l1, l2 = l2, l1 | ||
head = l1 | ||
while(l2): | ||
l1.val += l2.val | ||
l1 = l1.next | ||
l2 = l2.next | ||
|
||
p = head | ||
while(p): | ||
if p.val > 9: | ||
p.val -= 10 | ||
if p.next: | ||
p.next.val += 1 | ||
else: | ||
p.next = ListNode(1) | ||
p = p.next | ||
return head | ||
|
||
|
||
def getLength(self, l): | ||
tmp = 0 | ||
while(l): | ||
tmp += 1 | ||
l = l.next | ||
return tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution(object): | ||
def isPalindrome(self, x): | ||
""" | ||
:type x: int | ||
:rtype: bool | ||
""" | ||
#2019.6.1 | ||
xx = x | ||
if x < 0: | ||
return False | ||
|
||
reverse = 0 | ||
while x > 0: | ||
x, tmp = divmod(x, 10) | ||
reverse = reverse * 10 + tmp | ||
|
||
return reverse == xx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: | ||
l = 0 | ||
p = head | ||
while p: | ||
l += 1 | ||
p = p.next | ||
|
||
count = l - n + 1 | ||
|
||
dummy = ListNode(-1) | ||
dummy.next = head | ||
cur = -1 | ||
p = dummy | ||
while p: | ||
cur += 1 | ||
if cur == count - 1: | ||
node_to_be_deleted = p.next | ||
p.next = node_to_be_deleted.next | ||
break | ||
p = p.next | ||
|
||
return dummy.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution(object): | ||
def isValid(self, s): | ||
""" | ||
:type s: str | ||
:rtype: bool | ||
""" | ||
dic = {")": "(", "]":"[", "}":"{"} | ||
stack = [] | ||
for ch in s: | ||
if ch in ["(", "[", "{"]: | ||
stack.append(ch) | ||
else: | ||
if not stack or dic[ch] != stack[-1]: | ||
return False | ||
stack.pop() | ||
return len(stack) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution(object): | ||
def mergeTwoLists(self, l1, l2): | ||
""" | ||
:type l1: ListNode | ||
:type l2: ListNode | ||
:rtype: ListNode | ||
""" | ||
dummy = ListNode(-1) | ||
|
||
p = dummy | ||
|
||
while l1 and l2: | ||
if l1.val <= l2.val: | ||
p.next = ListNode(l1.val) | ||
l1 = l1.next | ||
else: | ||
p.next = ListNode(l2.val) | ||
l2 = l2.next | ||
p = p.next | ||
|
||
if l1: | ||
p.next = l1 | ||
|
||
if l2: | ||
p.next = l2 | ||
|
||
return dummy.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution(object): | ||
def generateParenthesis(self, n): | ||
""" | ||
:type n: int | ||
:rtype: List[str] | ||
""" | ||
|
||
res = [] | ||
|
||
def dfs(tmp, left, right): | ||
if len(tmp) == 2 * n: | ||
res.append(tmp) | ||
|
||
if left: | ||
dfs(tmp + "(", left - 1, right) | ||
if right > left: | ||
dfs(tmp + ")", left, right - 1) | ||
|
||
|
||
dfs("", n, n) | ||
return res | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution(object): | ||
def swapPairs(self, head): | ||
""" | ||
:type head: ListNode | ||
:rtype: ListNode | ||
""" | ||
if not head or not head.next: | ||
return head | ||
dummy = ListNode(1) | ||
dummy.next = head | ||
|
||
first = head | ||
second = head.next | ||
|
||
tail = second.next | ||
first.next = self.swapPairs(tail) | ||
second.next = first | ||
dummy.next = second | ||
|
||
return dummy.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def removeDuplicates(self, nums: List[int]) -> int: | ||
visited = set() | ||
index = 0 | ||
for num in nums: | ||
if num not in visited: | ||
visited.add(num) | ||
nums[index] = num | ||
index += 1 | ||
return index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution(object): | ||
def removeElement(self, nums, val): | ||
""" | ||
:type nums: List[int] | ||
:type val: int | ||
:rtype: int | ||
""" | ||
nums.sort() | ||
for i, num in enumerate(nums): | ||
if num == val: | ||
j = i + 1 | ||
while(j < len(nums) and nums[j] == num): | ||
j += 1 | ||
t = j | ||
while(j < len(nums)): | ||
nums[i] = nums[j] | ||
i += 1 | ||
j += 1 | ||
return i | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Solution: | ||
def strStr(self, haystack: str, needle: str) -> int: | ||
if needle not in haystack: | ||
return -1 | ||
return haystack.index(needle) |
56 changes: 56 additions & 0 deletions
56
0034.在排序数组中查找元素的第一个和最后一个位置/0034-在排序数组中查找元素的第一个和最后一个位置 2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class Solution(object): | ||
def searchRange(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
""" | ||
lo, hi = 0, len(nums) - 1 | ||
|
||
while lo <= hi: | ||
|
||
mid = (lo + hi) // 2 | ||
# print lo, hi, mid, nums | ||
if nums[mid] == target: | ||
break | ||
elif nums[mid] < target: | ||
lo = mid + 1 | ||
else: | ||
hi = mid - 1 | ||
|
||
if lo > hi: | ||
return [-1, -1] | ||
midposition = mid | ||
leftside, rightside = midposition, midposition | ||
#ÕÒ×ó±ß½ç | ||
# print 1 | ||
lo, hi = 0, midposition | ||
while lo <= hi: | ||
# print lo, hi, mid | ||
mid = (lo + hi) // 2 | ||
if nums[mid] < target: | ||
lo = mid + 1 | ||
elif nums[mid] == target: | ||
if mid == 0 or (mid - 1 >= 0 and nums[mid - 1] < target): | ||
leftside = mid | ||
break | ||
else: | ||
hi = mid - 1 | ||
# print 1 | ||
#ÕÒÓұ߽ç | ||
lo, hi = midposition, len(nums) - 1 | ||
while lo <= hi: | ||
mid = (lo + hi) // 2 | ||
if nums[mid] > target: | ||
hi = mid - 1 | ||
elif nums[mid] == target: | ||
if mid == len(nums) - 1 or (mid + 1 < len(nums) and nums[mid + 1] > target): | ||
rightside = mid | ||
break | ||
else: | ||
lo = mid + 1 | ||
|
||
return [leftside, rightside] | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution(object): | ||
def searchInsert(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: int | ||
""" | ||
left, right = 0, len(nums) - 1 | ||
while left <= right: | ||
mid = (left + right) // 2 | ||
|
||
if nums[mid] == target: | ||
return mid | ||
elif nums[mid] > target: | ||
right = mid - 1 | ||
elif nums[mid] < target: | ||
left = mid + 1 | ||
return left |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def isValidSudoku(self, board: List[List[str]]) -> bool: | ||
row = defaultdict(set) | ||
col = defaultdict(set) | ||
square = defaultdict(set) | ||
for i in range(9): | ||
for j in range(9): | ||
if board[i][j].isdigit(): | ||
if board[i][j] in row[i] or board[i][j] in col[j] or board[i][j] in square[(i // 3, j // 3)]: | ||
return False | ||
else: | ||
row[i].add(board[i][j]) | ||
col[j].add(board[i][j]) | ||
square[(i // 3, j // 3)].add(board[i][j]) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Solution: | ||
def jump(self, nums: List[int]) -> int: | ||
dp = [float("inf") for _ in nums] | ||
dp[0] = 0 | ||
for i, num in enumerate(nums): | ||
for j in range(1, 1 + num): | ||
if i + j < len(nums): | ||
dp[i + j] = min(dp[i + j], dp[i] + 1) | ||
return dp[-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution(object): | ||
def permute(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: List[List[int]] | ||
""" | ||
res = [] | ||
def dfs(tmp, nums): | ||
if not nums: | ||
res.append(tmp) | ||
|
||
for i, x in enumerate(nums): | ||
dfs(tmp + [x], nums[:i] + nums[i + 1:]) | ||
|
||
dfs([], nums) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution(object): | ||
def rotate(self, matrix): | ||
""" | ||
:type matrix: List[List[int]] | ||
:rtype: None Do not return anything, modify matrix in-place instead. | ||
""" | ||
#ÏÈתÖÃÔÙ×óÓҶԳƷת | ||
if not matrix or not matrix[0]: | ||
return matrix | ||
n = len(matrix) | ||
|
||
for i in range(n): | ||
for j in range(i + 1, n): | ||
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] | ||
|
||
for row in matrix: | ||
for i in range(n // 2): | ||
row[i], row[n - 1 - i] = row[n - 1 - i], row[i] | ||
|
||
return matrix |
Oops, something went wrong.