Skip to content

Commit 8bc026f

Browse files
committed
新增 栈与队列部分的 C#版
1 parent 8a562b7 commit 8bc026f

7 files changed

+235
-1
lines changed

problems/0020.有效的括号.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,5 +401,36 @@ bool isValid(char * s){
401401
}
402402
```
403403
404+
C#:
405+
```csharp
406+
public class Solution {
407+
public bool IsValid(string s) {
408+
var len = s.Length;
409+
if(len % 2 == 1) return false; // 字符串长度为单数,直接返回 false
410+
// 初始化栈
411+
var stack = new Stack<char>();
412+
// 遍历字符串
413+
for(int i = 0; i < len; i++){
414+
// 当字符串为左括号时,进栈对应的右括号
415+
if(s[i] == '('){
416+
stack.Push(')');
417+
}else if(s[i] == '['){
418+
stack.Push(']');
419+
}else if(s[i] == '{'){
420+
stack.Push('}');
421+
}
422+
// 当字符串为右括号时,当栈为空(无左括号) 或者 出栈字符不是当前的字符
423+
else if(stack.Count == 0 || stack.Pop() != s[i])
424+
return false;
425+
}
426+
// 如果栈不为空,例如“((()”,右括号少于左括号,返回false
427+
if (stack.Count > 0)
428+
return false;
429+
// 上面的校验都满足,则返回true
430+
else
431+
return true;
432+
}
433+
}
434+
```
404435
-----------------------
405436
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0150.逆波兰表达式求值.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,39 @@ func evalRPN(_ tokens: [String]) -> Int {
326326
}
327327
```
328328

329+
C#:
330+
```csharp
331+
public int EvalRPN(string[] tokens) {
332+
int num;
333+
Stack<int> stack = new Stack<int>();
334+
foreach(string s in tokens){
335+
if(int.TryParse(s, out num)){
336+
stack.Push(num);
337+
}else{
338+
int num1 = stack.Pop();
339+
int num2 = stack.Pop();
340+
switch (s)
341+
{
342+
case "+":
343+
stack.Push(num1 + num2);
344+
break;
345+
case "-":
346+
stack.Push(num2 - num1);
347+
break;
348+
case "*":
349+
stack.Push(num1 * num2);
350+
break;
351+
case "/":
352+
stack.Push(num2 / num1);
353+
break;
354+
default:
355+
break;
356+
}
357+
}
358+
}
359+
return stack.Pop();
360+
}
361+
```
362+
329363
-----------------------
330364
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0225.用队列实现栈.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,5 +816,40 @@ class MyStack {
816816
}
817817
```
818818

819+
C#:
820+
```csharp
821+
public class MyStack {
822+
Queue<int> queue1;
823+
Queue<int> queue2;
824+
public MyStack() {
825+
queue1 = new Queue<int>();
826+
queue2 = new Queue<int>();
827+
}
828+
829+
public void Push(int x) {
830+
queue2.Enqueue(x);
831+
while(queue1.Count != 0){
832+
queue2.Enqueue(queue1.Dequeue());
833+
}
834+
Queue<int> queueTemp;
835+
queueTemp = queue1;
836+
queue1 = queue2;
837+
queue2 = queueTemp;
838+
}
839+
840+
public int Pop() {
841+
return queue1.Count > 0 ? queue1.Dequeue() : -1;
842+
}
843+
844+
public int Top() {
845+
return queue1.Count > 0 ? queue1.Peek() : -1;
846+
}
847+
848+
public bool Empty() {
849+
return queue1.Count == 0;
850+
}
851+
}
852+
853+
```
819854
-----------------------
820855
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0232.用栈实现队列.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,5 +496,46 @@ void myQueueFree(MyQueue* obj) {
496496
}
497497
```
498498
499+
C#:
500+
```csharp
501+
public class MyQueue {
502+
Stack<int> inStack;
503+
Stack<int> outStack;
504+
505+
public MyQueue() {
506+
inStack = new Stack<int>();// 负责进栈
507+
outStack = new Stack<int>();// 负责出栈
508+
}
509+
510+
public void Push(int x) {
511+
inStack.Push(x);
512+
}
513+
514+
public int Pop() {
515+
dumpstackIn();
516+
return outStack.Pop();
517+
}
518+
519+
public int Peek() {
520+
dumpstackIn();
521+
return outStack.Peek();
522+
}
523+
524+
public bool Empty() {
525+
return inStack.Count == 0 && outStack.Count == 0;
526+
}
527+
528+
// 处理方法:
529+
// 如果outStack为空,那么将inStack中的元素全部放到outStack中
530+
private void dumpstackIn(){
531+
if (outStack.Count != 0) return;
532+
while(inStack.Count != 0){
533+
outStack.Push(inStack.Pop());
534+
}
535+
}
536+
}
537+
538+
```
539+
499540
-----------------------
500541
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0239.滑动窗口最大值.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,5 +631,46 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
631631
}
632632
```
633633

634+
C#:
635+
```csharp
636+
class myDequeue{
637+
private LinkedList<int> linkedList = new LinkedList<int>();
638+
639+
public void Enqueue(int n){
640+
while(linkedList.Count > 0 && linkedList.Last.Value < n){
641+
linkedList.RemoveLast();
642+
}
643+
linkedList.AddLast(n);
644+
}
645+
646+
public int Max(){
647+
return linkedList.First.Value;
648+
}
649+
650+
public void Dequeue(int n){
651+
if(linkedList.First.Value == n){
652+
linkedList.RemoveFirst();
653+
}
654+
}
655+
}
656+
657+
myDequeue window = new myDequeue();
658+
List<int> res = new List<int>();
659+
660+
public int[] MaxSlidingWindow(int[] nums, int k) {
661+
for(int i = 0; i < k; i++){
662+
window.Enqueue(nums[i]);
663+
}
664+
res.Add(window.Max());
665+
for(int i = k; i < nums.Length; i++){
666+
window.Dequeue(nums[i-k]);
667+
window.Enqueue(nums[i]);
668+
res.Add(window.Max());
669+
}
670+
671+
return res.ToArray();
672+
}
673+
```
674+
634675
-----------------------
635676
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0347.前K个高频元素.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,42 @@ function topKFrequent(nums: number[], k: number): number[] {
374374
};
375375
```
376376

