diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index c3ff9d53..eaf0a719 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -402,6 +402,38 @@ bool isValid(char * s){ ``` +C#: +```csharp +public class Solution { + public bool IsValid(string s) { + var len = s.Length; + if(len % 2 == 1) return false; // 字符串长度为单数,直接返回 false + // 初始化栈 + var stack = new Stack(); + // 遍历字符串 + for(int i = 0; i < len; i++){ + // 当字符串为左括号时,进栈对应的右括号 + if(s[i] == '('){ + stack.Push(')'); + }else if(s[i] == '['){ + stack.Push(']'); + }else if(s[i] == '{'){ + stack.Push('}'); + } + // 当字符串为右括号时,当栈为空(无左括号) 或者 出栈字符不是当前的字符 + else if(stack.Count == 0 || stack.Pop() != s[i]) + return false; + } + // 如果栈不为空,例如“((()”,右括号少于左括号,返回false + if (stack.Count > 0) + return false; + // 上面的校验都满足,则返回true + else + return true; + } +} +``` + PHP: ```php // https://www.php.net/manual/zh/class.splstack.php diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index 1a90265a..21bd8593 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -326,6 +326,40 @@ func evalRPN(_ tokens: [String]) -> Int { } ``` +C#: +```csharp +public int EvalRPN(string[] tokens) { + int num; + Stack stack = new Stack(); + foreach(string s in tokens){ + if(int.TryParse(s, out num)){ + stack.Push(num); + }else{ + int num1 = stack.Pop(); + int num2 = stack.Pop(); + switch (s) + { + case "+": + stack.Push(num1 + num2); + break; + case "-": + stack.Push(num2 - num1); + break; + case "*": + stack.Push(num1 * num2); + break; + case "/": + stack.Push(num2 / num1); + break; + default: + break; + } + } + } + return stack.Pop(); + } +``` + PHP: ```php diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 40415d8d..17c50cd9 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -900,6 +900,41 @@ class MyStack() { ``` +C#: +```csharp +public class MyStack { + Queue queue1; + Queue queue2; + public MyStack() { + queue1 = new Queue(); + queue2 = new Queue(); + } + + public void Push(int x) { + queue2.Enqueue(x); + while(queue1.Count != 0){ + queue2.Enqueue(queue1.Dequeue()); + } + Queue queueTemp; + queueTemp = queue1; + queue1 = queue2; + queue2 = queueTemp; + } + + public int Pop() { + return queue1.Count > 0 ? queue1.Dequeue() : -1; + } + + public int Top() { + return queue1.Count > 0 ? queue1.Peek() : -1; + } + + public bool Empty() { + return queue1.Count == 0; + } +} +``` + PHP > 双对列 ```php diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 4662e2f2..dcad6a79 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -497,6 +497,49 @@ void myQueueFree(MyQueue* obj) { ``` +C#: +```csharp +public class MyQueue { + Stack inStack; + Stack outStack; + + public MyQueue() { + inStack = new Stack();// 负责进栈 + outStack = new Stack();// 负责出栈 + } + + public void Push(int x) { + inStack.Push(x); + } + + public int Pop() { + dumpstackIn(); + return outStack.Pop(); + } + + public int Peek() { + dumpstackIn(); + return outStack.Peek(); + } + + public bool Empty() { + return inStack.Count == 0 && outStack.Count == 0; + } + + // 处理方法: + // 如果outStack为空,那么将inStack中的元素全部放到outStack中 + private void dumpstackIn(){ + if (outStack.Count != 0) return; + while(inStack.Count != 0){ + outStack.Push(inStack.Pop()); + } + } +} + +``` + + + PHP: ```php // SplStack 类通过使用一个双向链表来提供栈的主要功能。[PHP 5 >= 5.3.0, PHP 7, PHP 8] @@ -583,5 +626,6 @@ class MyQueue() { } ``` + -----------------------
diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index 7ee1fdb1..474b3a75 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -756,5 +756,46 @@ class MyQueue{ } ``` +C#: +```csharp +class myDequeue{ + private LinkedList linkedList = new LinkedList(); + + public void Enqueue(int n){ + while(linkedList.Count > 0 && linkedList.Last.Value < n){ + linkedList.RemoveLast(); + } + linkedList.AddLast(n); + } + + public int Max(){ + return linkedList.First.Value; + } + + public void Dequeue(int n){ + if(linkedList.First.Value == n){ + linkedList.RemoveFirst(); + } + } + } + + myDequeue window = new myDequeue(); + List res = new List(); + + public int[] MaxSlidingWindow(int[] nums, int k) { + for(int i = 0; i < k; i++){ + window.Enqueue(nums[i]); + } + res.Add(window.Max()); + for(int i = k; i < nums.Length; i++){ + window.Dequeue(nums[i-k]); + window.Enqueue(nums[i]); + res.Add(window.Max()); + } + + return res.ToArray(); + } +``` + -----------------------
diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 0c978b2a..430dcea1 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -371,6 +371,44 @@ function topKFrequent(nums: number[], k: number): number[] { }; ``` +C#: +```csharp + public int[] TopKFrequent(int[] nums, int k) { + //哈希表-标权重 + Dictionary dic = new(); + for(int i = 0; i < nums.Length; i++){ + if(dic.ContainsKey(nums[i])){ + dic[nums[i]]++; + }else{ + dic.Add(nums[i], 1); + } + } + //优先队列-从小到大排列 + PriorityQueue pq = new(); + foreach(var num in dic){ + pq.Enqueue(num.Key, num.Value); + if(pq.Count > k){ + pq.Dequeue(); + } + } + + // //Stack-倒置 + // Stack res = new(); + // while(pq.Count > 0){ + // res.Push(pq.Dequeue()); + // } + // return res.ToArray(); + + //数组倒装 + int[] res = new int[k]; + for(int i = k - 1; i >= 0; i--){ + res[i] = pq.Dequeue(); + } + return res; + } + +``` + Scala: 解法一: 优先级队列 @@ -413,6 +451,7 @@ object Solution { .map(_._1) } } + ``` ----------------------- diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 9691c75b..2c51bbb3 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -376,6 +376,25 @@ func removeDuplicates(_ s: String) -> String { ``` +C#: +```csharp +public string RemoveDuplicates(string s) { + //拿字符串直接作为栈,省去了栈还要转为字符串的操作 + StringBuilder res = new StringBuilder(); + + foreach(char c in s){ + if(res.Length > 0 && res[res.Length-1] == c){ + res.Remove(res.Length-1, 1); + }else{ + res.Append(c); + } + } + + return res.ToString(); + } +``` + + PHP: ```php class Solution {