Skip to content

Commit

Permalink
400
Browse files Browse the repository at this point in the history
400
  • Loading branch information
strengthen committed Aug 15, 2019
1 parent e6ef18d commit 3e4a6bf
Show file tree
Hide file tree
Showing 19 changed files with 878 additions and 26 deletions.
80 changes: 80 additions & 0 deletions Python3/382.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,85 @@
__________________________________________________________________________________________________
sample 72 ms submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
import random


class Solution:

def __init__(self, head: ListNode):
self.head = head
self.count = self.to_count()
"""
@param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node.
"""


def getRandom(self) -> int:
node = self.head
p = random.random()//(1/self.count)
for i in range(int(p)):
node = node.next
return node.val
"""
Returns a random node's value.
"""

def to_count(self):
node = self.head
count = 0
while node is not None:
count += 1
node = node.next
return count

# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()
__________________________________________________________________________________________________
sample 15976 kb submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

from random import random

class Solution:

def __init__(self, head: ListNode):
"""
@param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node.
"""
self.head = head
pass

def getRandom(self) -> int:
"""
Returns a random node's value.
"""
max_p = -1
cur = self.head
res = 0
while cur:
cur_p = random()
if cur_p > max_p:
res = cur.val
max_p = cur_p
cur = cur.next

return res




# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()
__________________________________________________________________________________________________
20 changes: 18 additions & 2 deletions Python3/383.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
__________________________________________________________________________________________________

sample 28 ms submission
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:

r = set(ransomNote)
for i in r:
if ransomNote.count(i) > magazine.count(i):
return False
return True
__________________________________________________________________________________________________

sample 13260 kb submission
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
for char in ransomNote:
if char in magazine:
magazine = magazine.replace(char, '', 1)
else:
return False
return True
__________________________________________________________________________________________________
51 changes: 51 additions & 0 deletions Python3/384.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
__________________________________________________________________________________________________
sample 268 ms submission
class Solution:

def __init__(self, nums: List[int]):
self.nums = nums

def reset(self) -> List[int]:
"""
Resets the array to its original configuration and return it.
"""
return self.nums

def shuffle(self) -> List[int]:
"""
Returns a random shuffling of the array.
"""
return sorted(self.nums, key = lambda x: random.random())



# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
__________________________________________________________________________________________________
sample 19028 kb submission
class Solution:

def __init__(self, nums: List[int]):
self.array = nums
self.original = list(nums)

def reset(self) -> List[int]:
"""
Resets the array to its original configuration and return it.
"""
self.array = self.original
self.original = list(self.original)
return self.array

def shuffle(self) -> List[int]:
"""
Returns a random shuffling of the array.
"""
for i in range(len(self.array)):
swap_idx = random.randrange(i, len(self.array))
self.array[i], self.array[swap_idx] = self.array[swap_idx], self.array[i]
return self.array


# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
__________________________________________________________________________________________________
113 changes: 113 additions & 0 deletions Python3/385.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,118 @@
__________________________________________________________________________________________________
sample 32 ms submission
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger:
# def __init__(self, value=None):
# """
# If value is not specified, initializes an empty list.
# Otherwise initializes a single integer equal to value.
# """

# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """

# def add(self, elem):
# """
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
# :rtype void
# """

# def setInteger(self, value):
# """
# Set this NestedInteger to hold a single integer equal to value.
# :rtype void
# """

# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """

# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """

class Solution:
def deserialize(self, s: str) -> NestedInteger:
return NestedInteger(s)
__________________________________________________________________________________________________
sample 16488 kb submission
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger:
# def __init__(self, value=None):
# """
# If value is not specified, initializes an empty list.
# Otherwise initializes a single integer equal to value.
# """
#
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def add(self, elem):
# """
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
# :rtype void
# """
#
# def setInteger(self, value):
# """
# Set this NestedInteger to hold a single integer equal to value.
# :rtype void
# """
#
# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """
#
# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """

class Solution:
def deserialize(self, s: str) -> NestedInteger:
nestedArray = [NestedInteger()]
startIndex = None
for idx, char in enumerate(s):
if char == '[':
nestedArray.append(NestedInteger())
elif char == ']':
if startIndex != None:
nestedArray[-1].add(NestedInteger(value=int(s[startIndex:idx])))
startIndex = None
nestedArray[-2].add(nestedArray.pop())
elif char == ',':
if startIndex != None:
nestedArray[-1].add(NestedInteger(value=int(s[startIndex:idx])))
startIndex = None
elif startIndex == None:
startIndex = idx

if startIndex != None:
nestedArray[-1].add(NestedInteger(value=int(s[startIndex:len(s)])))


return nestedArray[0].getList()[0]
__________________________________________________________________________________________________
22 changes: 20 additions & 2 deletions Python3/386.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
__________________________________________________________________________________________________

sample 100 ms submission
class Solution:
def lexicalOrder(self, n: int) -> List[int]:
return sorted(range(1, n + 1), key=str)
__________________________________________________________________________________________________

sample 18980 kb submission
class Solution:
def lexicalOrder(self, n: int) -> List[int]:
res = [0] * n
cur = 1
for i in range(n):
res[i] = cur
if cur*10 <= n:
cur *= 10
else:
if cur >= n:
cur //= 10
cur += 1
while cur % 10 == 0:
cur //= 10
return res
__________________________________________________________________________________________________
23 changes: 22 additions & 1 deletion Python3/387.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
__________________________________________________________________________________________________

sample 32 ms submission
class Solution:
def firstUniqChar(self, s: str) -> int:
uniq = []
first = len(s)
for letter in "abcdefghijklmnopqrstuvwxyz":
pos = s.find(letter)
rep = s.rfind(letter)
if pos == rep and pos >= 0:
if pos < first:
first = pos
return first if first < len(s) else -1
__________________________________________________________________________________________________
sample 13044 kb submission
class Solution:
def firstUniqChar(self, s: str) -> int:
from collections import Counter
c = Counter(s)

for i in range(len(s)):
if c[s[i]] == 1:
return i

return -1
__________________________________________________________________________________________________
Loading

0 comments on commit 3e4a6bf

Please sign in to comment.