From 8bc026fed035ee9c0f2f8b0452f7e0a5f5531281 Mon Sep 17 00:00:00 2001 From: UndeadSheep Date: Mon, 27 Jun 2022 15:28:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=A0=88=E4=B8=8E?= =?UTF-8?q?=E9=98=9F=E5=88=97=E9=83=A8=E5=88=86=E7=9A=84=20C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 31 ++++++++++++++ problems/0150.逆波兰表达式求值.md | 34 +++++++++++++++ problems/0225.用队列实现栈.md | 35 ++++++++++++++++ problems/0232.用栈实现队列.md | 41 +++++++++++++++++++ problems/0239.滑动窗口最大值.md | 41 +++++++++++++++++++ problems/0347.前K个高频元素.md | 35 ++++++++++++++++ ...除字符串中的所有相邻重复项.md | 17 ++++++++ 7 files changed, 234 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 7bb7f746..cdca70f9 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -401,5 +401,36 @@ 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; + } +} +``` -----------------------
diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index f4dad823..8dafa460 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -326,5 +326,39 @@ 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(); + } +``` + -----------------------
diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 3457c4b3..5d902fb1 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -816,5 +816,40 @@ 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; + } +} + +``` -----------------------
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 1a56d9f3..d58dc55f 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -496,5 +496,46 @@ 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()); + } + } +} + +``` + -----------------------
diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index f269450f..35ca1eed 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -631,5 +631,46 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { } ``` +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 1d6a358b..c570672f 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -374,7 +374,42 @@ 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; + } +``` -----------------------
diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 638c8f4e..9237acdb 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -375,5 +375,22 @@ 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(); + } +``` -----------------------