Skip to content

Commit 3e4a6bf

Browse files
committed
400
400
1 parent e6ef18d commit 3e4a6bf

File tree

19 files changed

+878
-26
lines changed

19 files changed

+878
-26
lines changed

Python3/382.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,85 @@
11
__________________________________________________________________________________________________
2+
sample 72 ms submission
3+
# Definition for singly-linked list.
4+
# class ListNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.next = None
8+
import random
29

10+
11+
class Solution:
12+
13+
def __init__(self, head: ListNode):
14+
self.head = head
15+
self.count = self.to_count()
16+
"""
17+
@param head The linked list's head.
18+
Note that the head is guaranteed to be not null, so it contains at least one node.
19+
"""
20+
21+
22+
def getRandom(self) -> int:
23+
node = self.head
24+
p = random.random()//(1/self.count)
25+
for i in range(int(p)):
26+
node = node.next
27+
return node.val
28+
"""
29+
Returns a random node's value.
30+
"""
31+
32+
def to_count(self):
33+
node = self.head
34+
count = 0
35+
while node is not None:
36+
count += 1
37+
node = node.next
38+
return count
39+
40+
# Your Solution object will be instantiated and called as such:
41+
# obj = Solution(head)
42+
# param_1 = obj.getRandom()
343
__________________________________________________________________________________________________
44+
sample 15976 kb submission
45+
# Definition for singly-linked list.
46+
# class ListNode:
47+
# def __init__(self, x):
48+
# self.val = x
49+
# self.next = None
50+
51+
from random import random
52+
53+
class Solution:
54+
55+
def __init__(self, head: ListNode):
56+
"""
57+
@param head The linked list's head.
58+
Note that the head is guaranteed to be not null, so it contains at least one node.
59+
"""
60+
self.head = head
61+
pass
62+
63+
def getRandom(self) -> int:
64+
"""
65+
Returns a random node's value.
66+
"""
67+
max_p = -1
68+
cur = self.head
69+
res = 0
70+
while cur:
71+
cur_p = random()
72+
if cur_p > max_p:
73+
res = cur.val
74+
max_p = cur_p
75+
cur = cur.next
76+
77+
return res
78+
79+
80+
481

82+
# Your Solution object will be instantiated and called as such:
83+
# obj = Solution(head)
84+
# param_1 = obj.getRandom()
585
__________________________________________________________________________________________________

Python3/383.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
__________________________________________________________________________________________________
2-
2+
sample 28 ms submission
3+
class Solution:
4+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
5+
6+
r = set(ransomNote)
7+
for i in r:
8+
if ransomNote.count(i) > magazine.count(i):
9+
return False
10+
return True
311
__________________________________________________________________________________________________
4-
12+
sample 13260 kb submission
13+
class Solution:
14+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
15+
for char in ransomNote:
16+
if char in magazine:
17+
magazine = magazine.replace(char, '', 1)
18+
else:
19+
return False
20+
return True
521
__________________________________________________________________________________________________

Python3/384.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
11
__________________________________________________________________________________________________
2+
sample 268 ms submission
3+
class Solution:
24

5+
def __init__(self, nums: List[int]):
6+
self.nums = nums
7+
8+
def reset(self) -> List[int]:
9+
"""
10+
Resets the array to its original configuration and return it.
11+
"""
12+
return self.nums
13+
14+
def shuffle(self) -> List[int]:
15+
"""
16+
Returns a random shuffling of the array.
17+
"""
18+
return sorted(self.nums, key = lambda x: random.random())
19+
20+
21+
22+
# Your Solution object will be instantiated and called as such:
23+
# obj = Solution(nums)
24+
# param_1 = obj.reset()
25+
# param_2 = obj.shuffle()
326
__________________________________________________________________________________________________
27+
sample 19028 kb submission
28+
class Solution:
29+
30+
def __init__(self, nums: List[int]):
31+
self.array = nums
32+
self.original = list(nums)
33+
34+
def reset(self) -> List[int]:
35+
"""
36+
Resets the array to its original configuration and return it.
37+
"""
38+
self.array = self.original
39+
self.original = list(self.original)
40+
return self.array
41+
42+
def shuffle(self) -> List[int]:
43+
"""
44+
Returns a random shuffling of the array.
45+
"""
46+
for i in range(len(self.array)):
47+
swap_idx = random.randrange(i, len(self.array))
48+
self.array[i], self.array[swap_idx] = self.array[swap_idx], self.array[i]
49+
return self.array
50+
451

52+
# Your Solution object will be instantiated and called as such:
53+
# obj = Solution(nums)
54+
# param_1 = obj.reset()
55+
# param_2 = obj.shuffle()
556
__________________________________________________________________________________________________

Python3/385.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,118 @@
11
__________________________________________________________________________________________________
2+
sample 32 ms submission
3+
# """
4+
# This is the interface that allows for creating nested lists.
5+
# You should not implement it, or speculate about its implementation
6+
# """
7+
#class NestedInteger:
8+
# def __init__(self, value=None):
9+
# """
10+
# If value is not specified, initializes an empty list.
11+
# Otherwise initializes a single integer equal to value.
12+
# """
213

