From 5efb10740ed78aa75dfb89f81ab1db5be04f7b3d Mon Sep 17 00:00:00 2001 From: Hnuczc <3260189532@qq.com> Date: Wed, 5 Jan 2022 19:08:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0150.逆波兰表达式求值.md | 39 ++++++++--------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index 7f7b2f4c..f44703f1 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -133,39 +133,26 @@ public: java: ```Java -public class EvalRPN { - +class Solution { public int evalRPN(String[] tokens) { Deque stack = new LinkedList(); - for (String token : tokens) { - char c = token.charAt(0); - if (!isOpe(token)) { - stack.addFirst(stoi(token)); - } else if (c == '+') { - stack.push(stack.pop() + stack.pop()); - } else if (c == '-') { - stack.push(- stack.pop() + stack.pop()); - } else if (c == '*') { - stack.push( stack.pop() * stack.pop()); + for (int i = 0; i < tokens.length; ++i) { + if ("+".equals(tokens[i])) { // leetcode 内置jdk的问题,不能使用==判断字符串是否相等 + stack.push(stack.pop() + stack.pop()); // 注意 - 和/ 需要特殊处理 + } else if ("-".equals(tokens[i])) { + stack.push(-stack.pop() + stack.pop()); + } else if ("*".equals(tokens[i])) { + stack.push(stack.pop() * stack.pop()); + } else if ("/".equals(tokens[i])) { + int temp1 = stack.pop(); + int temp2 = stack.pop(); + stack.push(temp2 / temp1); } else { - int num1 = stack.pop(); - int num2 = stack.pop(); - stack.push( num2/num1); + stack.push(Integer.valueOf(tokens[i])); } } return stack.pop(); } - private boolean isOpe(String s) { - return s.length() == 1 && s.charAt(0) <'0' || s.charAt(0) >'9'; - } - private int stoi(String s) { - return Integer.valueOf(s); - } - - public static void main(String[] args) { - new EvalRPN().evalRPN(new String[] {"10","6","9","3","+","-11","*","/","*","17","+","5","+"}); - } - } ``` From d28293da0c42ddf96599b3bdb022040fca548c62 Mon Sep 17 00:00:00 2001 From: YuYiming <41357594+Leovoki@users.noreply.github.com> Date: Wed, 5 Jan 2022 21:26:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 有些人使用的是从0开始的next数组不是从-1的,因此会疑惑。 --- problems/0459.重复的子字符串.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index fee9dee5..9c74f4a7 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -45,7 +45,7 @@ 这里就要说一说next数组了,next 数组记录的就是最长相同前后缀( [字符串:KMP算法精讲](https://programmercarl.com/0028.实现strStr.html) 这里介绍了什么是前缀,什么是后缀,什么又是最长相同前后缀), 如果 next[len - 1] != -1,则说明字符串有最长相同的前后缀(就是字符串里的前缀子串和后缀子串相同的最长长度)。 -最长相等前后缀的长度为:next[len - 1] + 1。 +最长相等前后缀的长度为:next[len - 1] + 1。(这里的next数组是以统一减一的方式计算的,因此需要+1) 数组长度为:len。 From 774c3da8309c468f3f12c28c8180fae6403a976d Mon Sep 17 00:00:00 2001 From: YiChih Wang Date: Wed, 5 Jan 2022 22:32:18 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2=20Rust=E8=AA=9E=E8=A8=80?= =?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/0131.分割回文串.md | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index ec8ad61c..439ad8ea 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -587,5 +587,57 @@ func partition(_ s: String) -> [[String]] { } ``` +## Rust + +```rust +impl Solution { + pub fn partition(s: String) -> Vec> { + let mut ret = vec![]; + let mut path = vec![]; + let sub_str: Vec = s.chars().collect(); + + Self::backtracing(&sub_str, 0, &mut ret, &mut path); + + ret + } + + fn backtracing(sub_str: &Vec, start: usize, ret: &mut Vec>, path: &mut Vec) { + //如果起始位置大于s的大小,说明找到了一组分割方案 + if start >= sub_str.len() { + ret.push(path.clone()); + return; + } + + for i in start..sub_str.len() { + if !Self::is_palindrome(sub_str, start, i) { + continue; + } + //如果是回文子串,则记录 + let s: String = sub_str[start..i+1].into_iter().collect(); + path.push(s); + + //起始位置后移,保证不重复 + Self::backtracing(sub_str, i+1, ret, path); + path.pop(); + } + + } + + fn is_palindrome(s: &Vec, start: usize, end: usize) -> bool { + let (mut start, mut end) = (start, end); + + while start < end { + if s[start] != s[end] { + return false; + } + + start += 1; + end -= 1; + } + + true + } +} +``` -----------------------