mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -493,6 +493,9 @@ function threeSum(nums: number[]): number[][] {
|
|||||||
right: number = length - 1;
|
right: number = length - 1;
|
||||||
let resArr: number[][] = [];
|
let resArr: number[][] = [];
|
||||||
for (let i = 0; i < length; i++) {
|
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]) {
|
if (i > 0 && nums[i] === nums[i - 1]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -191,18 +191,20 @@ TypeScript:
|
|||||||
```typescript
|
```typescript
|
||||||
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
|
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
|
||||||
let newHead: ListNode | null = new ListNode(0, head);
|
let newHead: ListNode | null = new ListNode(0, head);
|
||||||
let slowNode: ListNode | null = newHead,
|
//根据leetcode题目的定义可推断这里快慢指针均不需要定义为ListNode | null。
|
||||||
fastNode: ListNode | null = newHead;
|
let slowNode: ListNode = newHead;
|
||||||
for (let i = 0; i < n; i++) {
|
let fastNode: ListNode = newHead;
|
||||||
fastNode = fastNode.next;
|
|
||||||
|
while(n--) {
|
||||||
|
fastNode = fastNode.next!; //由虚拟头节点前进n个节点时,fastNode.next可推断不为null。
|
||||||
}
|
}
|
||||||
while (fastNode.next) {
|
while(fastNode.next) { //遍历直至fastNode.next = null, 即尾部节点。 此时slowNode指向倒数第n个节点。
|
||||||
fastNode = fastNode.next;
|
fastNode = fastNode.next;
|
||||||
slowNode = slowNode.next;
|
slowNode = slowNode.next!;
|
||||||
}
|
}
|
||||||
slowNode.next = slowNode.next.next;
|
slowNode.next = slowNode.next!.next; //倒数第n个节点可推断其next节点不为空。
|
||||||
return newHead.next;
|
return newHead.next;
|
||||||
};
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
版本二(计算节点总数法):
|
版本二(计算节点总数法):
|
||||||
|
@ -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
|
||||||
|
|
||||||
```c
|
```c
|
||||||
|
@ -385,7 +385,8 @@ function removeElements(head: ListNode | null, val: number): ListNode | null {
|
|||||||
if (cur.val === val) {
|
if (cur.val === val) {
|
||||||
pre.next = cur.next;
|
pre.next = cur.next;
|
||||||
} else {
|
} else {
|
||||||
pre = pre.next;
|
//此处不加类型断言时:编译器会认为pre类型为ListNode, pre.next类型为ListNode | null
|
||||||
|
pre = pre.next as ListNode;
|
||||||
}
|
}
|
||||||
cur = cur.next;
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
|
@ -299,6 +299,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
|
||||||
|
|
||||||
```c
|
```c
|
||||||
|
@ -266,22 +266,58 @@ class Solution:
|
|||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
|
**贪心**
|
||||||
```golang
|
```golang
|
||||||
func wiggleMaxLength(nums []int) int {
|
func wiggleMaxLength(nums []int) int {
|
||||||
var count,preDiff,curDiff int
|
var count, preDiff, curDiff int //初始化默认为0
|
||||||
count=1
|
count = 1 // 初始化为1,因为最小的序列是1个数
|
||||||
if len(nums)<2{
|
if len(nums) < 2 {
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
for i:=0;i<len(nums)-1;i++{
|
for i := 0; i < len(nums)-1; i++ {
|
||||||
curDiff=nums[i+1]-nums[i]
|
curDiff = nums[i+1] - nums[i]
|
||||||
//如果有正有负则更新下标值||或者只有前一个元素为0(针对两个不等元素的序列也视作摆动序列,且摆动长度为2)
|
if (curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0) {
|
||||||
if (curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0){
|
count++
|
||||||
preDiff=curDiff
|
}
|
||||||
count++
|
}
|
||||||
}
|
return count
|
||||||
}
|
}
|
||||||
return count
|
```
|
||||||
|
|
||||||
|
**动态规划**
|
||||||
|
```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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -186,7 +186,7 @@ TypeScript:
|
|||||||
```typescript
|
```typescript
|
||||||
class ListNode {
|
class ListNode {
|
||||||
public val: number;
|
public val: number;
|
||||||
public next: ListNode = null;
|
public next: ListNode|null = null;
|
||||||
constructor(value: number) {
|
constructor(value: number) {
|
||||||
this.val = value;
|
this.val = value;
|
||||||
this.next = null;
|
this.next = null;
|
||||||
|
Reference in New Issue
Block a user