From 74b76bfd120446653495d96135e697b5797bae35 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 22:21:29 +0800
Subject: [PATCH 01/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201002.=E6=9F=A5?=
=?UTF-8?q?=E6=89=BE=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/1002.查找常用字符.md | 34 ++++++++++++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md
index 36937b0b..075b5ef1 100644
--- a/problems/1002.查找常用字符.md
+++ b/problems/1002.查找常用字符.md
@@ -418,6 +418,38 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){
return ret;
}
```
-
+Scala:
+```scala
+object Solution {
+ def commonChars(words: Array[String]): List[String] = {
+ // 声明返回结果的不可变List集合,因为res要重新赋值,所以声明为var
+ var res = List[String]()
+ var hash = new Array[Int](26) // 统计字符出现的最小频率
+ // 统计第一个字符串中字符出现的次数
+ for (i <- 0 until words(0).length) {
+ hash(words(0)(i) - 'a') += 1
+ }
+ // 统计其他字符串出现的频率
+ for (i <- 1 until words.length) {
+ // 统计其他字符出现的频率
+ var hashOtherStr = new Array[Int](26)
+ for (j <- 0 until words(i).length) {
+ hashOtherStr(words(i)(j) - 'a') += 1
+ }
+ // 更新hash,取26个字母最小出现的频率
+ for (k <- 0 until 26) {
+ hash(k) = math.min(hash(k), hashOtherStr(k))
+ }
+ }
+ // 根据hash的结果转换输出的形式
+ for (i <- 0 until 26) {
+ for (j <- 0 until hash(i)) {
+ res = res :+ (i + 'a').toChar.toString
+ }
+ }
+ res
+ }
+}
+```
-----------------------
From 78c6602ff20c309132007795956f332d66ff7130 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 10:05:27 +0800
Subject: [PATCH 02/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200349.=E4=B8=A4?=
=?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0349.两个数组的交集.md | 42 ++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md
index 45f19b6e..49cf4112 100644
--- a/problems/0349.两个数组的交集.md
+++ b/problems/0349.两个数组的交集.md
@@ -312,7 +312,49 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
return result;
}
```
+Scala:
+正常解法:
+```scala
+object Solution {
+ def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
+ // 导入mutable
+ import scala.collection.mutable
+ // 临时Set,用于记录数组1出现的每个元素
+ val tmpSet: mutable.HashSet[Int] = new mutable.HashSet[Int]()
+ // 结果Set,存储最终结果
+ val resSet: mutable.HashSet[Int] = new mutable.HashSet[Int]()
+ // 遍历nums1,把每个元素添加到tmpSet
+ nums1.foreach(tmpSet.add(_))
+ // 遍历nums2,如果在tmpSet存在就添加到resSet
+ nums2.foreach(elem => {
+ if (tmpSet.contains(elem)) {
+ resSet.add(elem)
+ }
+ })
+ // 将结果转换为Array返回,return可以省略
+ resSet.toArray
+ }
+}
+```
+骚操作1:
+```scala
+object Solution {
+ def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
+ // 先转为Set,然后取交集,最后转换为Array
+ (nums1.toSet).intersect(nums2.toSet).toArray
+ }
+}
+```
+骚操作2:
+```scala
+object Solution {
+ def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
+ // distinct去重,然后取交集
+ (nums1.distinct).intersect(nums2.distinct)
+ }
+}
+```
## 相关题目
* 350.两个数组的交集 II
From 9f720470e8e2360047052897885261545d0e1ac7 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 10:29:17 +0800
Subject: [PATCH 03/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202.=E5=BF=AB?=
=?UTF-8?q?=E4=B9=90=E6=95=B0.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0202.快乐数.md | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md
index 741a735a..2d0f7d27 100644
--- a/problems/0202.快乐数.md
+++ b/problems/0202.快乐数.md
@@ -385,5 +385,38 @@ bool isHappy(int n){
return bHappy;
}
```
+Scala:
+```scala
+object Solution {
+ // 引入mutable
+ import scala.collection.mutable
+ def isHappy(n: Int): Boolean = {
+ // 存放每次计算后的结果
+ val set: mutable.HashSet[Int] = new mutable.HashSet[Int]()
+ var tmp = n // 因为形参是不可变量,所以需要找到一个临时变量
+ // 开始进入循环
+ while (true) {
+ val sum = getSum(tmp) // 获取这个数每个值的平方和
+ if (sum == 1) return true // 如果最终等于 1,则返回true
+ // 如果set里面已经有这个值了,说明进入无限循环,可以返回false,否则添加这个值到set
+ if (set.contains(sum)) return false
+ else set.add(sum)
+ tmp = sum
+ }
+ // 最终需要返回值,直接返回个false
+ false
+ }
+
+ def getSum(n: Int): Int = {
+ var sum = 0
+ var tmp = n
+ while (tmp != 0) {
+ sum += (tmp % 10) * (tmp % 10)
+ tmp = tmp / 10
+ }
+ sum
+ }
+}
+```
-----------------------
From be5cc136c56cb194af8ce1d794871ca7af4b1e1d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 11:00:11 +0800
Subject: [PATCH 04/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200035.=E6=90=9C?=
=?UTF-8?q?=E7=B4=A2=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0035.搜索插入位置.md | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md
index 9a770703..3e8cf5c8 100644
--- a/problems/0035.搜索插入位置.md
+++ b/problems/0035.搜索插入位置.md
@@ -316,7 +316,26 @@ func searchInsert(_ nums: [Int], _ target: Int) -> Int {
return right + 1
}
```
-
+### Scala
+```scala
+object Solution {
+ def searchInsert(nums: Array[Int], target: Int): Int = {
+ var left = 0
+ var right = nums.length - 1
+ while (left <= right) {
+ var mid = left + (right - left) / 2
+ if (target == nums(mid)) {
+ return mid
+ } else if (target > nums(mid)) {
+ left = mid + 1
+ } else {
+ right = mid - 1
+ }
+ }
+ right + 1
+ }
+}
+```
From 95593634b341a951850fae3b5aa739ba7c5d3864 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 11:01:04 +0800
Subject: [PATCH 05/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200704.=E4=BA=8C?=
=?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0704.二分查找.md | 41 +++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index 55625130..1e474f9a 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -610,7 +610,48 @@ public class Solution{
}
}
```
+**Scala:**
+(版本一)左闭右闭区间
+```scala
+object Solution {
+ def search(nums: Array[Int], target: Int): Int = {
+ var left = 0
+ var right = nums.length - 1
+ while (left <= right) {
+ var mid = left + ((right - left) / 2)
+ if (target == nums(mid)) {
+ return mid
+ } else if (target < nums(mid)) {
+ right = mid - 1
+ } else {
+ left = mid + 1
+ }
+ }
+ -1
+ }
+}
+```
+(版本二)左闭右开区间
+```scala
+object Solution {
+ def search(nums: Array[Int], target: Int): Int = {
+ var left = 0
+ var right = nums.length
+ while (left < right) {
+ val mid = left + (right - left) / 2
+ if (target == nums(mid)) {
+ return mid
+ } else if (target < nums(mid)) {
+ right = mid
+ } else {
+ left = mid + 1
+ }
+ }
+ -1
+ }
+}
+```
-----------------------
From 9fc232aba413b43fd479ad5b4fd1a186ff90a359 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 11:01:45 +0800
Subject: [PATCH 06/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200034.=E5=9C=A8?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE?=
=?UTF-8?q?=E5=85=83=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C?=
=?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...元素的第一个和最后一个位置.md | 45 +++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md
index dfd90b82..260462c2 100644
--- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md
+++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md
@@ -480,7 +480,52 @@ var searchRange = function(nums, target) {
return [-1, -1];
};
```
+### Scala
+```scala
+object Solution {
+ def searchRange(nums: Array[Int], target: Int): Array[Int] = {
+ var left = getLeftBorder(nums, target)
+ var right = getRightBorder(nums, target)
+ if (left == -2 || right == -2) return Array(-1, -1)
+ if (right - left > 1) return Array(left + 1, right - 1)
+ Array(-1, -1)
+ }
+ // 寻找左边界
+ def getLeftBorder(nums: Array[Int], target: Int): Int = {
+ var leftBorder = -2
+ var left = 0
+ var right = nums.length - 1
+ while (left <= right) {
+ var mid = left + (right - left) / 2
+ if (nums(mid) >= target) {
+ right = mid - 1
+ leftBorder = right
+ } else {
+ left = mid + 1
+ }
+ }
+ leftBorder
+ }
+
+ // 寻找右边界
+ def getRightBorder(nums: Array[Int], target: Int): Int = {
+ var rightBorder = -2
+ var left = 0
+ var right = nums.length - 1
+ while (left <= right) {
+ var mid = left + (right - left) / 2
+ if (nums(mid) <= target) {
+ left = mid + 1
+ rightBorder = left
+ } else {
+ right = mid - 1
+ }
+ }
+ rightBorder
+ }
+}
+```
-----------------------
From f131f27744693b91f40df4ea0fb7c1578bd6647b Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 11:02:19 +0800
Subject: [PATCH 07/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0027.移除元素.md | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md
index 590cf0b9..5ff327fb 100644
--- a/problems/0027.移除元素.md
+++ b/problems/0027.移除元素.md
@@ -328,6 +328,20 @@ int removeElement(int* nums, int numsSize, int val){
return slow;
}
```
-
+Scala:
+```scala
+object Solution {
+ def removeElement(nums: Array[Int], `val`: Int): Int = {
+ var slow = 0
+ for (fast <- 0 until nums.length) {
+ if (`val` != nums(fast)) {
+ nums(slow) = nums(fast)
+ slow += 1
+ }
+ }
+ slow
+ }
+}
+```
-----------------------
From 495998e509527b53a8b84cc0527dfcff4fc1e1af Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 13 May 2022 12:07:52 +0800
Subject: [PATCH 08/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880188.=E4=B9=B0?=
=?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?=
=?UTF-8?q?=E6=9C=BAIV.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0188.买卖股票的最佳时机IV.md | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md
index 61c558a1..27eb38c3 100644
--- a/problems/0188.买卖股票的最佳时机IV.md
+++ b/problems/0188.买卖股票的最佳时机IV.md
@@ -409,5 +409,27 @@ var maxProfit = function(k, prices) {
};
```
+TypeScript:
+
+```typescript
+function maxProfit(k: number, prices: number[]): number {
+ const length: number = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = new Array(length).fill(0)
+ .map(_ => new Array(k * 2 + 1).fill(0));
+ for (let i = 1; i <= k; i++) {
+ dp[0][i * 2 - 1] = -prices[0];
+ }
+ for (let i = 1; i < length; i++) {
+ for (let j = 1; j < 2 * k + 1; j++) {
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1] + Math.pow(-1, j) * prices[i]);
+ }
+ }
+ return dp[length - 1][2 * k];
+};
+```
+
+
+
-----------------------
From 127986e03a0b4acdedf80a6b38d3ded7d61514f3 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 13 May 2022 16:19:59 +0800
Subject: [PATCH 09/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880309.=E6=9C=80?=
=?UTF-8?q?=E4=BD=B3=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA?=
=?UTF-8?q?=E5=90=AB=E5=86=B7=E5=86=BB=E6=9C=9F.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...09.最佳买卖股票时机含冷冻期.md | 60 +++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md
index f3e7541b..f037fe85 100644
--- a/problems/0309.最佳买卖股票时机含冷冻期.md
+++ b/problems/0309.最佳买卖股票时机含冷冻期.md
@@ -325,6 +325,66 @@ const maxProfit = (prices) => {
};
```
+TypeScript:
+
+> 版本一,与本文思路一致
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 持股状态;
+ dp[i][1]: 无股状态,当天为非冷冻期;
+ dp[i][2]: 无股状态,当天卖出;
+ dp[i][3]: 无股状态,当天为冷冻期;
+ */
+ const length: number = prices.length;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0][0] = -prices[0];
+ dp[0][1] = dp[0][2] = dp[0][3] = 0;
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = Math.max(
+ dp[i - 1][0],
+ Math.max(dp[i - 1][1], dp[i - 1][3]) - prices[i]
+ );
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][3]);
+ dp[i][2] = dp[i - 1][0] + prices[i];
+ dp[i][3] = dp[i - 1][2];
+ }
+ const lastEl: number[] = dp[length - 1];
+ return Math.max(lastEl[1], lastEl[2], lastEl[3]);
+};
+```
+
+> 版本二,状态定义略有不同,可以帮助理解
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 持股状态,当天买入;
+ dp[i][1]: 持股状态,当天未买入;
+ dp[i][2]: 无股状态,当天卖出;
+ dp[i][3]: 无股状态,当天未卖出;
+
+ 买入有冷冻期限制,其实就是状态[0]只能由前一天的状态[3]得到;
+ 如果卖出有冷冻期限制,其实就是[2]由[1]得到。
+ */
+ const length: number = prices.length;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0][0] = -prices[0];
+ dp[0][1] = -Infinity;
+ dp[0][2] = dp[0][3] = 0;
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = dp[i - 1][3] - prices[i];
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0]);
+ dp[i][2] = Math.max(dp[i - 1][0], dp[i - 1][1]) + prices[i];
+ dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2]);
+ }
+ return Math.max(dp[length - 1][2], dp[length - 1][3]);
+};
+```
+
+
+
-----------------------
From 6c0d4365c6ed1e9a47c5f31e14d59912648f4afa Mon Sep 17 00:00:00 2001
From: lizhendong128
Date: Fri, 13 May 2022 17:26:03 +0800
Subject: [PATCH 10/11] =?UTF-8?q?=E4=BF=AE=E6=94=B90059=E8=9E=BA=E6=97=8B?=
=?UTF-8?q?=E7=9F=A9=E9=98=B5II=20Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
精简Java代码中不必要的变量,调整结构,减少代码量,使代码更加易读
---
problems/0059.螺旋矩阵II.md | 44 +++++++++------------------------
1 file changed, 12 insertions(+), 32 deletions(-)
diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md
index 93735895..7afc1575 100644
--- a/problems/0059.螺旋矩阵II.md
+++ b/problems/0059.螺旋矩阵II.md
@@ -130,57 +130,37 @@ Java:
```Java
class Solution {
public int[][] generateMatrix(int n) {
+ int loop = 0; // 控制循环次数
int[][] res = new int[n][n];
+ int start = 0; // 每次循环的开始点(start, start)
+ int count = 1; // 定义填充数字
+ int i, j;
- // 循环次数
- int loop = n / 2;
-
- // 定义每次循环起始位置
- int startX = 0;
- int startY = 0;
-
- // 定义偏移量
- int offset = 1;
-
- // 定义填充数字
- int count = 1;
-
- // 定义中间位置
- int mid = n / 2;
- while (loop > 0) {
- int i = startX;
- int j = startY;
-
+ while (loop++ < n / 2) { // 判断边界后,loop从1开始
// 模拟上侧从左到右
- for (; j startY; j--) {
+ for (; j >= loop; j--) {
res[i][j] = count++;
}
// 模拟左侧从下到上
- for (; i > startX; i--) {
+ for (; i >= loop; i--) {
res[i][j] = count++;
}
-
- loop--;
-
- startX += 1;
- startY += 1;
-
- offset += 2;
+ start++;
}
if (n % 2 == 1) {
- res[mid][mid] = count;
+ res[start][start] = count;
}
return res;
From 0b88af0824b696be6d2f107adde3758f1df1c3f8 Mon Sep 17 00:00:00 2001
From: lizhendong128
Date: Thu, 19 May 2022 11:08:59 +0800
Subject: [PATCH 11/11] =?UTF-8?q?=E4=BF=AE=E6=94=B90150=E9=80=86=E6=B3=A2?=
=?UTF-8?q?=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC=20Java?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
将for循环改为for each,使代码更加简洁。因为循环除了对token进行遍历,i并没有其他用途。
---
problems/0150.逆波兰表达式求值.md | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md
index fd3d69aa..6ce7e2f9 100644
--- a/problems/0150.逆波兰表达式求值.md
+++ b/problems/0150.逆波兰表达式求值.md
@@ -136,19 +136,19 @@ java:
class Solution {
public int evalRPN(String[] tokens) {
Deque stack = new LinkedList();
- for (int i = 0; i < tokens.length; ++i) {
- if ("+".equals(tokens[i])) { // leetcode 内置jdk的问题,不能使用==判断字符串是否相等
+ for (String s : tokens) {
+ if ("+".equals(s)) { // leetcode 内置jdk的问题,不能使用==判断字符串是否相等
stack.push(stack.pop() + stack.pop()); // 注意 - 和/ 需要特殊处理
- } else if ("-".equals(tokens[i])) {
+ } else if ("-".equals(s)) {
stack.push(-stack.pop() + stack.pop());
- } else if ("*".equals(tokens[i])) {
+ } else if ("*".equals(s)) {
stack.push(stack.pop() * stack.pop());
- } else if ("/".equals(tokens[i])) {
+ } else if ("/".equals(s)) {
int temp1 = stack.pop();
int temp2 = stack.pop();
stack.push(temp2 / temp1);
} else {
- stack.push(Integer.valueOf(tokens[i]));
+ stack.push(Integer.valueOf(s));
}
}
return stack.pop();