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

Commit d5db77b

Browse files
Update to 029
Update to 029
1 parent 1ed6abe commit d5db77b

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+
class Solution(object):
9+
def generateParenthesis(self, n):
10+
"""
11+
:type n: int
12+
:rtype: List[str]
13+
"""
14+
result = []
15+
self.generate(n, n, "", result)
16+
return result
17+
18+
def generate(self, left, right, string, result):
19+
if left == 0 and right == 0:
20+
result.append(string)
21+
return
22+
if left > 0:
23+
self.generate(left - 1, right, string + "(", result)
24+
if right > 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+
class ListNode(object):
17+
def __init__(self, x):
18+
self.val = x
19+
self.next = None
20+
21+
22+
class Solution(object):
23+
def swapPairs(self, head):
24+
"""
25+
:type head: ListNode
26+
:rtype: ListNode
27+
"""
28+
prev = ListNode(-1)
29+
prev.next = head
30+
temp = prev
31+
while temp.next and temp.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+
return prev.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+
class Solution(object):
11+
def divide(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+
if dividend >= 0 and divisor < 0 or dividend <= 0 and divisor > 0:
20+
sign = -1
21+
dividend = abs(dividend)
22+
divisor = abs(divisor)
23+
24+
result = 0
25+
current = divisor
26+
currentR = 1
27+
while current <= dividend:
28+
current <<= 1
29+
currentR <<= 1
30+
while divisor <= dividend:
31+
current >>= 1
32+
currentR >>= 1
33+
if current <= dividend:
34+
dividend -= current
35+
result += currentR
36+
return min(sign * result, MAX_INT)
37+
38+
39+
if __name__ == "__main__":
40+
assert Solution().divide(5, -1) == -5
41+
# assert Solution().divide(10, 2) == 5
42+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /