diff --git "a/problems/0020.346円234円211円346円225円210円347円232円204円346円213円254円345円217円267円.md" "b/problems/0020.346円234円211円346円225円210円347円232円204円346円213円254円345円217円267円.md"
index 7bb7f7463a..a0df0d07fc 100644
--- "a/problems/0020.346円234円211円346円225円210円347円232円204円346円213円254円345円217円267円.md"
+++ "b/problems/0020.346円234円211円346円225円210円347円232円204円346円213円254円345円217円267円.md"
@@ -400,6 +400,27 @@ bool isValid(char * s){
return !stackTop;
}
```
-
+Scala:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def isValid(s: String): Boolean = {
+ if(s.length % 2 != 0) return false // 如果字符串长度是奇数直接返回false
+ val stack = mutable.Stack[Char]()
+ // 循环遍历字符串
+ for (i <- s.indices) { + val c = s(i) + if (c == '(' || c == '[' || c == '{') stack.push(c) + else if(stack.isEmpty) return false // 如果没有(、[、{则直接返回false + // 以下三种情况,不满足则直接返回false + else if(c==')' && stack.pop() != '(') return false + else if(c==']' && stack.pop() != '[') return false + else if(c=='}' && stack.pop() != '{') return false + } + // 如果为空则正确匹配,否则还有余孽就不匹配 + stack.isEmpty + } +} +``` -----------------------
diff --git "a/problems/0028.345円256円236円347円216円260円strStr.md" "b/problems/0028.345円256円236円347円216円260円strStr.md"
index d67e5f70fc..1cdd5292a9 100644
--- "a/problems/0028.345円256円236円347円216円260円strStr.md"
+++ "b/problems/0028.345円256円236円347円216円260円strStr.md"
@@ -1166,5 +1166,80 @@ func strStr(_ haystack: String, _ needle: String) -> Int {
```
+PHP:
+
+> 前缀表统一减一
+```php
+function strStr($haystack, $needle) {
+ if (strlen($needle) == 0) return 0;
+ $next= [];
+ $this->getNext($next,$needle);
+
+ $j = -1;
+ for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始 + while($j>= 0 && $haystack[$i] != $needle[$j + 1]) {
+ $j = $next[$j];
+ }
+ if ($haystack[$i] == $needle[$j + 1]) {
+ $j++;
+ }
+ if ($j == (strlen($needle) - 1) ) {
+ return ($i - strlen($needle) + 1);
+ }
+ }
+ return -1;
+}
+
+function getNext(&$next, $s){
+ $j = -1;
+ $next[0] = $j;
+ for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始 + while ($j>= 0 && $s[$i] != $s[$j + 1]) {
+ $j = $next[$j];
+ }
+ if ($s[$i] == $s[$j + 1]) {
+ $j++;
+ }
+ $next[$i] = $j;
+ }
+}
+```
+
+> 前缀表统一不减一
+```php
+function strStr($haystack, $needle) {
+ if (strlen($needle) == 0) return 0;
+ $next= [];
+ $this->getNext($next,$needle);
+
+ $j = 0;
+ for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始 + while($j> 0 && $haystack[$i] != $needle[$j]) {
+ $j = $next[$j-1];
+ }
+ if ($haystack[$i] == $needle[$j]) {
+ $j++;
+ }
+ if ($j == strlen($needle)) {
+ return ($i - strlen($needle) + 1);
+ }
+ }
+ return -1;
+}
+
+function getNext(&$next, $s){
+ $j = 0;
+ $next[0] = $j;
+ for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始 + while ($j> 0 && $s[$i] != $s[$j]) {
+ $j = $next[$j-1];
+ }
+ if ($s[$i] == $s[$j]) {
+ $j++;
+ }
+ $next[$i] = $j;
+ }
+}
+```
-----------------------
diff --git "a/problems/0203.347円247円273円351円231円244円351円223円276円350円241円250円345円205円203円347円264円240円.md" "b/problems/0203.347円247円273円351円231円244円351円223円276円350円241円250円345円205円203円347円264円240円.md"
index 6777652919..fe78ddab7c 100644
--- "a/problems/0203.347円247円273円351円231円244円351円223円276円350円241円250円345円205円203円347円264円240円.md"
+++ "b/problems/0203.347円247円273円351円231円244円351円223円276円350円241円250円345円205円203円347円264円240円.md"
@@ -266,6 +266,27 @@ public ListNode removeElements(ListNode head, int val) {
}
return head;
}
+/**
+ * 不添加虚拟节点and pre Node方式
+ * 时间复杂度 O(n)
+ * 空间复杂度 O(1)
+ * @param head
+ * @param val
+ * @return
+ */
+public ListNode removeElements(ListNode head, int val) {
+ while(head!=null && head.val==val){
+ head = head.next;
+ }
+ ListNode curr = head;
+ while(curr!=null){
+ while(curr.next!=null && curr.next.val == val){
+ curr.next = curr.next.next;
+ }
+ curr = curr.next;
+ }
+ return head;
+}
```
Python:
diff --git "a/problems/0225.347円224円250円351円230円237円345円210円227円345円256円236円347円216円260円346円240円210円.md" "b/problems/0225.347円224円250円351円230円237円345円210円227円345円256円236円347円216円260円346円240円210円.md"
index 3457c4b368..3c134870ae 100644
--- "a/problems/0225.347円224円250円351円230円237円345円210円227円345円256円236円347円216円260円346円240円210円.md"
+++ "b/problems/0225.347円224円250円351円230円237円345円210円227円345円256円236円347円216円260円346円240円210円.md"
@@ -815,6 +815,89 @@ class MyStack {
}
}
```
+Scala:
+使用两个队列模拟栈:
+```scala
+import scala.collection.mutable
+
+class MyStack() {
+
+ val queue1 = new mutable.Queue[Int]()
+ val queue2 = new mutable.Queue[Int]()
+
+ def push(x: Int) {
+ queue1.enqueue(x)
+ }
+
+ def pop(): Int = {
+ var size = queue1.size
+ // 将queue1中的每个元素都移动到queue2
+ for (i <- 0 until size - 1) { + queue2.enqueue(queue1.dequeue()) + } + var res = queue1.dequeue() + // 再将queue2中的每个元素都移动到queue1 + while (!queue2.isEmpty) { + queue1.enqueue(queue2.dequeue()) + } + res + } + + def top(): Int = { + var size = queue1.size + for (i <- 0 until size - 1) { + queue2.enqueue(queue1.dequeue()) + } + var res = queue1.dequeue() + while (!queue2.isEmpty) { + queue1.enqueue(queue2.dequeue()) + } + // 最终还需要把res送进queue1 + queue1.enqueue(res) + res + } + + def empty(): Boolean = { + queue1.isEmpty + } +} +``` +使用一个队列模拟: +```scala +import scala.collection.mutable + +class MyStack() { + + val queue = new mutable.Queue[Int]() + + def push(x: Int) { + queue.enqueue(x) + } + + def pop(): Int = { + var size = queue.size + for (i <- 0 until size - 1) { + queue.enqueue(queue.head) // 把头添到队列最后 + queue.dequeue() // 再出队 + } + queue.dequeue() + } + + def top(): Int = { + var size = queue.size + var res = 0 + for (i <- 0 until size) { + queue.enqueue(queue.head) // 把头添到队列最后 + res = queue.dequeue() // 再出队 + } + res + } + + def empty(): Boolean = { + queue.isEmpty + } +} +``` -----------------------
diff --git "a/problems/0232.347円224円250円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.md" "b/problems/0232.347円224円250円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.md"
index 1a56d9f386..d9ba8e2632 100644
--- "a/problems/0232.347円224円250円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.md"
+++ "b/problems/0232.347円224円250円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.md"
@@ -495,6 +495,45 @@ void myQueueFree(MyQueue* obj) {
obj->stackOutTop = 0;
}
```
-
+Scala:
+```scala
+class MyQueue() {
+ import scala.collection.mutable
+ val stackIn = mutable.Stack[Int]() // 负责出栈
+ val stackOut = mutable.Stack[Int]() // 负责入栈
+
+ // 添加元素
+ def push(x: Int) {
+ stackIn.push(x)
+ }
+
+ // 复用代码,如果stackOut为空就把stackIn的所有元素都压入StackOut
+ def dumpStackIn(): Unit = {
+ if (!stackOut.isEmpty) return
+ while (!stackIn.isEmpty) {
+ stackOut.push(stackIn.pop())
+ }
+ }
+
+ // 弹出元素
+ def pop(): Int = {
+ dumpStackIn()
+ stackOut.pop()
+ }
+
+ // 获取队头
+ def peek(): Int = {
+ dumpStackIn()
+ val res: Int = stackOut.pop()
+ stackOut.push(res)
+ res
+ }
+
+ // 判断是否为空
+ def empty(): Boolean = {
+ stackIn.isEmpty && stackOut.isEmpty
+ }
+}
+```
-----------------------
diff --git "a/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" "b/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md"
index 61f08ad264..987ce27e34 100644
--- "a/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md"
+++ "b/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md"
@@ -193,7 +193,7 @@ class Solution {
否则的话,可以直接入栈。
注意,单调栈里 加入的元素是 下标。
*/
- Stackstack=new Stack();
+ Deque stack=new LinkedList();
stack.push(0);
for(int i=1;istack=new Stack();
+ Deque stack=new LinkedList();
for(int i=0;itemperatures[stack.peek()]){
diff --git "a/problems/1047.345円210円240円351円231円244円345円255円227円347円254円246円344円270円262円344円270円255円347円232円204円346円211円200円346円234円211円347円233円270円351円202円273円351円207円215円345円244円215円351円241円271円.md" "b/problems/1047.345円210円240円351円231円244円345円255円227円347円254円246円344円270円262円344円270円255円347円232円204円346円211円200円346円234円211円347円233円270円351円202円273円351円207円215円345円244円215円351円241円271円.md"
index 638c8f4ed2..a92a3911e1 100644
--- "a/problems/1047.345円210円240円351円231円244円345円255円227円347円254円246円344円270円262円344円270円255円347円232円204円346円211円200円346円234円211円347円233円270円351円202円273円351円207円215円345円244円215円351円241円271円.md"
+++ "b/problems/1047.345円210円240円351円231円244円345円255円227円347円254円246円344円270円262円344円270円255円347円232円204円346円211円200円346円234円211円347円233円270円351円202円273円351円207円215円345円244円215円351円241円271円.md"
@@ -374,6 +374,27 @@ func removeDuplicates(_ s: String) -> String {
return String(stack)
}
```
-
+Scala:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def removeDuplicates(s: String): String = {
+ var stack = mutable.Stack[Int]()
+ var str = "" // 保存最终结果
+ for (i <- s.indices) { + var tmp = s(i) + // 如果栈非空并且栈顶元素等于当前字符,那么删掉栈顶和字符串最后一个元素 + if (!stack.isEmpty && tmp == stack.head) { + str = str.take(str.length - 1) + stack.pop() + } else { + stack.push(tmp) + str += tmp + } + } + str + } +} +``` -----------------------
diff --git "a/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md" "b/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md"
index 581ac1c7cc..7c39ed69ae 100644
--- "a/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md"
+++ "b/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md"
@@ -290,6 +290,28 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
}
```
+
+### PHP
+
+```php
+function reverseLeftWords($s, $n) {
+ $this->reverse($s,0,$n-1); //反转区间为前n的子串
+ $this->reverse($s,$n,strlen($s)-1); //反转区间为n到末尾的子串
+ $this->reverse($s,0,strlen($s)-1); //反转整个字符串
+ return $s;
+}
+
+// 按指定进行翻转 【array、string都可】
+function reverse(&$s, $start, $end) {
+ for ($i = $start, $j = $end; $i < $j; $i++, $j--) { + $tmp = $s[$i]; + $s[$i] = $s[$j]; + $s[$j] = $tmp; + } +} +``` + + Scala: ```scala @@ -322,5 +344,6 @@ object Solution { + -----------------------