Skip to content

Commit 52d648c

Browse files
committedMay 24, 2019
2019-5-24
1 parent a311bb2 commit 52d648c

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution(object):
2+
def lengthOfLongestSubstringTwoDistinct(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
left, right = 0, 0
8+
queue = []
9+
last_pos = {}
10+
res = 0
11+
for right in range(len(s)):
12+
if s[right] in queue:
13+
last_pos[s[right]] = right
14+
elif s[right] not in queue:
15+
if len(queue) >= 2:
16+
17+
if last_pos[queue[0]] < last_pos[queue[1]]: #°Ñ0ºÅÔªËØÌßµô
18+
left = last_pos[queue[0]] + 1
19+
last_pos.pop(queue[0])
20+
queue.pop(0)
21+
else:
22+
left = last_pos[queue[1]] + 1
23+
last_pos.pop(queue[1])
24+
queue.pop(1)
25+
26+
queue.append(s[right])
27+
last_pos[s[right]] = right
28+
# print s[left:right + 1]
29+
res = max(res, right - left + 1)
30+
return res
31+
32+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class ZigzagIterator(object):
2+
3+
def __init__(self, v1, v2):
4+
"""
5+
Initialize your data structure here.
6+
:type v1: List[int]
7+
:type v2: List[int]
8+
"""
9+
self.list = []
10+
if v1 and v2:
11+
i = 0
12+
for i in range(min(len(v1), len(v2))):
13+
self.list.append(v1[i])
14+
self.list.append(v2[i])
15+
if v1[i + 1:]:
16+
self.list += v1[i + 1:]
17+
else:
18+
self.list += v2[i + 1:]
19+
20+
elif v1:
21+
self.list = v1
22+
else:
23+
self.list = v2
24+
self.index = 0
25+
def next(self):
26+
"""
27+
:rtype: int
28+
"""
29+
self.index += 1
30+
return self.list[self.index - 1]
31+
32+
def hasNext(self):
33+
"""
34+
:rtype: bool
35+
"""
36+
return self.index != len(self.list)
37+
38+
39+
# Your ZigzagIterator object will be instantiated and called as such:
40+
# i, v = ZigzagIterator(v1, v2), []
41+
# while i.hasNext(): v.append(i.next())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class TicTacToe(object):
2+
3+
def __init__(self, n):
4+
"""
5+
Initialize your data structure here.
6+
:type n: int
7+
"""
8+
self.size = n
9+
self.board = [[0 for _ in range(n)] for j in range(n) ]
10+
def move(self, row, col, player):
11+
"""
12+
Player {player} makes a move at ({row}, {col}).
13+
@param row The row of the board.
14+
@param col The column of the board.
15+
@param player The player, can be either 1 or 2.
16+
@return The current winning condition, can be either:
17+
0: No one wins.
18+
1: Player 1 wins.
19+
2: Player 2 wins.
20+
:type row: int
21+
:type col: int
22+
:type player: int
23+
:rtype: int
24+
"""
25+
self.board[row][col] = player
26+
if self.check(row, col, player):
27+
return player
28+
29+
return 0
30+
31+
32+
def check(self, row, col, toe):
33+
row_valid = True
34+
col_valid = True
35+
36+
37+
for i in range(self.size):
38+
if self.board[row][i] != toe:
39+
row_valid = False
40+
if self.board[i][col] != toe:
41+
# print i, self.board[col][i], toe, col_valid
42+
col_valid = False
43+
44+
if row != col and row + col != self.size - 1:
45+
return row_valid or col_valid
46+
47+
dia_valid1 = False
48+
dia_valid2 = False
49+
if row == col:
50+
dia_valid1 = True
51+
for i in range(self.size):
52+
if self.board[i][i] != toe:
53+
dia_valid1 = False
54+
if row + col == self.size - 1:
55+
dia_valid2 = True
56+
for i in range(self.size):
57+
if self.board[i][self.size - 1 - i] != toe:
58+
dia_valid2 = False
59+
# print self.board
60+
# print (row, col), toe, row_valid, col_valid ,dia_valid1 ,dia_valid2
61+
return row_valid or col_valid or dia_valid1 or dia_valid2
62+
63+
64+
# for j in range(self.)
65+
66+
67+
68+
# Your TicTacToe object will be instantiated and called as such:
69+
# obj = TicTacToe(n)
70+
# param_1 = obj.move(row,col,player)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def search(self, reader, target):
3+
"""
4+
:type reader: ArrayReader
5+
:type target: int
6+
:rtype: int
7+
"""
8+
i = 0
9+
tmp = reader.get(i)
10+
if tmp > target: #target比第一个元素都小,所以不存在
11+
return -1
12+
while tmp != 2147483647:
13+
if tmp == target: #找到了
14+
return i
15+
if tmp > target: #后面的元素都比target大,找不到
16+
break
17+
i += 1
18+
tmp = reader.get(i)
19+
return -1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import deque
2+
class RecentCounter(object):
3+
def __init__(self):
4+
self.queue = deque()
5+
6+
def ping(self, t):
7+
"""
8+
:type t: int
9+
:rtype: int
10+
"""
11+
self.queue.append(t)
12+
13+
while(self.queue[0] < t - 3000):
14+
self.queue.popleft()
15+
16+
return len(self.queue)

0 commit comments

Comments
 (0)
Please sign in to comment.