Merge branch 'master' into dev

This commit is contained in:
程序员Carl
2022-10-09 10:27:26 +08:00
committed by GitHub
10 changed files with 320 additions and 6 deletions

View File

@ -338,5 +338,28 @@ object Solution {
}
}
```
Rust:
```rust
impl Solution {
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, mut n: i32) -> Option<Box<ListNode>> {
let mut dummy_head = Box::new(ListNode::new(0));
dummy_head.next = head;
let mut fast = &dummy_head.clone();
let mut slow = &mut dummy_head;
while n > 0 {
fast = fast.next.as_ref().unwrap();
n -= 1;
}
while fast.next.is_some() {
fast = fast.next.as_ref().unwrap();
slow = slow.next.as_mut().unwrap();
}
slow.next = slow.next.as_mut().unwrap().next.take();
dummy_head.next
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -380,5 +380,51 @@ function swapPairs($head)
}
```
Rust:
```rust
// 虚拟头节点
impl Solution {
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy_head = Box::new(ListNode::new(0));
dummy_head.next = head;
let mut cur = dummy_head.as_mut();
while let Some(mut node) = cur.next.take() {
if let Some(mut next) = node.next.take() {
node.next = next.next.take();
next.next = Some(node);
cur.next = Some(next);
cur = cur.next.as_mut().unwrap().next.as_mut().unwrap();
} else {
cur.next = Some(node);
cur = cur.next.as_mut().unwrap();
}
}
dummy_head.next
}
}
```
```rust
// 递归
impl Solution {
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head == None || head.as_ref().unwrap().next == None {
return head;
}
let mut node = head.unwrap();
if let Some(mut next) = node.next.take() {
node.next = Solution::swap_pairs(next.next);
next.next = Some(node);
Some(next)
} else {
Some(node)
}
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -429,7 +429,57 @@ function searchInsert($nums, $target)
return $r + 1;
}
```
### C
```c
//版本一 [left, right]左闭右闭区间
int searchInsert(int* nums, int numsSize, int target){
//左闭右开区间 [0 , numsSize-1]
int left =0;
int mid =0;
int right = numsSize - 1;
while(left <= right){//左闭右闭区间 所以可以 left == right
mid = left + (right - left) / 2;
if(target < nums[mid]){
//target 在左区间 [left, mid - 1]中原区间包含mid右区间边界可以向左内缩
right = mid -1;
}else if( target > nums[mid]){
//target 在右区间 [mid + 1, right]中,原区间包含mid左区间边界可以向右内缩
left = mid + 1;
}else {
// nums[mid] == target 顺利找到target直接返回mid
return mid;
}
}
//数组中未找到target元素
//target在数组所有元素之后[left, right]是右闭区间,需要返回 right +1
return right + 1;
}
```
```c
//版本二 [left, right]左闭右开区间
int searchInsert(int* nums, int numsSize, int target){
//左闭右开区间 [0 , numsSize)
int left =0;
int mid =0;
int right = numsSize;
while(left < right){//左闭右闭区间 所以 left < right
mid = left + (right - left) / 2;
if(target < nums[mid]){
//target 在左区间 [left, mid)中原区间没有包含mid右区间边界不能内缩
right = mid ;
}else if( target > nums[mid]){
// target 在右区间 [mid+1, right)中原区间包含mid左区间边界可以向右内缩
left = mid + 1;
}else {
// nums[mid] == target 顺利找到target直接返回mid
return mid;
}
}
//数组中未找到target元素
//target在数组所有元素之后[left, right)是右开区间, return right即可
return right;
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -364,6 +364,7 @@ function minDistance(word1: string, word2: string): number {
C
```c
int min(int num1, int num2, int num3) {
return num1 > num2 ? (num2 > num3 ? num3 : num2) : (num1 > num3 ? num3 : num1);

View File

@ -367,6 +367,39 @@ function subsetsWithDup(nums: number[]): number[][] {
};
```
set去重版本:
```typescript
// 使用set去重版本
function subsetsWithDup(nums: number[]): number[][] {
const result: number[][] = [];
const path: number[] = [];
// 去重之前先排序
nums.sort((a, b) => a - b);
function backTracking(startIndex: number) {
// 收集结果
result.push([...path])
// 此处不返回也可以因为每次递归都会使startIndex + 1当这个数大到nums.length的时候就不会进入递归了。
if (startIndex === nums.length) {
return
}
// 定义每一个树层的set集合
const set: Set<number> = new Set()
for (let i = startIndex; i < nums.length; i++) {
// 去重
if (set.has(nums[i])) {
continue
}
set.add(nums[i])
path.push(nums[i])
backTracking(i + 1)
// 回溯
path.pop()
}
}
backTracking(0)
return result
};
```
### Rust
```Rust

View File

@ -89,26 +89,29 @@ C++代码如下:
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
// 力扣修改了后台测试数据需要用longlong
stack<long long> st;
for (int i = 0; i < tokens.size(); i++) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int num1 = st.top();
long long num1 = st.top();
st.pop();
int num2 = st.top();
long long num2 = st.top();
st.pop();
if (tokens[i] == "+") st.push(num2 + num1);
if (tokens[i] == "-") st.push(num2 - num1);
if (tokens[i] == "*") st.push((long)num2 * (long)num1); //力扣改了后台测试数据
if (tokens[i] == "/") st.push(num2 / num1);
} else {
st.push(stoi(tokens[i]));
st.push(stoll(tokens[i]));
}
}
int result = st.top();
st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
return result;
}
};
```
## 题外话

View File

@ -588,5 +588,45 @@ object Solution {
}
```
Rust:
双指针法:
```rust
impl Solution {
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut cur = head;
let mut pre = None;
while let Some(mut node) = cur.take() {
cur = node.next;
node.next = pre;
pre = Some(node);
}
pre
}
}
```
递归法:
```rust
impl Solution {
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
fn rev(
mut head: Option<Box<ListNode>>,
mut pre: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
if let Some(mut node) = head.take() {
let cur = node.next;
node.next = pre;
pre = Some(node);
return rev(cur, pre);
}
pre
}
rev(head, None)
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -274,6 +274,7 @@ function findContentChildren(g: number[], s: number[]): number {
### C
```c
///小餅乾先餵飽小胃口的
int cmp(int* a, int* b) {
return *a - *b;
}
@ -296,6 +297,33 @@ int findContentChildren(int* g, int gSize, int* s, int sSize){
}
```
```c
///大餅乾先餵飽大胃口的
int cmp(int* a, int* b) {
return *a - *b;
}
int findContentChildren(int* g, int gSize, int* s, int sSize){
if(sSize == 0)
return 0;
//将两个数组排序为升序
qsort(g, gSize, sizeof(int), cmp);
qsort(s, sSize, sizeof(int), cmp);
int count = 0;
int start = sSize - 1;
for(int i = gSize - 1; i >= 0; i--) {
if(start >= 0 && s[start] >= g[i] ) {
start--;
count++;
}
}
return count;
}
```
### Scala
```scala

View File

@ -47,6 +47,8 @@ dp[i][j]以i-1为结尾的字符串word1和以j-1位结尾的字符串word
那最后当然是取最小值所以当word1[i - 1] 与 word2[j - 1]不相同的时候递推公式dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1});
因为dp[i - 1][j - 1] + 1等于 dp[i - 1][j] 或 dp[i][j - 1]所以递推公式可简化为dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
3. dp数组如何初始化
@ -90,7 +92,7 @@ public:
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1});
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
}
}
}

View File

@ -1240,6 +1240,94 @@ class MyLinkedList() {
}
```
Rust:
```rust
#[derive(Debug)]
pub struct MyLinkedList {
pub val: i32,
pub next: Option<Box<MyLinkedList>>,
}
impl MyLinkedList {
fn new() -> Self {
// 增加头节点
MyLinkedList { val: 0, next: None }
}
fn get(&self, index: i32) -> i32 {
if index < 0 {
return -1;
}
let mut i = 0;
let mut cur = &self.next;
while let Some(node) = cur {
if i == index {
return node.val;
}
i += 1;
cur = &node.next;
}
-1
}
fn add_at_head(&mut self, val: i32) {
let new_node = Box::new(MyLinkedList {
val,
next: self.next.take(),
});
self.next = Some(new_node);
}
fn add_at_tail(&mut self, val: i32) {
let new_node = Box::new(MyLinkedList { val, next: None });
let mut last_node = &mut self.next;
while let Some(node) = last_node {
last_node = &mut node.next;
}
*last_node = Some(new_node);
}
fn add_at_index(&mut self, index: i32, val: i32) {
if index <= 0 {
self.add_at_head(val);
} else {
let mut i = 0;
let mut cur = &mut self.next;
while let Some(node) = cur {
if i + 1 == index {
let new_node = Box::new(MyLinkedList {
val,
next: node.next.take(),
});
node.next = Some(new_node);
break;
}
i += 1;
cur = &mut node.next;
}
}
}
fn delete_at_index(&mut self, index: i32) {
if index < 0 {
return;
}
let mut i = 0;
let mut cur = self;
while let Some(node) = cur.next.take() {
if i == index {
cur.next = node.next;
break;
}
i += 1;
cur.next = Some(node);
cur = cur.next.as_mut().unwrap();
}
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>