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

Commitd5db77b

Browse files
committed
Update to 029
Update to 029
1 parent1ed6abe commitd5db77b

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

‎Python3/022_Generate_Parentheses.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
5+
'''
6+
7+
8+
classSolution(object):
9+
defgenerateParenthesis(self,n):
10+
"""
11+
:type n: int
12+
:rtype: List[str]
13+
"""
14+
result= []
15+
self.generate(n,n,"",result)
16+
returnresult
17+
18+
defgenerate(self,left,right,string,result):
19+
ifleft==0andright==0:
20+
result.append(string)
21+
return
22+
ifleft>0:
23+
self.generate(left-1,right,string+"(",result)
24+
ifright>left:
25+
self.generate(left,right-1,string+")",result)
26+
27+
28+
if__name__=="__main__":
29+
assert (Solution().generateParenthesis(3))== ['((()))','(()())','(())()','()(())','()()()']
30+

‎Python3/024_Swap_Nodes_in_Pairs.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a linked list, swap every two adjacent nodes and return its head.
5+
6+
For example,
7+
Given 1->2->3->4, you should return the list as 2->1->4->3.
8+
9+
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
10+
11+
Subscribe to see which companies asked this question
12+
'''
13+
14+
15+
# Definition for singly-linked list.
16+
classListNode(object):
17+
def__init__(self,x):
18+
self.val=x
19+
self.next=None
20+
21+
22+
classSolution(object):
23+
defswapPairs(self,head):
24+
"""
25+
:type head: ListNode
26+
:rtype: ListNode
27+
"""
28+
prev=ListNode(-1)
29+
prev.next=head
30+
temp=prev
31+
whiletemp.nextandtemp.next.next:
32+
node1=temp.next
33+
node2=temp.next.next
34+
temp.next=node2
35+
node1.next=node2.next
36+
node2.next=node1
37+
temp=temp.next.next
38+
returnprev.next

‎Python3/029_Divide_Two_Integers.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Divide two integers without using multiplication, division and mod operator.
5+
6+
If it is overflow, return MAX_INT.
7+
'''
8+
9+
10+
classSolution(object):
11+
defdivide(self,dividend,divisor):
12+
"""
13+
:type dividend: int
14+
:type divisor: int
15+
:rtype: int
16+
"""
17+
MAX_INT=2**31-1
18+
sign=1
19+
ifdividend>=0anddivisor<0ordividend<=0anddivisor>0:
20+
sign=-1
21+
dividend=abs(dividend)
22+
divisor=abs(divisor)
23+
24+
result=0
25+
current=divisor
26+
currentR=1
27+
whilecurrent<=dividend:
28+
current<<=1
29+
currentR<<=1
30+
whiledivisor<=dividend:
31+
current>>=1
32+
currentR>>=1
33+
ifcurrent<=dividend:
34+
dividend-=current
35+
result+=currentR
36+
returnmin(sign*result,MAX_INT)
37+
38+
39+
if__name__=="__main__":
40+
assertSolution().divide(5,-1)==-5
41+
# assert Solution().divide(10, 2) == 5
42+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp