It is the empty string,
It can be written as AB (A concatenated with B), where A and B are valid strings, or It can be written as (A), where A is a valid string.
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
For example, if s = "()))", you can insert an opening parenthesis to be "(**(**)))" or a closing parenthesis to be "())**)**)".
Return the minimum number of moves required to makesvalid.
defminAddToMakeValid2(self, s: str) -> int: left, res = 0, 0 for char in s: if char == '(': left += 1 else: left -= 1 if left < 0: res += 1 left = 0 return res + abs(left)
Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range[low, high].
Example 1:
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15 Output: 32 Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
Example 2:
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10 Output: 23 Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
Constraints:
The number of nodes in the tree is in the range `[1, 2 104].
*1 <= Node.val <= 105*1 <= low <= high <= 105* AllNode.val` are unique.
Design a data structure that simulates an in-memory file system.
Implement the FileSystem class:
FileSystem() Initializes the object of the system.
List<String> ls(String path) If path is a file path, returns a list that only contains this file’s name.
If path is a directory path, returns the list of file and directory names in this directory.The answer should in lexicographic order. void mkdir(String path) Makes a new directory according to the given path. The given directory path does not exist. If the middle directories in the path do not exist, you should create them as well.
void addContentToFile(String filePath, String content) If filePath does not exist, creates that file containing given content.
If filePath already exists, appends the given content to original content. String readContentFromFile(String filePath) Returns the content in the file at filePath.
Constraints:1 <= path.length, filePath.length <= 100 path and filePath are absolute paths which begin with '/' and do not end with '/' except that the path is just "/".
You can assume that all directory names and file names only contain lowercase letters, and the same names will not exist in the same directory. You can assume that all operations will be passed valid parameters, and users will not attempt to retrieve file content or list a directory or file that does not exist.
1 <= content.length <= 50 * At most 300 calls will be made to ls, mkdir, addContentToFile, and readContentFromFile.
defls(self, path: str) -> List[str]: # req remember /a not /a/ if path == '/': returnsorted(list(self.root.children.keys()) + list(self.root.files.keys())) # remember
dirs = path[1:].split('/') it = self._ls(dirs[:-1]) if dirs[-1] in it.files: return [dirs[-1]] else: # return files if no dir, no mixed types in same dir returnsorted(list(it.children[dirs[-1]].children.keys()) + list(it.children[dirs[-1]].files.keys()))
def_ls(self, dirs): it = self.root for _dirin dirs: it = it.children[_dir] return it
def_mkdir(self, dirs): it = self.root for _dirin dirs: it = it.children[_dir] return it
classTrieNode:
def__init__(self): self.children = collections.defaultdict(TrieNode) self.files = collections.defaultdict(str) # use dict coz filename can't be duplicate and faster for lookup
Design a HashMap without using any built-in hash table libraries.
Implement the MyHashMap class:
MyHashMap() initializes the object with an empty map.
void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value. int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
Explanation MyHashMap myHashMap = new MyHashMap(); myHashMap.put(1, 1); // The map is now [[1,1]] myHashMap.put(2, 2); // The map is now [[1,1], [2,2]] myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]] myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]] myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value) myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]] myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]] myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]
Constraints:
0 <= key, value <= 10<sup>6</sup> At most 10<sup>4</sup> calls will be made to put, get, and remove.
题目大意:
设计HashMap
LL解题思路(推荐):
大学学到的方法,用Array实现,将key mod prime num找到index插入。难点在于冲突处理,这里用chaining方法,也就是LL