mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -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<char>();
|
||||
// 遍历字符串
|
||||
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
|
||||
|
@ -326,6 +326,40 @@ func evalRPN(_ tokens: [String]) -> Int {
|
||||
}
|
||||
```
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public int EvalRPN(string[] tokens) {
|
||||
int num;
|
||||
Stack<int> stack = new Stack<int>();
|
||||
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
|
||||
|
@ -900,6 +900,41 @@ class MyStack() {
|
||||
```
|
||||
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public class MyStack {
|
||||
Queue<int> queue1;
|
||||
Queue<int> queue2;
|
||||
public MyStack() {
|
||||
queue1 = new Queue<int>();
|
||||
queue2 = new Queue<int>();
|
||||
}
|
||||
|
||||
public void Push(int x) {
|
||||
queue2.Enqueue(x);
|
||||
while(queue1.Count != 0){
|
||||
queue2.Enqueue(queue1.Dequeue());
|
||||
}
|
||||
Queue<int> 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
|
||||
|
@ -497,6 +497,49 @@ void myQueueFree(MyQueue* obj) {
|
||||
```
|
||||
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public class MyQueue {
|
||||
Stack<int> inStack;
|
||||
Stack<int> outStack;
|
||||
|
||||
public MyQueue() {
|
||||
inStack = new Stack<int>();// 负责进栈
|
||||
outStack = new Stack<int>();// 负责出栈
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -756,5 +756,46 @@ class MyQueue{
|
||||
}
|
||||
```
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
class myDequeue{
|
||||
private LinkedList<int> linkedList = new LinkedList<int>();
|
||||
|
||||
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<int> res = new List<int>();
|
||||
|
||||
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();
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -371,6 +371,44 @@ function topKFrequent(nums: number[], k: number): number[] {
|
||||
};
|
||||
```
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public int[] TopKFrequent(int[] nums, int k) {
|
||||
//哈希表-标权重
|
||||
Dictionary<int,int> 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<int,int> pq = new();
|
||||
foreach(var num in dic){
|
||||
pq.Enqueue(num.Key, num.Value);
|
||||
if(pq.Count > k){
|
||||
pq.Dequeue();
|
||||
}
|
||||
}
|
||||
|
||||
// //Stack-倒置
|
||||
// Stack<int> 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)
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
-----------------------
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user