14+
# def isInteger(self):
15+
# """
16+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
17+
# :rtype bool
18+
# """
19+
20+
# def add(self, elem):
21+
# """
22+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
23+
# :rtype void
24+
# """
25+
26+
# def setInteger(self, value):
27+
# """
28+
# Set this NestedInteger to hold a single integer equal to value.
29+
# :rtype void
30+
# """
31+
32+
# def getInteger(self):
33+
# """
34+
# @return the single integer that this NestedInteger holds, if it holds a single integer
35+
# Return None if this NestedInteger holds a nested list
36+
# :rtype int
37+
# """
38+
39+
# def getList(self):
40+
# """
41+
# @return the nested list that this NestedInteger holds, if it holds a nested list
42+
# Return None if this NestedInteger holds a single integer
43+
# :rtype List[NestedInteger]
44+
# """
45+
46+
class Solution:
47+
def deserialize(self, s: str) -> NestedInteger:
48+
return NestedInteger(s)
349
__________________________________________________________________________________________________
50+
sample 16488 kb submission
51+
# """
52+
# This is the interface that allows for creating nested lists.
53+
# You should not implement it, or speculate about its implementation
54+
# """
55+
#class NestedInteger:
56+
# def __init__(self, value=None):
57+
# """
58+
# If value is not specified, initializes an empty list.
59+
# Otherwise initializes a single integer equal to value.
60+
# """
61+
#
62+
# def isInteger(self):
63+
# """
64+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
65+
# :rtype bool
66+
# """
67+
#
68+
# def add(self, elem):
69+
# """
70+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
71+
# :rtype void
72+
# """
73+
#
74+
# def setInteger(self, value):
75+
# """
76+
# Set this NestedInteger to hold a single integer equal to value.
77+
# :rtype void
78+
# """
79+
#
80+
# def getInteger(self):
81+
# """
82+
# @return the single integer that this NestedInteger holds, if it holds a single integer
83+
# Return None if this NestedInteger holds a nested list
84+
# :rtype int
85+
# """
86+
#
87+
# def getList(self):
88+
# """
89+
# @return the nested list that this NestedInteger holds, if it holds a nested list
90+
# Return None if this NestedInteger holds a single integer
91+
# :rtype List[NestedInteger]
92+
# """
93+
94+
class Solution:
95+
def deserialize(self, s: str) -> NestedInteger:
96+
nestedArray = [NestedInteger()]
97+
startIndex = None
98+
for idx, char in enumerate(s):
99+
if char == '[':
100+
nestedArray.append(NestedInteger())
101+
elif char == ']':
102+
if startIndex != None:
103+
nestedArray[-1].add(NestedInteger(value=int(s[startIndex:idx])))
104+
startIndex = None
105+
nestedArray[-2].add(nestedArray.pop())
106+
elif char == ',':
107+
if startIndex != None:
108+
nestedArray[-1].add(NestedInteger(value=int(s[startIndex:idx])))
109+
startIndex = None
110+
elif startIndex == None:
111+
startIndex = idx
112+
113+
if startIndex != None:
114+
nestedArray[-1].add(NestedInteger(value=int(s[startIndex:len(s)])))
4115

116+
117+
return nestedArray[0].getList()[0]
5118
__________________________________________________________________________________________________

Python3/386.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
__________________________________________________________________________________________________
2-
2+
sample 100 ms submission
3+
class Solution:
4+
def lexicalOrder(self, n: int) -> List[int]:
5+
return sorted(range(1, n + 1), key=str)
36
__________________________________________________________________________________________________
4-
7+
sample 18980 kb submission
8+
class Solution:
9+
def lexicalOrder(self, n: int) -> List[int]:
10+
res = [0] * n
11+
cur = 1
12+
for i in range(n):
13+
res[i] = cur
14+
if cur*10 <= n:
15+
cur *= 10
16+
else:
17+
if cur >= n:
18+
cur //= 10
19+
cur += 1
20+
while cur % 10 == 0:
21+
cur //= 10
22+
return res
523
__________________________________________________________________________________________________

Python3/387.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
__________________________________________________________________________________________________
2-
2+
sample 32 ms submission
3+
class Solution:
4+
def firstUniqChar(self, s: str) -> int:
5+
uniq = []
6+
first = len(s)
7+
for letter in "abcdefghijklmnopqrstuvwxyz":
8+
pos = s.find(letter)
9+
rep = s.rfind(letter)
10+
if pos == rep and pos >= 0:
11+
if pos < first:
12+
first = pos
13+
return first if first < len(s) else -1
314
__________________________________________________________________________________________________
15+
sample 13044 kb submission
16+
class Solution:
17+
def firstUniqChar(self, s: str) -> int:
18+
from collections import Counter
19+
c = Counter(s)
420

21+
for i in range(len(s)):
22+
if c[s[i]] == 1:
23+
return i
24+
25+
return -1
526
__________________________________________________________________________________________________

0 commit comments

Comments
 (0)