Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
programmercarl
2022-08-23 17:34:40 +08:00
25 changed files with 553 additions and 38 deletions

View File

@ -263,13 +263,15 @@ php
```php
function twoSum(array $nums, int $target): array
{
for ($i = 0; $i < count($nums);$i++) {
// 计算剩下的数
$residue = $target - $nums[$i];
// 匹配的index有则返回index 无则返回false
$match_index = array_search($residue, $nums);
if ($match_index !== false && $match_index != $i) {
return array($i, $match_index);
$map = [];
foreach($nums as $i => $num) {
if (isset($map[$target - $num])) {
return [
$i,
$map[$target - $num]
];
} else {
$map[$num] = $i;
}
}
return [];

View File

@ -493,6 +493,9 @@ function threeSum(nums: number[]): number[][] {
right: number = length - 1;
let resArr: number[][] = [];
for (let i = 0; i < length; i++) {
if (nums[i]>0) {
return resArr; //nums经过排序后只要nums[i]>0, 此后的nums[i] + nums[left] + nums[right]均大于0,可以提前终止循环。
}
if (i > 0 && nums[i] === nums[i - 1]) {
continue;
}

View File

@ -191,18 +191,20 @@ TypeScript:
```typescript
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
let newHead: ListNode | null = new ListNode(0, head);
let slowNode: ListNode | null = newHead,
fastNode: ListNode | null = newHead;
for (let i = 0; i < n; i++) {
fastNode = fastNode.next;
//根据leetcode题目的定义可推断这里快慢指针均不需要定义为ListNode | null。
let slowNode: ListNode = newHead;
let fastNode: ListNode = newHead;
while(n--) {
fastNode = fastNode.next!; //由虚拟头节点前进n个节点时,fastNode.next可推断不为null。
}
while (fastNode.next) {
while(fastNode.next) { //遍历直至fastNode.next = null 即尾部节点。 此时slowNode指向倒数第n个节点。
fastNode = fastNode.next;
slowNode = slowNode.next;
slowNode = slowNode.next!;
}
slowNode.next = slowNode.next.next;
slowNode.next = slowNode.next!.next; //倒数第n个节点可推断其next节点不为空。
return newHead.next;
};
}
```
版本二(计算节点总数法):

View File

@ -337,5 +337,48 @@ object Solution {
}
```
PHP:
```php
//虚拟头结点
function swapPairs($head) {
if ($head == null || $head->next == null) {
return $head;
}
$dummyNode = new ListNode(0, $head);
$preNode = $dummyNode; //虚拟头结点
$curNode = $head;
$nextNode = $head->next;
while($curNode && $nextNode) {
$nextNextNode = $nextNode->next; //存下一个节点
$nextNode->next = $curNode; //交换curHead 和 nextHead
$curNode->next = $nextNextNode;
$preNode->next = $nextNode; //上一个节点的下一个指向指向nextHead
//更新当前的几个指针
$preNode = $preNode->next->next;
$curNode = $nextNextNode;
$nextNode = $nextNextNode->next;
}
return $dummyNode->next;
}
//递归版本
function swapPairs($head)
{
// 终止条件
if ($head === null || $head->next === null) {
return $head;
}
//结果要返回的头结点
$next = $head->next;
$head->next = $this->swapPairs($next->next); //当前头结点->next指向更新
$next->next = $head; //当前第二个节点的->next指向更新
return $next; //返回翻转后的头结点
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -329,5 +329,31 @@ object Solution {
}
```
### Rust
```Rust
impl Solution {
fn max(a: i32, b: i32) -> i32 {
if a > b { a } else { b }
}
pub fn merge(intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut intervals = intervals;
let mut result = Vec::new();
if intervals.len() == 0 { return result; }
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
result.push(intervals[0].clone());
for i in 1..intervals.len() {
if result.last_mut().unwrap()[1] >= intervals[i][0] {
result.last_mut().unwrap()[1] = Self::max(result.last_mut().unwrap()[1], intervals[i][1]);
} else {
result.push(intervals[i].clone());
}
}
result
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -114,7 +114,7 @@ vector<vector<int>> result; // 存放符合条件结果的集合
vector<int> path; // 用来存放符合条件结果
```
其实不定义这两个全局遍历也是可以的,把这两个变量放进递归函数的参数里,但函数里参数太多影响可读性,所以我定义全局变量了。
其实不定义这两个全局变量也是可以的,把这两个变量放进递归函数的参数里,但函数里参数太多影响可读性,所以我定义全局变量了。
函数里一定有两个参数既然是集合n里面取k的数那么n和k是两个int型的参数。