377-
377+
C#:
378+
```csharp
379+
public int[] TopKFrequent(int[] nums, int k) {
380+
//哈希表-标权重
381+
Dictionary<int,int> dic = new();
382+
for(int i = 0; i < nums.Length; i++){
383+
if(dic.ContainsKey(nums[i])){
384+
dic[nums[i]]++;
385+
}else{
386+
dic.Add(nums[i], 1);
387+
}
388+
}
389+
//优先队列-从小到大排列
390+
PriorityQueue<int,int> pq = new();
391+
foreach(var num in dic){
392+
pq.Enqueue(num.Key, num.Value);
393+
if(pq.Count > k){
394+
pq.Dequeue();
395+
}
396+
}
397+
398+
// //Stack-倒置
399+
// Stack<int> res = new();
400+
// while(pq.Count > 0){
401+
// res.Push(pq.Dequeue());
402+
// }
403+
// return res.ToArray();
404+
405+
//数组倒装
406+
int[] res = new int[k];
407+
for(int i = k - 1; i >= 0; i--){
408+
res[i] = pq.Dequeue();
409+
}
410+
return res;
411+
}
412+
```
378413

379414
-----------------------
380415
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/1047.删除字符串中的所有相邻重复项.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,22 @@ func removeDuplicates(_ s: String) -> String {
375375
}
376376
```
377377

378+
C#:
379+
```csharp
380+
public string RemoveDuplicates(string s) {
381+
//拿字符串直接作为栈,省去了栈还要转为字符串的操作
382+
StringBuilder res = new StringBuilder();
383+
384+
foreach(char c in s){
385+
if(res.Length > 0 && res[res.Length-1] == c){
386+
res.Remove(res.Length-1, 1);
387+
}else{
388+
res.Append(c);
389+
}
390+
}
391+
392+
return res.ToString();
393+
}
394+
```
378395
-----------------------
379396
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)