Design a data structure to support the following operations under the average time complexity O(1).
- insert(val): when the element val does not exist, insert the item into the collection.
- remove(val): remove the item from the collection when the element val exists.
- getRandom: returns a random entry from an existing collection. Each element should have the same probability to be returned.
map+set
class RandomizedSet(object): def __init__(self): self.element = set() self.size = 0 def insert(self, val): if val in self.element: return False self.element.add(val) self.size+=1 return True def remove(self, val): if val not in self.element: return False self.element.remove(val) self.size-=1 return True def getRandom(self): return list(self.element)[random.randint(0,self.size-1)]
map+list
class RandomizedSet(object): def __init__(self): self.element = [] self.map = {} self.size = 0 def insert(self, val): if self.map.has_key(val): return False self.element.append(val) self.map[val] = self.size self.size+=1 return True def remove(self, val): if not self.map.has_key(val) or self.size==0: return False #Exchange the last element to the location where val is located tail = self.element[self.size-1] self.element[self.map.get(val)] = tail self.map[tail] = self.map.get(val) #Delete last element self.element.pop() self.map.pop(val) self.size -=1 return True def getRandom(self): import random return self.element[random.randint(0,self.size-1)]
Scramble an array without repeating elements.
Example:
// Initializes the array with a number set of 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return the result. Any permutation of [1,2,3] should return the same probability. solution.shuffle(); // Reset the array to its initial state [1,2,3]. solution.reset(); // Randomly returns the result after the array [1,2,3] is scrambled. solution.shuffle();
class Solution(object): def __init__(self, nums): self.origin = nums def reset(self): return self.origin def shuffle(self): new_list = self.origin[:] k = len(new_list) import random for i in range(k,1,-1): # Exchange the number on the [0..i-1] range with the first number without shuffle each time t = random.randint(0,i-1) tmp = new_list[t] new_list[t] = new_list[i-1] new_list[i-1] = tmp return new_list
Given a string, find its first distinct character and return its index. If not, returns - 1.
Case study:
s = "leetcode"
Return to 0.
s = "loveleetcode",
Return to 2.
Note: you can assume that the string contains only lowercase letters.
class Solution(object): def firstUniqChar(self, s): map = {} for i in range(len(s)): map[s[i]] = map.get(s[i],0)+1 for i in range(len(s)): if map[s[i]] == 1: return i return -1
Write a program to output a string representation of numbers from 1 to n.
-
If n is a multiple of 3, output "Fizz";
-
If n is a multiple of 5, output "Buzz";
-
If n is a multiple of 3 and 5 at the same time, output FizzBuzz.
class Solution(object): def fizzBuzz(self, n): res = ['1','2'] for i in range(3,n+1): if i%15==0: res.append('FizzBuzz') elif i%3==0: res.append('Fizz') elif i%5==0: res.append('Buzz') else: res.append(str(i)) return res[:n]
Given four array lists a, B, C, D containing integers, how many tuples (i, j, k, l) are calculated so that A[i] + B[j] + C[k] + D[l] = 0.
Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
Output:
2
Interpretation:
Two tuples are as follows:
- (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
- (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
class Solution(object): def fourSumCount(self, A, B, C, D): map = {} for i in A: for j in C: s = i+j map[s] = map.get(s,0)+1 res = 0 for i in B: for j in D: s = -i-j res += map.get(s,0) return res