View File

@ -252,6 +252,24 @@ function numTrees(n: number): number {
};
```
### Rust
```Rust
impl Solution {
pub fn num_trees(n: i32) -> i32 {
let n = n as usize;
let mut dp = vec![0; n + 1];
dp[0] = 1;
for i in 1..=n {
for j in 1..=i {
dp[i] += dp[j - 1] * dp[i - j];
}
}
dp[n]
}
}
```
### C
```c

View File

@ -240,6 +240,20 @@ Go
JavaScript
> 递归法
```javascript
var isSameTree = function (p, q) {
if (p == null && q == null)
return true;
if (p == null || q == null)
return false;
if (p.val != q.val)
return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};
```
TypeScript:
> 递归法-先序遍历

View File

@ -405,6 +405,55 @@ function canCompleteCircuit(gas: number[], cost: number[]): number {
};
```
### Rust
贪心算法:方法一
```Rust
impl Solution {
pub fn can_complete_circuit(gas: Vec<i32>, cost: Vec<i32>) -> i32 {
let mut cur_sum = 0;
let mut min = i32::MAX;
for i in 0..gas.len() {
let rest = gas[i] - cost[i];
cur_sum += rest;
if cur_sum < min { min = cur_sum; }
}
if cur_sum < 0 { return -1; }
if min > 0 { return 0; }
for i in (0..gas.len()).rev() {
let rest = gas[i] - cost[i];
min += rest;
if min >= 0 { return i as i32; }
}
-1
}
}
```
贪心算法:方法二
```Rust
impl Solution {
pub fn can_complete_circuit(gas: Vec<i32>, cost: Vec<i32>) -> i32 {
let mut cur_sum = 0;
let mut total_sum = 0;
let mut start = 0;
for i in 0..gas.len() {
cur_sum += gas[i] - cost[i];
total_sum += gas[i] - cost[i];
if cur_sum < 0 {
start = i + 1;
cur_sum = 0;
}
}
if total_sum < 0 { return -1; }
start as i32
}
}
```
### C
贪心算法:方法一

View File

@ -385,7 +385,8 @@ function removeElements(head: ListNode | null, val: number): ListNode | null {
if (cur.val === val) {
pre.next = cur.next;
} else {
pre = pre.next;
//此处不加类型断言时编译器会认为pre类型为ListNode, pre.next类型为ListNode | null
pre = pre.next as ListNode;
}
cur = cur.next;
}

View File

@ -190,9 +190,9 @@ public:
if (cur == NULL) return vector<int>{0, 0};
vector<int> left = robTree(cur->left);
vector<int> right = robTree(cur->right);
// 偷cur
int val1 = cur->val + left[0] + right[0];
// 不偷cur
// 偷cur,那么就不能偷左右节点。
int val1 = cur->val + left[1] + right[1];
// 不偷cur,那么可以偷也可以不偷左右节点,则取较大的情况
int val2 = max(left[0], left[1]) + max(right[0], right[1]);
return {val2, val1};
}

View File

@ -259,6 +259,21 @@ func max(a,b int) int{
}
```
### Rust
```rust
pub fn integer_break(n: i32) -> i32 {
let n = n as usize;
let mut dp = vec![0; n + 1];
dp[2] = 1;
for i in 3..=n {
for j in 1..i-1 {
dp[i] = dp[i].max((i - j) * j).max(dp[i - j] * j);
}
}
dp[n] as i32
}
```
### Javascript
```Javascript
var integerBreak = function(n) {
@ -299,6 +314,27 @@ function integerBreak(n: number): number {
};
```
### Rust
```Rust
impl Solution {
fn max(a: i32, b: i32) -> i32{
if a > b { a } else { b }
}
pub fn integer_break(n: i32) -> i32 {
let n = n as usize;
let mut dp = vec![0; n + 1];
dp[2] = 1;
for i in 3..=n {
for j in 1..i - 1 {
dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32));
}
}
dp[n]
}
}
```
### C
```c

View File

@ -266,18 +266,17 @@ class Solution:
### Go
**贪心**
```golang
func wiggleMaxLength(nums []int) int {
var count,preDiff,curDiff int
count=1
var count, preDiff, curDiff int //初始化默认为0
count = 1 // 初始化为1因为最小的序列是1个数
if len(nums) < 2 {
return count
}
for i := 0; i < len(nums)-1; i++ {
curDiff = nums[i+1] - nums[i]
//如果有正有负则更新下标值||或者只有前一个元素为0针对两个不等元素的序列也视作摆动序列且摆动长度为2
if (curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0){
preDiff=curDiff
if (curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0) {
count++
}
}
@ -285,6 +284,43 @@ func wiggleMaxLength(nums []int) int {
}
```
**动态规划**
```golang
func wiggleMaxLength(nums []int) int {
n := len(nums)
if n <= 1 {
return n
}
dp := make([][2]int, n)
// i 0 作为波峰的最大长度
// i 1 作为波谷的最大长度
dp[0][0] = 1
dp[0][1] = 1
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
if nums[j] > nums[i] { //nums[i]为波谷
dp[i][1] = max(dp[i][1], dp[j][0]+1)
}
if nums[j] < nums[i] { //nums[i]为波峰 或者相等
dp[i][0] = max(dp[i][0], dp[j][1]+1)
}
if nums[j] == nums[i] { //添加一种情况nums[i]为相等
dp[i][0] = max(dp[i][0], dp[j][0]) //波峰
dp[i][1] = max(dp[i][1], dp[j][1]) //波谷
}
}
}
return max(dp[n-1][0], dp[n-1][1])
}
func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
```
### Javascript
**贪心**
```Javascript

