forked from JiayangWu/LeetCode-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
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
178 changed files
with
2,893 additions
and
2,212 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,15 @@ | ||
class Solution(object): | ||
def twoSum(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
""" | ||
hashmap = {} | ||
for i, x in enumerate(nums): | ||
if hashmap.has_key(target - x): | ||
return [hashmap[target - x], i] | ||
else: | ||
hashmap[x] = i | ||
|
||
return [] |
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,16 @@ | ||
class Solution(object): | ||
def lengthOfLongestSubstring(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
record = {} | ||
start, res = 0, 0 | ||
for end in range(len(s)): | ||
if s[end] in record: | ||
start = max(start, record[s[end]] + 1) | ||
|
||
record[s[end]] = end | ||
res = max(res, end - start + 1) | ||
|
||
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
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
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 |
---|---|---|
@@ -1,35 +1,40 @@ | ||
class Solution(object): | ||
def convert(self, s, n): | ||
def convert(self, s, numRows): | ||
""" | ||
:type s: str | ||
:type numRows: int | ||
:rtype: str | ||
""" | ||
#ÕÒ¹æÂÉ | ||
if n <= 1: | ||
#第一行和最后一行都是相差 2 * (n - 1) | ||
#对于直角在上面的直角三角形, 相差 2 * (n - 1 - i) | ||
#对于直角在下面的直角三角形, 相差 2 * i | ||
if not s or numRows == 1: | ||
return s | ||
l = len(s) | ||
res = "" | ||
for i in range(n): | ||
tmp, index = "", i | ||
if i in [0, n - 1]: | ||
while(index < l): | ||
|
||
tmp += s[index] | ||
index += 2 * (n - 1) | ||
for idx in range(numRows): | ||
if idx < len(s): | ||
res += s[idx] | ||
|
||
if idx in [0, numRows - 1]: | ||
tmp = idx + 2 *(numRows - 1) | ||
while tmp < len(s): | ||
res += s[tmp] | ||
tmp += 2 *(numRows - 1) | ||
else: | ||
state = "down" | ||
while(index < l): | ||
tmp += s[index] | ||
if state == "down": | ||
state = "up" | ||
index += 2 * (n - 1 - i) | ||
else: | ||
state = "down" | ||
index += 2 * i | ||
res += tmp | ||
|
||
tmp = idx + 2 * (numRows - 1 - idx) | ||
tri = "down" | ||
while tmp < len(s): | ||
res += s[tmp] | ||
if tri == "up": | ||
tmp += 2 * (numRows - 1 - idx) | ||
tri = "down" | ||
else: | ||
tmp += 2 * idx | ||
tri = "up" | ||
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
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
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 |
---|---|---|
@@ -1,11 +1,17 @@ | ||
class Solution(object): | ||
def isPalindrome(self, x): | ||
""" | ||
:type x: int | ||
:rtype: bool | ||
""" | ||
#2019.6.1 | ||
xx = x | ||
if x < 0: | ||
return False | ||
|
||
s = str(x) | ||
if len(s) <= 1: | ||
return True | ||
print len(s) / 2 | ||
for count in range(0, len(s)/2): | ||
if s[count] != s[len(s)-1 - count]: | ||
return False | ||
return True | ||
reverse = 0 | ||
while x > 0: | ||
x, tmp = divmod(x, 10) | ||
reverse = reverse * 10 + tmp | ||
|
||
return reverse == xx |
Oops, something went wrong.