Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 042241d

Browse filesBrowse files
committed
Update to 019
Update to 019
1 parent 90ad977 commit 042241d
Copy full SHA for 042241d

File tree

Expand file treeCollapse file tree

3 files changed

+172
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+172
-0
lines changed
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a digit string, return all possible letter combinations that the number could represent.
5+
6+
A mapping of digit to letters (just like on the telephone buttons) is given below.
7+
8+
Input:Digit string "23"
9+
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
10+
Note:
11+
Although the above answer is in lexicographical order, your answer could be in any order you want.
12+
'''
13+
14+
15+
class Solution:
16+
digit2letters = {
17+
'2': "abc",
18+
'3': "def",
19+
'4': "ghi",
20+
'5': "jkl",
21+
'6': "mno",
22+
'7': "pqrs",
23+
'8': "tuv",
24+
'9': "wxyz",
25+
}
26+
27+
def letterCombinations(self, digits):
28+
"""
29+
:type digits: str
30+
:rtype: List[str]
31+
"""
32+
if not digits:
33+
return []
34+
result = []
35+
self.dfs(digits, "", result)
36+
return result
37+
38+
def dfs(self, digits, current, result):
39+
if not digits:
40+
result.append(current)
41+
return
42+
for c in self.digit2letters[digits[0]]:
43+
self.dfs(digits[1:], current + c, result)
44+
45+
46+
if __name__ == "__main__":
47+
assert Solution().letterCombinations("23") == ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
48+
49+
50+
51+
52+

‎Python3/018_4Sum.py

Copy file name to clipboard
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
5+
6+
Note:
7+
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
8+
The solution set must not contain duplicate quadruplets.
9+
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
10+
11+
A solution set is:
12+
(-1, 0, 0, 1)
13+
(-2, -1, 1, 2)
14+
(-2, 0, 0, 2)
15+
'''
16+
17+
18+
class Solution(object):
19+
def fourSum(self, nums, target):
20+
"""
21+
:type nums: List[int]
22+
:type target: int
23+
:rtype: List[List[int]]
24+
"""
25+
if len(nums) < 4:
26+
return []
27+
result = set()
28+
sumsIndexes = {}
29+
for i in range(len(nums)):
30+
for j in range(i + 1, len(nums)):
31+
if nums[i] + nums[j] in sumsIndexes:
32+
sumsIndexes[nums[i] + nums[j]].append((i, j))
33+
else:
34+
sumsIndexes[nums[i] + nums[j]] = [(i, j)]
35+
36+
for i in range(len(nums)):
37+
for j in range(i + 1, len(nums)):
38+
sumNeeded = target - (nums[i] + nums[j])
39+
if sumNeeded in sumsIndexes:
40+
for index in sumsIndexes[sumNeeded]:
41+
if index[0] > j:
42+
result.add(tuple(sorted([nums[i], nums[j], nums[index[0]], nums[index[1]]])))
43+
result = [list(l) for l in result]
44+
return result
45+
46+
47+
if __name__ == "__main__":
48+
assert Solution().fourSum([1, 0, -1, 0, -2, 2], 0) == [[-1, 0, 0, 1], [-2, 0, 0, 2], [-2, -1, 1, 2]]
49+
50+
51+
52+
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a linked list, remove the nth node from the end of list and return its head.
5+
6+
For example,
7+
8+
Given linked list: 1->2->3->4->5, and n = 2.
9+
10+
After removing the second node from the end, the linked list becomes 1->2->3->5.
11+
Note:
12+
Given n will always be valid.
13+
Try to do this in one pass.
14+
'''
15+
16+
17+
# Definition for singly-linked list.
18+
class ListNode(object):
19+
def __init__(self, x):
20+
self.val = x
21+
self.next = None
22+
23+
# Define this to check if it works well
24+
def myPrint(self):
25+
print(self.val)
26+
if self.next:
27+
self.next.myPrint()
28+
29+
30+
class Solution(object):
31+
def removeNthFromEnd(self, head, n):
32+
"""
33+
:type head: ListNode
34+
:type n: int
35+
:rtype: ListNode
36+
"""
37+
if not head:
38+
return head
39+
point = ListNode(-1)
40+
point.next = head
41+
prev = point
42+
cur = point
43+
while prev and n >=0:
44+
prev = prev.next
45+
n -= 1
46+
while prev:
47+
prev = prev.next
48+
cur = cur.next
49+
cur.next = cur.next.next
50+
return point.next
51+
52+
53+
if __name__ == "__main__":
54+
n5 = ListNode(5)
55+
n4 = ListNode(4)
56+
n3 = ListNode(3)
57+
n2 = ListNode(2)
58+
n1 = ListNode(1)
59+
n1.next = n2
60+
n2.next = n3
61+
n3.next = n4
62+
n4.next = n5
63+
result = Solution().removeNthFromEnd(n1, 5)
64+
result.myPrint()
65+
66+
67+
68+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.