View File

@ -290,6 +290,26 @@ var reconstructQueue = function(people) {
};
```
### Rust
```Rust
impl Solution {
pub fn reconstruct_queue(people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut people = people;
people.sort_by(|a, b| {
if a[0] == b[0] { return a[1].cmp(&b[1]); }
b[0].cmp(&a[0])
});
let mut que: Vec<Vec<i32>> = Vec::new();
que.push(people[0].clone());
for i in 1..people.len() {
let position = people[i][1];
que.insert(position as usize, people[i].clone());
}
que
}
}
```
### C
```c

View File

@ -417,7 +417,32 @@ var canPartition = function(nums) {
```
### Rust
```Rust
impl Solution {
fn max(a: usize, b: usize) -> usize {
if a > b { a } else { b }
}
pub fn can_partition(nums: Vec<i32>) -> bool {
let nums = nums.iter().map(|x| *x as usize).collect::<Vec<usize>>();
let mut sum = 0;
let mut dp: Vec<usize> = vec![0; 10001];
for i in 0..nums.len() {
sum += nums[i];
}
if sum % 2 == 1 { return false; }
let target = sum / 2;
for i in 0..nums.len() {
for j in (nums[i]..=target).rev() {
dp[j] = Self::max(dp[j], dp[j - nums[i]] + nums[i]);
}
}
if dp[target] == target { return true; }
false
}
}
```
### C:

View File

@ -374,7 +374,26 @@ object Solution {
}
```
### Rust
```Rust
impl Solution {
pub fn erase_overlap_intervals(intervals: Vec<Vec<i32>>) -> i32 {
if intervals.len() == 0 { return 0; }
let mut intervals = intervals;
intervals.sort_by(|a, b| a[1].cmp(&b[1]));
let mut count = 1;
let mut end = intervals[0][1];
for i in 1..intervals.len() {
if end <= intervals[i][0] {
end = intervals[i][1];
count += 1;
}
}
intervals.len() as i32 - count
}
}
```
-----------------------

View File

@ -221,8 +221,46 @@ function maxProfit(prices: number[], fee: number): number {
};
```
Rust:
**贪心**
```Rust
impl Solution {
pub fn max_profit(prices: Vec<i32>, fee: i32) -> i32 {
let mut result = 0;
let mut min_price = prices[0];
for i in 1..prices.len() {
if prices[i] < min_price { min_price = prices[i]; }
// if prices[i] >= min_price && prices[i] <= min_price + fee { continue; }
if prices[i] > min_price + fee {
result += prices[i] - min_price - fee;
min_price = prices[i] - fee;
}
}
result
}
}
```
**动态规划**
```Rust
impl Solution {
fn max(a: i32, b: i32) -> i32 {
if a > b { a } else { b }
}
pub fn max_profit(prices: Vec<i32>, fee: i32) -> i32 {
let n = prices.len();
let mut dp = vec![vec![0; 2]; n];
dp[0][0] -= prices[0];
for i in 1..n {
dp[i][0] = Self::max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = Self::max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
}
Self::max(dp[n - 1][0], dp[n - 1][1])
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -278,5 +278,26 @@ object Solution {
}
```
### Rust
```Rust
impl Solution {
pub fn monotone_increasing_digits(n: i32) -> i32 {
let mut str_num = n.to_string().chars().map(|x| x.to_digit(10).unwrap() as i32).collect::<Vec<i32>>();
let mut flag = str_num.len();
for i in (1..str_num.len()).rev() {
if str_num[i - 1] > str_num[i] {
flag = i;
str_num[i - 1] -= 1;
}
}
for i in flag..str_num.len() {
str_num[i] = 9;
}
str_num.iter().fold(0, |acc, x| acc * 10 + x)
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -343,6 +343,34 @@ object Solution {
}
```
### Rust
```Rust
use std::collections::HashMap;
impl Solution {
fn max (a: usize, b: usize) -> usize {
if a > b { a } else { b }
}
pub fn partition_labels(s: String) -> Vec<i32> {
let s = s.chars().collect::<Vec<char>>();
let mut hash: HashMap<char, usize> = HashMap::new();
for i in 0..s.len() {
hash.insert(s[i], i);
}
let mut result: Vec<i32> = Vec::new();
let mut left: usize = 0;
let mut right: usize = 0;
for i in 0..s.len() {
right = Self::max(right, hash[&s[i]]);
if i == right {
result.push((right - left + 1) as i32);
left = i + 1;
}
}
result
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -251,6 +251,38 @@ var lemonadeChange = function(bills) {
};
```
### Rust
```Rust
impl Solution {
pub fn lemonade_change(bills: Vec<i32>) -> bool {
let mut five = 0;
let mut ten = 0;
// let mut twenty = 0;
for bill in bills {
if bill == 5 { five += 1; }
if bill == 10 {
if five <= 0 { return false; }
ten += 1;
five -= 1;
}
if bill == 20 {
if five > 0 && ten > 0 {
five -= 1;
ten -= 1;
// twenty += 1;
} else if five >= 3 {
five -= 3;
// twenty += 1;
} else { return false; }
}
}
true
}
}
```
### C
```c
bool lemonadeChange(int* bills, int billsSize){

View File

@ -147,6 +147,59 @@ class Solution {
}
```
### java
```java
//方法一:采用额外的数组空间
class Solution {
public int[] sortArrayByParityII(int[] nums) {
//定义结果数组 result
int[] result = new int[nums.length];
int even = 0, odd = 1;
for(int i = 0; i < nums.length; i++){
//如果为偶数
if(nums[i] % 2 == 0){
result[even] = nums[i];
even += 2;
}else{
result[odd] = nums[i];
odd += 2;
}
}
return result;
}
}
```
```java
//方法二:不采用额外的数组空间
class Solution922 {
public int[] sortArrayByParityII(int[] nums) {
//定义双指针
int oddPoint = 1, evenPoint = 0;
//开始移动并交换,最后一层必然为相互交换后再移动或者相同直接移动
while(oddPoint < nums.length && evenPoint < nums.length){
//进行判断
if(nums[oddPoint] % 2 == 0 && nums[evenPoint] % 2 == 1){ //如果均不满足
int temp = 0;
temp = nums[oddPoint];
nums[oddPoint] = nums[evenPoint];
nums[evenPoint] = temp;
oddPoint += 2;
evenPoint += 2;
}else if(nums[oddPoint] % 2 == 0 && nums[evenPoint] % 2 == 0){ //偶数满足
evenPoint += 2;
}else if(nums[oddPoint] % 2 == 1 && nums[evenPoint] % 2 == 1){ //奇数满足
oddPoint += 2;
}else{
oddPoint += 2;
evenPoint += 2;
}
}
return nums;
}
}
```
### Python3
```python

View File

@ -186,7 +186,7 @@ TypeScript:
```typescript
class ListNode {
public val: number;
public next: ListNode = null;
public next: ListNode|null = null;
constructor(value: number) {
this.val = value;
this.next = null;

View File

@ -7,6 +7,8 @@
# 面试题 02.07. 链表相交
160.链表相交
[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/)
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

View File

@ -0,0 +1,25 @@
```go
func countNodes(root *TreeNode) int {
if root == nil {
return 0
}
q := list.New()
q.PushBack(root)
res := 0
for q.Len() > 0 {
n := q.Len()
for i := 0; i < n; i++ {
node := q.Remove(q.Front()).(*TreeNode)
if node.Left != nil {
q.PushBack(node.Left)
}
if node.Right != nil {
q.PushBack(node.Right)
}
res++
}
}
return res
}
```

View File

@ -0,0 +1,22 @@
```go
func maxDepth(root *Node) int {
if root == nil {
return 0
}
q := list.New()
q.PushBack(root)
depth := 0
for q.Len() > 0 {
n := q.Len()
for i := 0; i < n; i++ {
node := q.Remove(q.Front()).(*Node)
for j := range node.Children {
q.PushBack(node.Children[j])
}
}
depth++
}
return depth
}
```