Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 16f6617

Browse files
committed
2020-01-23
1 parent ffe1708 commit 16f6617

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

0905.按奇偶排序数组/0905-按奇偶排序数组.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,4 @@ def sortArrayByParity(self, A):
44
:type A: List[int]
55
:rtype: List[int]
66
"""
7-
odd, even = [], []
8-
for i in A:
9-
if i % 2:
10-
odd.append(i)
11-
else:
12-
even.append(i)
13-
14-
return even + odd
7+
return sorted(A, key = lambda x:x % 2)
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
# -*- coding: utf-8 -*-
21
class Solution(object):
32
def minAddToMakeValid(self, S):
43
"""
54
:type S: str
65
:rtype: int
76
"""
8-
dic = {"(":"", ")" : "("}
9-
res = len(S)
10-
temp = [None]
11-
for item in S:
12-
# print item
13-
if temp[-1] != dic[item.encode('utf-8')]:
14-
temp.append(item)
7+
stack = []
8+
dic = {"(":")", "{":"}", "[":"]"}
9+
for ch in S:
10+
if stack and stack[-1] in dic and dic[stack[-1]] == ch:
11+
stack.pop()
1512
else:
16-
temp = temp[:-1]
17-
return len(temp) -1
18-
19-
13+
stack.append(ch)
14+
return len(stack)

0 commit comments

Comments
 (0)