Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit042241d

Browse files
committed
Update to 019
Update to 019
1 parent90ad977 commit042241d

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines 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+
classSolution:
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+
defletterCombinations(self,digits):
28+
"""
29+
:type digits: str
30+
:rtype: List[str]
31+
"""
32+
ifnotdigits:
33+
return []
34+
result= []
35+
self.dfs(digits,"",result)
36+
returnresult
37+
38+
defdfs(self,digits,current,result):
39+
ifnotdigits:
40+
result.append(current)
41+
return
42+
forcinself.digit2letters[digits[0]]:
43+
self.dfs(digits[1:],current+c,result)
44+
45+
46+
if__name__=="__main__":
47+
assertSolution().letterCombinations("23")== ["ad","ae","af","bd","be","bf","cd","ce","cf"]
48+
49+
50+
51+
52+

‎Python3/018_4Sum.py

Lines 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+
classSolution(object):
19+
deffourSum(self,nums,target):
20+
"""
21+
:type nums: List[int]
22+
:type target: int
23+
:rtype: List[List[int]]
24+
"""
25+
iflen(nums)<4:
26+
return []
27+
result=set()
28+
sumsIndexes= {}
29+
foriinrange(len(nums)):
30+
forjinrange(i+1,len(nums)):
31+
ifnums[i]+nums[j]insumsIndexes:
32+
sumsIndexes[nums[i]+nums[j]].append((i,j))
33+
else:
34+
sumsIndexes[nums[i]+nums[j]]= [(i,j)]
35+
36+
foriinrange(len(nums)):
37+
forjinrange(i+1,len(nums)):
38+
sumNeeded=target- (nums[i]+nums[j])
39+
ifsumNeededinsumsIndexes:
40+
forindexinsumsIndexes[sumNeeded]:
41+
ifindex[0]>j:
42+
result.add(tuple(sorted([nums[i],nums[j],nums[index[0]],nums[index[1]]])))
43+
result= [list(l)forlinresult]
44+
returnresult
45+
46+
47+
if__name__=="__main__":
48+
assertSolution().fourSum([1,0,-1,0,-2,2],0)== [[-1,0,0,1], [-2,0,0,2], [-2,-1,1,2]]
49+
50+
51+
52+
Lines 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+
classListNode(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+
defmyPrint(self):
25+
print(self.val)
26+
ifself.next:
27+
self.next.myPrint()
28+
29+
30+
classSolution(object):
31+
defremoveNthFromEnd(self,head,n):
32+
"""
33+
:type head: ListNode
34+
:type n: int
35+
:rtype: ListNode
36+
"""
37+
ifnothead:
38+
returnhead
39+
point=ListNode(-1)
40+
point.next=head
41+
prev=point
42+
cur=point
43+
whileprevandn>=0:
44+
prev=prev.next
45+
n-=1
46+
whileprev:
47+
prev=prev.next
48+
cur=cur.next
49+
cur.next=cur.next.next
50+
returnpoint.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)

[8]ページ先頭

©2009-2025 Movatter.jp