Merge branch 'youngyangyang04:master' into master

This commit is contained in:
YDLIN
2022-01-12 15:59:35 +08:00
committed by GitHub
4 changed files with 137 additions and 27 deletions

View File

@ -323,5 +323,76 @@ func permuteUnique(_ nums: [Int]) -> [[Int]] {
} }
``` ```
### C
```c
//临时数组
int *path;
//返回数组
int **ans;
int *used;
int pathTop, ansTop;
//拷贝path到ans中
void copyPath() {
int *tempPath = (int*)malloc(sizeof(int) * pathTop);
int i;
for(i = 0; i < pathTop; ++i) {
tempPath[i] = path[i];
}
ans[ansTop++] = tempPath;
}
void backTracking(int* used, int *nums, int numsSize) {
//若path中元素个数等于numsSize将path拷贝入ans数组中
if(pathTop == numsSize)
copyPath();
int i;
for(i = 0; i < numsSize; i++) {
//若当前元素已被使用
//或前一位元素与当前元素值相同但并未被使用
//则跳过此分支
if(used[i] || (i != 0 && nums[i] == nums[i-1] && used[i-1] == 0))
continue;
//将当前元素的使用情况设为True
used[i] = 1;
path[pathTop++] = nums[i];
backTracking(used, nums, numsSize);
used[i] = 0;
--pathTop;
}
}
int cmp(void* elem1, void* elem2) {
return *((int*)elem1) - *((int*)elem2);
}
int** permuteUnique(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
//排序数组
qsort(nums, numsSize, sizeof(int), cmp);
//初始化辅助变量
pathTop = ansTop = 0;
path = (int*)malloc(sizeof(int) * numsSize);
ans = (int**)malloc(sizeof(int*) * 1000);
//初始化used辅助数组
used = (int*)malloc(sizeof(int) * numsSize);
int i;
for(i = 0; i < numsSize; i++) {
used[i] = 0;
}
backTracking(used, nums, numsSize);
//设置返回的数组的长度
*returnSize = ansTop;
*returnColumnSizes = (int*)malloc(sizeof(int) * ansTop);
int z;
for(z = 0; z < ansTop; z++) {
(*returnColumnSizes)[z] = numsSize;
}
return ans;
}
```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -587,5 +587,57 @@ func partition(_ s: String) -> [[String]] {
} }
``` ```
## Rust
```rust
impl Solution {
pub fn partition(s: String) -> Vec<Vec<String>> {
let mut ret = vec![];
let mut path = vec![];
let sub_str: Vec<char> = s.chars().collect();
Self::backtracing(&sub_str, 0, &mut ret, &mut path);
ret
}
fn backtracing(sub_str: &Vec<char>, start: usize, ret: &mut Vec<Vec<String>>, path: &mut Vec<String>) {
//如果起始位置大于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<char>, 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
}
}
```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -133,39 +133,26 @@ public:
java: java:
```Java ```Java
public class EvalRPN { class Solution {
public int evalRPN(String[] tokens) { public int evalRPN(String[] tokens) {
Deque<Integer> stack = new LinkedList(); Deque<Integer> stack = new LinkedList();
for (String token : tokens) { for (int i = 0; i < tokens.length; ++i) {
char c = token.charAt(0); if ("+".equals(tokens[i])) { // leetcode 内置jdk的问题不能使用==判断字符串是否相等
if (!isOpe(token)) { stack.push(stack.pop() + stack.pop()); // 注意 - 和/ 需要特殊处理
stack.addFirst(stoi(token)); } else if ("-".equals(tokens[i])) {
} else if (c == '+') { stack.push(-stack.pop() + stack.pop());
stack.push(stack.pop() + stack.pop()); } else if ("*".equals(tokens[i])) {
} else if (c == '-') { stack.push(stack.pop() * stack.pop());
stack.push(- stack.pop() + stack.pop()); } else if ("/".equals(tokens[i])) {
} else if (c == '*') { int temp1 = stack.pop();
stack.push( stack.pop() * stack.pop()); int temp2 = stack.pop();
stack.push(temp2 / temp1);
} else { } else {
int num1 = stack.pop(); stack.push(Integer.valueOf(tokens[i]));
int num2 = stack.pop();
stack.push( num2/num1);
} }
} }
return stack.pop(); 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","+"});
}
} }
``` ```

View File

@ -45,7 +45,7 @@
这里就要说一说next数组了next 数组记录的就是最长相同前后缀( [字符串KMP算法精讲](https://programmercarl.com/0028.实现strStr.html) 这里介绍了什么是前缀,什么是后缀,什么又是最长相同前后缀) 如果 next[len - 1] != -1则说明字符串有最长相同的前后缀就是字符串里的前缀子串和后缀子串相同的最长长度 这里就要说一说next数组了next 数组记录的就是最长相同前后缀( [字符串KMP算法精讲](https://programmercarl.com/0028.实现strStr.html) 这里介绍了什么是前缀,什么是后缀,什么又是最长相同前后缀) 如果 next[len - 1] != -1则说明字符串有最长相同的前后缀就是字符串里的前缀子串和后缀子串相同的最长长度
最长相等前后缀的长度为next[len - 1] + 1。 最长相等前后缀的长度为next[len - 1] + 1。(这里的next数组是以统一减一的方式计算的因此需要+1)
数组长度为len。 数组长度为len。