LeetCode刷题笔记(2022-05) LeetCode刷题笔记(2022-06) LeetCode刷题笔记(2022-07,08) LeetCode刷题笔记(2022-09) LeetCode刷题笔记(2022-11) [345] Reverse Vowels of a String Time: 2022-11-04
Difficulty: Easy (47.72%)
Tags: two-pointers | string
我的解法,比较慢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 public class MySolution { private static readonly char [] vowels = { 'a' , 'e' , 'i' , 'o' , 'u' }; private bool isOfVowels (char ch ) { var res = false ; vowels .ToList() .ForEach( (c) => { if (char .ToLower(ch) == c) { res = true ; } } ); return res; } public string ReverseVowels (string s ) { var vowelsPoses = new List<int >(); for (int i = 0 ; i < s.Length; i++) { if (isOfVowels(s[i])) { vowelsPoses.Add(i); } } var sBuilder = new StringBuilder(s); for (int i = 0 ; i < vowelsPoses.Count / 2 ; i++) { (sBuilder[vowelsPoses[i]], sBuilder[vowelsPoses[vowelsPoses.Count - 1 - i]]) = ( sBuilder[vowelsPoses[vowelsPoses.Count - 1 - i]], sBuilder[vowelsPoses[i]] ); } return sBuilder.ToString(); } }
大佬解法,速度提升了 90ms,内存减少了 2m,复杂度是相同的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Solution { private static readonly ISet<char > vowels = new HashSet<char >(new char [] { 'a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' }); public string ReverseVowels (string s ) { var chars = s.ToCharArray(); var start = 0 ; var end = s.Length - 1 ; while (end > start) { while (start < end && !vowels.Contains(chars[start])) { start++; } while (start < end && !vowels.Contains(chars[end])) { end--; } (chars[start], chars[end]) = (chars[end], chars[start]); start++; end--; } return new string (chars); } }
[212] Word Search II Time: 2022-11-05
Difficulty: Hard (36.89%)
Tags: backtracking | trie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 public class Solution { private class TrieNode { public TrieNode[] next = new TrieNode[26 ]; public string ? word; } private TrieNode buildNode (string [] words ) { var root = new TrieNode(); foreach (var word in words) { var p = root; foreach (var ch in word) { int i = ch - 'a' ; if (p.next[i] is null ) { p.next[i] = new TrieNode(); } p = p.next[i]; } p.word = word; } return root; } private void dfs (char [][] board, int i, int j, TrieNode p, IList<string > res ) { var c = board[i][j]; if (c == '#' || p.next[c - 'a' ] is null ) { return ; } p = p.next[c - 'a' ]; if (p.word is not null ) { res.Add(p.word); p.word = null ; } board[i][j] = '#' ; if (i > 0 ) dfs(board, i - 1 , j, p, res); if (j > 0 ) dfs(board, i, j - 1 , p, res); if (i < board.Length - 1 ) dfs(board, i + 1 , j, p, res); if (j < board[0 ].Length - 1 ) dfs(board, i, j + 1 , p, res); board[i][j] = c; } public IList<string > FindWords (char [][] board, string [] words ) { var res = new List<string >(); var root = buildNode(words); for (int i = 0 ; i < board.Length; i++) { for (int j = 0 ; j < board[0 ].Length; j++) { dfs(board, i, j, root, res); } } return res; } }
[899] Orderly Queue Time: 2022-11-06
Difficulty: Hard (58.96%)
Tags: math
一根死脑筋的我,直接超时了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class MySolution { private ISet<string > list = new SortedSet<string >(); private void dfs (StringBuilder sb, int i, ref int k ) { sb.Append(sb[i]); sb.Remove(i, 1 ); if (list.Contains(sb.ToString())) { return ; } list.Add(sb.ToString()); for (int j = 0 ; j < k; j++) { dfs(new StringBuilder(sb.ToString()), j, ref k); } } public string OrderlyQueue (string s, int k ) { list.Add(s); for (int i = 0 ; i < k; i++) { dfs(new StringBuilder(s), i, ref k); } return list.First(); } }
具体看注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class Solution { public string OrderlyQueue (string s, int k ) { if (k > 1 ) { var chars = s.ToCharArray(); Array.Sort(chars); return new string (chars); } string res = s; for (int i = 1 ; i < s.Length; i++) { string tmp = string .Concat(s.AsSpan(i), s.AsSpan(0 , i)); if (res.CompareTo(tmp) > 0 ) res = tmp; } return res; } }
[1323] Maximum 69 Number Time: 2022-11-07
Difficulty: Easy (79.14%)
Tags: Unknown
弱智题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Solution { public int Maximum69Number (int num ) { var str = new StringBuilder(num.ToString()); for (int i = 0 ; i < str.Length; i++) { if (str[i] == '6' ) { str[i] = '9' ; break ; } } return int .Parse(str.ToString()); } }
[1544] Make The String Great Time: 2022-11-08
Difficulty: Easy (57.11%)
Tags: Unknown
我的办法,嗯遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class MySolution { public string MakeGood (string s ) { var sb = new StringBuilder(s); for (int i = 0 ; i < sb.Length - 1 ; ) { if (char .ToLower(sb[i]) == char .ToLower(sb[i + 1 ]) && sb[i] != sb[i + 1 ]) { sb.Remove(i, 2 ); if (i != 0 ) { i--; } } else { i++; } } return sb.ToString(); } }
别人的办法,用栈,复杂度差不多,速度的提升感觉是波动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class Solution { public string MakeGood (string s ) { var st = new Stack<char >(); for (int i = 0 ; i < s.Length; i++) { if (st.Count > 0 && char .ToLower(st.Peek()) == char .ToLower(s[i]) && st.Peek() != s[i]) { st.Pop(); } else { st.Push(s[i]); } } var charArr = st.ToList().ToArray(); Array.Reverse(charArr); return new string (charArr); } }
[901] Online Stock Span Time: 2022-11-09
Difficulty: Medium (63.90%)
Tags: array | greedy
死方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class SimpleStockSpanner { private IList<int > prices = new List<int >(); public SimpleStockSpanner () { } public int Next (int price ) { var res = 1 ; for (int i = prices.Count - 1 ; i >= 0 ; i--) { if (price >= prices[i]) { res++; } else { break ; } } prices.Add(price); return res; } }
大佬的方法,我也想把前面每一步结果存储起来做优化来着,但没写出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public class StockSpanner { private Stack<KeyValuePair<int , int >> stack = new Stack<KeyValuePair<int , int >>(); public StockSpanner () { } public int Next (int price ) { int res = 1 ; while (stack.Count > 0 && stack.Peek().Key <= price) { res += stack.Pop().Value; } stack.Push(new KeyValuePair<int , int >(price, res)); return res; } }
[1047] Remove All Adjacent Duplicates In String Time: 2022-11-10
Difficulty: Easy (70.47%)
Tags: greedy
陈年老题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 { public string RemoveDuplicates (string s ) { var st = new Stack<char >(); foreach (var ch in s) { if (st.Count > 0 && st.Peek() == ch) { st.Pop(); } else { st.Push(ch); } } var chArr = st.ToArray(); Array.Reverse(chArr); return new string (chArr); } }
[26] Remove Duplicates from Sorted Array Time: 2022-11-11
Difficulty: Easy (50.35%)
Tags: array | two-pointers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class Solution { public int RemoveDuplicates (int [] nums ) { var i = 0 ; var now = nums[0 ]; var res = 1 ; for (int j = 0 ; j < nums.Length; j++) { if (nums[j] != now) { res++; nums[++i] = nums[j]; now = nums[j]; } } return res; } }
Time: 2022-11-12
Difficulty: Hard (51.09%)
Tags: heap | design
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class MedianFinder { private PriorityQueue<int , int > small = new PriorityQueue<int , int >(); private PriorityQueue<int , int > large = new PriorityQueue<int , int >(); private bool even = true ; public MedianFinder () { } public void AddNum (int num ) { if (even) { small.Enqueue(num, -num); large.Enqueue(small.Peek(), small.Dequeue()); } else { large.Enqueue(num, num); small.Enqueue(large.Peek(), -large.Dequeue()); } even = !even; } public double FindMedian () { if (even) return (small.Peek() + large.Peek()) / 2.0 ; else return large.Peek(); } }
[151] Reverse Words in a String Time: 2022-11-13
Difficulty: Medium (30.30%)
Tags: string
普通做法,api 调用大师
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class MySolution { public string ReverseWords (string s ) { var strArr = new List<string >(); Array.ForEach( s.Split(" " ), str => { if (str != "" ) { strArr.Add(str); } } ); strArr.Reverse(); return string .Join(" " , strArr); } }
附加题:If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?
自己没想出来,抄的大佬题解,感觉现在脑子都不怎么会变通了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 public class Solution { private void reverse (char [] a, int i, int j ) { while (i < j) { var t = a[i]; a[i++] = a[j]; a[j--] = t; } } private void reverseWords (char [] a, int n ) { var i = 0 ; var j = 0 ; while (i < n) { while (i < j || i < n && a[i] == ' ' ) i++; while (j < i || j < n && a[j] != ' ' ) j++; reverse(a, i, j - 1 ); } } private string cleanSpaces (char [] a, int n ) { var i = 0 ; var j = 0 ; while (j < n) { while (j < n && a[j] == ' ' ) j++; while (j < n && a[j] != ' ' ) a[i++] = a[j++]; while (j < n && a[j] == ' ' ) j++; if (j < n) a[i++] = ' ' ; } return new string (a)[..i]; } public string ReverseWords (string s ) { if (s is null ) return null ; var a = s.ToCharArray(); var n = a.Length; reverse(a, 0 , n - 1 ); reverseWords(a, n); return cleanSpaces(a, n); } }
[947] Most Stones Removed with Same Row or Column Time: 2022-11-14
Difficulty: Medium (57.10%)
Tags: binary-search
并查集 并查集在底层实现可以是数组,也可以是散列表,不管使用什么底层实现,关键在于能表示一个对应关系,即:key 或下标表示了节点本身,而 value 表示顶点的父亲节点,初始化时指向自己。
并查集两个基本 操作:合并 & 查询
合并操作 合并操作就是将一个节点的的父节点指向另一个的根节点 查询操作 查询操作就是查询节点的根节点,如果两个节点的根节点相同,那么表示在一个集合中(相连)。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public class Solution { private IDictionary<int , int > f = new Dictionary<int , int >(); private int islands = 0 ; public int RemoveStones (int [][] stones ) { for (int i = 0 ; i < stones.Length; ++i) union(stones[i][0 ], ~stones[i][1 ]); return stones.Length - islands; } private int find (int x ) { if (!f.ContainsKey(x) && (f[x] = x) == x) islands++; if (x != f[x]) f[x] = find(f[x]); return f[x]; } private void union (int x, int y ) { x = find(x); y = find(y); if (x != y) { f[x] = y; islands--; } } }
[222] Count Complete Tree Nodes Time: 2022-11-15
Difficulty: Medium (57.47%)
Tags: binary-search | tree
就嗯剪枝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class Solution { private int deepth = 0 ; private int dfs (TreeNode node, int nowDeep, int pos ) { if (node is { right: TreeNode, left: TreeNode }) { var res = -1 ; if ((res = dfs(node.right, nowDeep + 1 , pos * 2 )) > 0 ) return res; if ((res = dfs(node.left, nowDeep + 1 , pos * 2 - 1 )) > 0 ) return res; } else if (node is { left: TreeNode }) { deepth = nowDeep + 1 ; return pos * 2 - 1 ; } else { if (deepth == 0 ) { deepth = nowDeep; } if (nowDeep > deepth) { deepth++; return pos; } } return -1 ; } public int CountNodes (TreeNode root ) { if (root is null ) return 0 ; var pos = dfs(root, 1 , 1 ); return pos < 0 ? (1 << deepth) - 1 : pos + (1 << (deepth - 1 )) - 1 ; } }
[374] Guess Number Higher or Lower Time: 2022-11-16
Difficulty: Easy (50.45%)
Tags: binary-search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class Solution : GuessGame { public int GuessNumber (int n ) { int guessV = n / 2 ; int guessR; int min = 0 ; int max = n; while ((guessR = guess(guessV)) != 0 ) { if (guessR == 1 ) { min = guessV; guessV = guessV / 2 + max / 2 ; if (guessV == min) { guessV++; } } else { max = guessV; guessV = guessV / 2 + min / 2 ; if (guessV == max) { guessV--; } } } return guessV; } }
[223] Rectangle Area Time: 2022-11-17
Difficulty: Medium (40.85%)
Tags: math
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class Solution { private int getRectangleArea (int x1, int y1, int x2, int y2 ) { return (y2 - y1) * (x2 - x1); } public int ComputeArea (int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2 ) { var aS = getRectangleArea(ax1, ay1, ax2, ay2); var bS = getRectangleArea(bx1, by1, bx2, by2); if (ax2 <= bx1 || ax1 >= bx2 || ay1 >= by2 || ay2 <= by1) { return aS + bS; } return aS + bS - getRectangleArea(Math.Max(ax1, bx1), Math.Max(ay1, by1), Math.Min(ax2, bx2), Math.Min(ay2, by2)); } }
[263] Ugly Number Time: 2022-11-18
Difficulty: Easy (41.64%)
Tags: math
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class Solution { public bool IsUgly (int n ) { if (n == 0 ) return false ; while (n % 2 == 0 ) { n /= 2 ; } while (n % 3 == 0 ) { n /= 3 ; } while (n % 5 == 0 ) { n /= 5 ; } if (n == 1 ) return true ; return false ; } }
[587] Erect the Fence Time: 2022-11-19
Difficulty: Hard (43.14%)
Tags: geometry
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class Solution { private int cmp (int [] p1, int [] p2, int [] p3 ) { return (p3[1 ] - p2[1 ]) * (p2[0 ] - p1[0 ]) - (p2[1 ] - p1[1 ]) * (p3[0 ] - p2[0 ]); } public int [][] OuterTrees(int [][] trees) { if (trees.Length <= 3 ) return trees; Array.Sort(trees, (a, b) => { if (a[0 ] == b[0 ]) return a[1 ] - b[1 ]; else return a[0 ] - b[0 ]; }); var lower = new List<int []>(); var upper = new List<int []>(); foreach (var tree in trees) { while (lower.Count >= 2 && cmp(lower[^2 ], lower[^1 ], tree) < 0 ) lower.RemoveAt(lower.Count - 1 ); while (upper.Count >= 2 && cmp(upper[^2 ], upper[^1 ], tree) > 0 ) upper.RemoveAt(upper.Count - 1 ); lower.Add(tree); upper.Add(tree); } var set = new HashSet<int []>(lower.Concat(upper)); return set .ToArray(); } }
[224] Basic Calculator Time: 2022-11-20
Difficulty: Hard (41.16%)
Tags: math | stack
一开始照本宣科搞了两个栈,但其实没有必要,两个栈的作用是判断表达式是否合法,但是题目已经保证了表达式合法,所以可以省略这一步。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 public class Solution { public int Calculate (string s ) { var stack = new Stack<int >(); var result = 0 ; var number = 0 ; var sign = 1 ; for (int i = 0 ; i < s.Length; i++) { char c = s[i]; if (char .IsDigit(c)) { number = 10 * number + (c - '0' ); } else if (c == '+' ) { result += sign * number; number = 0 ; sign = 1 ; } else if (c == '-' ) { result += sign * number; number = 0 ; sign = -1 ; } else if (c == '(' ) { stack.Push(result); stack.Push(sign); sign = 1 ; result = 0 ; } else if (c == ')' ) { result += sign * number; number = 0 ; result *= stack.Pop(); result += stack.Pop(); } } if (number != 0 ) result += sign * number; return result; } }
[1926] Nearest Exit from Entrance in Maze Time: 2022-11-21
Difficulty: Medium (43.12%)
Tags: Unknown
广搜
事实证明,语言和代码风格也是会影响编程水平的,这思路我一早想出来,但 res++因为屎一般的大括号嵌套写错了地方,竟然一直都没有看出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 public class Solution { private static readonly int [][] direct = new int [][] { new int [] { 0 , 1 }, new int [] { 1 , 0 }, new int [] { 0 , -1 }, new int [] { -1 , 0 } }; public int NearestExit (char [][] maze, int [] entrance ) { var rows = maze.Length; var cols = maze[0 ].Length; var bfs = new Queue<int []>(); var res = 1 ; bfs.Enqueue(entrance); maze[entrance[0 ]][entrance[1 ]] = '+' ; while (bfs.Count > 0 ) { var size = bfs.Count; for (int i = 0 ; i < size; i++) { var item = bfs.Dequeue(); for (int j = 0 ; j < 4 ; j++) { var newX = item[0 ] + direct[j][0 ]; var newY = item[1 ] + direct[j][1 ]; if (newX < 0 || newY < 0 || newX >= rows || newY >= cols || maze[newX][newY] == '+' ) continue ; else if (newX == 0 || newX == rows - 1 || newY == 0 || newY == cols - 1 ) return res; else { bfs.Enqueue(new int [] { newX, newY }); maze[newX][newY] = '+' ; } } } res++; } return -1 ; } }
[279] Perfect Squares Time: 2022-11-22
Difficulty: Medium (52.14%)
Tags: math | dynamic-programming | breadth-first-search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 public class Solution { private int getSquareNumLessThan (int n ) { return (int )Math.Sqrt(n); } public int NumSquares (int n ) { var bfs = new Queue<int >(); var res = 1 ; bfs.Enqueue(n); while (bfs.Count > 0 ) { var size = bfs.Count; for (int i = 0 ; i < size; i++) { var first = bfs.Dequeue(); var maxSquareNum = getSquareNumLessThan(first); for (int j = maxSquareNum; j >= 1 ; j--) if (first - j * j == 0 ) return res; else bfs.Enqueue(first - j * j); } res++; } return n; } }
[36] Valid Sudoku Time: 2022-11-23
Difficulty: Medium (56.81%)
Tags: hash-table
原来只要判定当前是否 valid 就行了,我还以为得要做出来呢 (= =!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class Solution { public bool IsValidSudoku (char [][] board ) { var seen = new HashSet<string >(); for (int i = 0 ; i < 9 ; ++i) { for (int j = 0 ; j < 9 ; ++j) { char number = board[i][j]; if (number != '.' ) if (!seen.Add($"{number} in row {i} " ) || !seen.Add($"{number} in column {j} " ) || !seen.Add($"{number} in block {i / 3 } -{j / 3 } " )) return false ; } } return true ; } }
[79] Word Search Time: 2022-11-25
Difficulty: Medium (39.79%)
Tags: array | backtracking
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 public class Solution { private static readonly int [][] direct = new int [][] { new int [] { -1 , 0 }, new int [] { 1 , 0 }, new int [] { 0 , 1 }, new int [] { 0 , -1 } }; private string word; private bool bfs (char [][] board, int x, int y, int index ) { if (index == word.Length - 1 ) { return true ; } var ch = board[x][y]; board[x][y] = ' ' ; index++; for (int i = 0 ; i < direct.Length; i++) { var newX = x + direct[i][0 ]; var newY = y + direct[i][1 ]; if (newX < 0 || newY < 0 || newX >= board.Length || newY >= board[0 ].Length) continue ; if (board[newX][newY] == word[index]) { if (bfs(board, newX, newY, index)) { return true ; } } } board[x][y] = ch; return false ; } public bool Exist (char [][] board, string word ) { this .word = word; for (int i = 0 ; i < board.Length; i++) { for (int j = 0 ; j < board[0 ].Length; j++) { if (board[i][j] == word[0 ]) { if (bfs(board, i, j, 0 )) { return true ; } } } } return false ; } }
[2225] Find Players With Zero or One Losses Time: 2022-11-28
Difficulty: Medium (68.97%)
Tags: Unknown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 public class Solution { public IList<IList<int >> FindWinners(int [][] matches) { var lost = new Dictionary<int , int >(); Array.ForEach(matches, match => { if (lost.ContainsKey(match[1 ])) lost[match[1 ]]++; else lost[match[1 ]] = 1 ; if (!lost.ContainsKey(match[0 ])) lost[match[0 ]] = 0 ; } ); var lost0 = new List<int >(); var lost1 = new List<int >(); foreach (var kv in lost) { switch (kv.Value) { case 0 : lost0.Add(kv.Key); break ; case 1 : lost1.Add(kv.Key); break ; default : continue ; } } lost0.Sort(); lost1.Sort(); return new List<IList<int >> { lost0, lost1 }; } }
[380] Insert Delete GetRandom O(1) Time: 2022-11-29
Difficulty: Medium (52.04%)
Tags: array | hash-table | design
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public class RandomizedSet { private List<int > nums = new (); private Dictionary<int , int > locs = new (); private Random random = new (); public RandomizedSet () { } public bool Insert (int val ) { if (locs.ContainsKey(val)) return false ; locs[val] = nums.Count; nums.Add(val); return true ; } public bool Remove (int val ) { if (!locs.ContainsKey(val)) return false ; var loc = locs[val]; if (loc < nums.Count - 1 ) { nums[loc] = nums[^1 ]; locs[nums[loc]] = loc; } nums.RemoveAt(nums.Count - 1 ); locs.Remove(val); return true ; } public int GetRandom () { return nums[random.Next(nums.Count)]; } }