From e5d694da4d071b1286eb22db2477f55bb91b0cf6 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: 2023年7月19日 10:33:50 +0800 Subject: [PATCH 01/13] Update --- ...275円221円345円205円254円345円217円270円346円200円273円347円273円223円.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/345円211円215円345円272円217円/346円267円261円345円234円263円344円272円222円350円201円224円347円275円221円345円205円254円345円217円270円346円200円273円347円273円223円.md" "b/problems/345円211円215円345円272円217円/346円267円261円345円234円263円344円272円222円350円201円224円347円275円221円345円205円254円345円217円270円346円200円273円347円273円223円.md" index f8c07016a4..52a8448b51 100644 --- "a/problems/345円211円215円345円272円217円/346円267円261円345円234円263円344円272円222円350円201円224円347円275円221円345円205円254円345円217円270円346円200円273円347円273円223円.md" +++ "b/problems/345円211円215円345円272円217円/346円267円261円345円234円263円344円272円222円350円201円224円347円275円221円345円205円254円345円217円270円346円200円273円347円273円223円.md" @@ -36,7 +36,7 @@ * 微众银行(总部深圳) * 招银科技(总部深圳) * 平安系列(平安科技、平安寿险、平安产险、平安金融、平安好医生等) -* Shopee(东南亚最大的电商平台,最近发展势头非常强劲) +* Shopee(21年有裁员风波) * 有赞(深圳) * 迅雷(总部深圳) * 金蝶(总部深圳) From b3d1a88fde09afa258b64812cf776674a024c534 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 20:38:47 +0800 Subject: [PATCH 02/13] =?UTF-8?q?Update=200503.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4345円244円247円345円205円203円347円264円240円II.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" "b/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" index 3fd4b3b6db..a090f32c3e 100644 --- "a/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" +++ "b/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" @@ -266,6 +266,24 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` +Rust +```rust +impl Solution { + pub fn next_greater_elements(nums: Vec) -> Vec { + let mut ans = vec![-1; nums.len() * 2]; + let mut stack = vec![]; + let double = nums.repeat(2); + for (idx, &i) in double.iter().enumerate() { + while !stack.is_empty() && double[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + ans[pos] = i; + } + stack.push(idx); + } + ans.into_iter().take(nums.len()).collect() + } +} +```

From f3f549d317113718f61509a85937853d3a81a7f7 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 22:13:12 +0800 Subject: [PATCH 03/13] =?UTF-8?q?Update=200042.=E6=8E=A5=E9=9B=A8=E6=B0=B4?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.346円216円245円351円233円250円346円260円264円.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git "a/problems/0042.346円216円245円351円233円250円346円260円264円.md" "b/problems/0042.346円216円245円351円233円250円346円260円264円.md" index db66095da2..833a7613e2 100644 --- "a/problems/0042.346円216円245円351円233円250円346円260円264円.md" +++ "b/problems/0042.346円216円245円351円233円250円346円260円264円.md" @@ -926,6 +926,56 @@ int trap(int* height, int heightSize) { * 空间复杂度 O(1) +Rust + +双指针 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let n = height.len(); + let mut max_left = vec![0; height.len()]; + let mut max_right = vec![0; height.len()]; + max_left.iter_mut().zip(max_right.iter_mut().rev()).enumerate().fold((0, 0), |(lm, rm), (idx, (x, y))| { + let lmax = lm.max(height[idx]); + let rmax = rm.max(height[n - 1 - idx]); + *x = lmax; *y = rmax; + (lmax, rmax) + }); + height.iter().enumerate().fold(0, |acc, (idx, x)| { + let h = max_left[idx].min(max_right[idx]); + if h> 0 { h - x + acc } else { acc } + }) + } +} +``` + +单调栈 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let mut stack = vec![]; + let mut ans = 0; + for (right_pos, &right_h) in height.iter().enumerate() { + while !stack.is_empty() && height[*stack.last().unwrap()] <= right_h { + let mid_pos = stack.pop().unwrap(); + if !stack.is_empty() { + let left_pos = *stack.last().unwrap(); + let left_h = height[left_pos]; + let top = std::cmp::min(left_h, right_h); + if top> height[mid_pos] { + ans += (top - height[mid_pos]) * (right_pos - left_pos - 1) as i32; + } + } + } + stack.push(right_pos); + } + ans + } +} +``` +

From 7aa2a3efea391bc30047c31d17b179fcdb99221d Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sun, 9 Jul 2023 14:30:01 +0800 Subject: [PATCH 04/13] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6=E5=9B=BE?= =?UTF-8?q?=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47347円232円204円347円237円251円345円275円242円.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git "a/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" "b/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" index f9a8350818..bc82a860cc 100644 --- "a/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" +++ "b/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" @@ -670,6 +670,61 @@ function largestRectangleArea(heights: number[]): number { ``` +Rust + +双指针预处理 +```rust + +impl Solution { + pub fn largest_rectangle_area(v: Vec) -> i32 { + let n = v.len(); + let mut left_smaller_idx = vec![-1; n]; + let mut right_smaller_idx = vec![n as i32; n]; + for i in 1..n { + let mut mid = i as i32 - 1; + while mid>= 0 && v[mid as usize]>= v[i] { + mid = left_smaller_idx[mid as usize]; + } + left_smaller_idx[i] = mid; + } + for i in (0..n-1).rev() { + let mut mid = i + 1; + while mid < n && v[mid]>= v[i] { + mid = right_smaller_idx[mid] as usize; + } + right_smaller_idx[i] = mid as i32; + } + let mut res = 0; + for (idx, &e) in v.iter().enumerate() { + res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); + } + dbg!(res) + } +} +``` + +单调栈 +```rust +impl Solution { + pub fn largest_rectangle_area1(mut v: Vec) -> i32 { + v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 + v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 + let mut res = 0; + let mut stack = vec![]; // 递增的栈 + for (idx, &e) in v.iter().enumerate() { + while !stack.is_empty() && v[*stack.last().unwrap()]> e { + let pos = stack.pop().unwrap(); + let prev_pos = *stack.last().unwrap(); + let s = (idx - prev_pos - 1) as i32 * v[pos]; + res = res.max(s); + } + stack.push(idx); + } + res + } +} +``` +

From 0dc6bb5669981d25069fc338566e73b3a8e97828 Mon Sep 17 00:00:00 2001 From: han Date: 2023年7月25日 16:24:55 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346円227円213円347円237円251円351円230円265円II.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0059.350円236円272円346円227円213円347円237円251円351円230円265円II.md" "b/problems/0059.350円236円272円346円227円213円347円237円251円351円230円265円II.md" index f03fcdad35..73e9e4daea 100644 --- "a/problems/0059.350円236円272円346円227円213円347円237円251円351円230円265円II.md" +++ "b/problems/0059.350円236円272円346円227円213円347円237円251円351円230円265円II.md" @@ -688,6 +688,58 @@ public class Solution { } ``` +### Ruby#: +```ruby +def generate_matrix(n) + result = Array.new(n) { Array.new(n, 0) } + #循环次数 + loop_times = 0 + #步长 + step = n - 1 + val = 1 + + + while loop_times < n / 2 + #模拟从左向右 + for i in 0..step - 1 + #行数不变,列数变 + result[loop_times][i+loop_times] = val + val += 1 + end + + #模拟从上到下 + for i in 0..step - 1 + #列数不变,行数变 + result[i+loop_times][n-loop_times-1] = val + val += 1 + end + + #模拟从右到左 + for i in 0..step - 1 + #行数不变,列数变 + result[n-loop_times-1][n-loop_times-i-1] = val + val += 1 + end + + #模拟从下到上 + for i in 0..step - 1 + #列数不变,行数变 + result[n-loop_times-i-1][loop_times] = val + val += 1 + end + + loop_times += 1 + step -= 2 + end + + #如果是奇数,则填充最后一个元素 + result[n/2][n/2] = n**2 if n % 2 + + return result + +end +``` +

From 7c9fcfe09d91fa4e5bc66ee6ab16fac7446426e0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: 2023年7月27日 14:12:11 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=20O(n)=E8=B6=85=E6=97=B6=20?= =?UTF-8?q?=E6=8B=8D=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57345円244円232円345円244円247円357円274円237円.md" | 22 +++++-------------- ...50350円277円231円351円207円214円357円274円201円.md" | 16 ++++---------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git "a/problems/O(n)347円232円204円347円256円227円346円263円225円345円261円205円347円204円266円350円266円205円346円227円266円344円272円206円357円274円214円346円255円244円346円227円266円347円232円204円n347円251円266円347円253円237円346円230円257円345円244円232円345円244円247円357円274円237円.md" "b/problems/O(n)347円232円204円347円256円227円346円263円225円345円261円205円347円204円266円350円266円205円346円227円266円344円272円206円357円274円214円346円255円244円346円227円266円347円232円204円n347円251円266円347円253円237円346円230円257円345円244円232円345円244円247円357円274円237円.md" index a488c0bad4..8be48f38c2 100644 --- "a/problems/O(n)347円232円204円347円256円227円346円263円225円345円261円205円347円204円266円350円266円205円346円227円266円344円272円206円357円274円214円346円255円244円346円227円266円347円232円204円n347円251円266円347円253円237円346円230円257円345円244円232円345円244円247円357円274円237円.md" +++ "b/problems/O(n)347円232円204円347円256円227円346円263円225円345円261円205円347円204円266円350円266円205円346円227円266円344円272円206円357円274円214円346円255円244円346円227円266円347円232円204円n347円251円266円347円253円237円346円230円257円345円244円232円345円244円247円357円274円237円.md" @@ -13,7 +13,7 @@ 计算机究竟1s可以执行多少次操作呢? 接下来探讨一下这个问题。 -# 超时是怎么回事 +## 超时是怎么回事 ![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117.png) @@ -25,7 +25,7 @@ 如果n的规模已经足够让O(n)的算法运行时间超过了1s,就应该考虑log(n)的解法了。 -# 从硬件配置看计算机的性能 +## 从硬件配置看计算机的性能 计算机的运算速度主要看CPU的配置,以2015年MacPro为例,CPU配置:2.7 GHz Dual-Core Intel Core i5 。 @@ -44,7 +44,7 @@ 所以我们的程序在计算机上究竟1s真正能执行多少次操作呢? -# 做个测试实验 +## 做个测试实验 在写测试程序测1s内处理多大数量级数据的时候,有三点需要注意: @@ -155,7 +155,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 -# 完整测试代码 +## 完整测试代码 ```CPP #include @@ -212,7 +212,7 @@ int main() { ``` -# 总结 +## 总结 本文详细分析了在leetcode上做题程序为什么会有超时,以及从硬件配置上大体知道CPU的执行速度,然后亲自做一个实验来看看O(n)的算法,跑一秒钟,这个n究竟是做大,最后给出不同时间复杂度,一秒内可以运算出来的n的大小。 @@ -220,17 +220,6 @@ int main() { 这样,大家应该对程序超时时候的数据规模有一个整体的认识了。 -## 其他语言版本 - - -Java: - - -Python: - - -Go: - @@ -238,3 +227,4 @@ Go: + diff --git "a/problems/345円205円263円344円272円216円346円227円266円351円227円264円345円244円215円346円235円202円345円272円246円357円274円214円344円275円240円344円270円215円347円237円245円351円201円223円347円232円204円351円203円275円345円234円250円350円277円231円351円207円214円357円274円201円.md" "b/problems/345円205円263円344円272円216円346円227円266円351円227円264円345円244円215円346円235円202円345円272円246円357円274円214円344円275円240円344円270円215円347円237円245円351円201円223円347円232円204円351円203円275円345円234円250円350円277円231円351円207円214円357円274円201円.md" index c479dddc67..95c7567c75 100644 --- "a/problems/345円205円263円344円272円216円346円227円266円351円227円264円345円244円215円346円235円202円345円272円246円357円274円214円344円275円240円344円270円215円347円237円245円351円201円223円347円232円204円351円203円275円345円234円250円350円277円231円351円207円214円357円274円201円.md" +++ "b/problems/345円205円263円344円272円216円346円227円266円351円227円264円345円244円215円346円235円202円345円272円246円357円274円214円344円275円240円344円270円215円347円237円245円351円201円223円347円232円204円351円203円275円345円234円250円350円277円231円351円207円214円357円274円201円.md" @@ -8,6 +8,8 @@ 所以重新整理的时间复杂度文章,正式和大家见面啦! +# 时间复杂度 + ## 究竟什么是时间复杂度 **时间复杂度是一个函数,它定性描述该算法的运行时间**。 @@ -145,7 +147,7 @@ O(2 ×ばつ n^2 + 10 ×ばつ n + 1000) < O(3 ×ばつ n^2),所以说最后省略掉常数项 **当然这不是这道题目的最优解,我仅仅是用这道题目来讲解一下时间复杂度**。 -# 总结 +## 总结 本篇讲解了什么是时间复杂度,复杂度是用来干什么,以及数据规模对时间复杂度的影响。 @@ -157,17 +159,6 @@ O(2 ×ばつ n^2 + 10 ×ばつ n + 1000) < O(3 ×ばつ n^2),所以说最后省略掉常数项 如果感觉「代码随想录」很不错,赶快推荐给身边的朋友同学们吧,他们发现和「代码随想录」相见恨晚! -## 其他语言版本 - - -Java: - - -Python: - - -Go: - @@ -175,3 +166,4 @@ Go: + From e0c5da76e6a616a1cf3da18059c59947a90f877d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: 2023年7月27日 14:18:47 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E6=A0=88=E7=B3=BB=E5=88=97=E9=A2=98=E7=9B=AE=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.346円216円245円351円233円250円346円260円264円.md" | 20 +++++++-------- ...47347円232円204円347円237円251円345円275円242円.md" | 25 ++++++++++--------- ...4345円244円247円345円205円203円347円264円240円I.md" | 17 +++++++------ ...345円244円247円345円205円203円347円264円240円II.md" | 19 ++++++++------ ...17346円227円245円346円270円251円345円272円246円.md" | 15 +++++------ 5 files changed, 53 insertions(+), 43 deletions(-) diff --git "a/problems/0042.346円216円245円351円233円250円346円260円264円.md" "b/problems/0042.346円216円245円351円233円250円346円260円264円.md" index 833a7613e2..4f0682ba6f 100644 --- "a/problems/0042.346円216円245円351円233円250円346円260円264円.md" +++ "b/problems/0042.346円216円245円351円233円250円346円260円264円.md" @@ -29,7 +29,7 @@ * 输出:9 -# 思路 +## 思路 接雨水问题在面试中还是常见题目的,有必要好好讲一讲。 @@ -39,7 +39,7 @@ * 动态规划 * 单调栈 -## 暴力解法 +### 暴力解法 本题暴力解法也是也是使用双指针。 @@ -137,7 +137,7 @@ public: 力扣后面修改了后台测试数据,所以以上暴力解法超时了。 -## 双指针优化 +### 双指针优化 在暴力解法中,我们可以看到只要记录左边柱子的最高高度 和 右边柱子的最高高度,就可以计算当前位置的雨水面积,这就是通过列来计算。 @@ -184,7 +184,7 @@ public: }; ``` -## 单调栈解法 +### 单调栈解法 关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -194,7 +194,7 @@ public: 而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。 -### 准备工作 +#### 准备工作 那么本题使用单调栈有如下几个问题: @@ -248,7 +248,7 @@ stack st; // 存着下标,计算的时候用下标对应的柱子高度 明确了如上几点,我们再来看处理逻辑。 -### 单调栈处理逻辑 +#### 单调栈处理逻辑 以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -596,7 +596,7 @@ class Solution: ``` -### Go +### Go: ```go func trap(height []int) int { @@ -802,7 +802,7 @@ var trap = function(height) { }; ``` -### TypeScript +### TypeScript: 暴力解法: @@ -925,8 +925,7 @@ int trap(int* height, int heightSize) { * 时间复杂度 O(n) * 空间复杂度 O(1) - -Rust +### Rust: 双指针 @@ -980,3 +979,4 @@ impl Solution { + diff --git "a/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" "b/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" index bc82a860cc..4d949941cb 100644 --- "a/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" +++ "b/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" @@ -20,7 +20,7 @@ * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 -# 思路 +## 思路 本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解! @@ -28,7 +28,7 @@ 我们先来看一下暴力解法的解法: -## 暴力解法 +### 暴力解法 ```CPP class Solution { @@ -55,7 +55,7 @@ public: 如上代码并不能通过leetcode,超时了,因为时间复杂度是$O(n^2)$。 -## 双指针解法 +### 双指针解法 本题双指针的写法整体思路和[42. 接雨水](https://programmercarl.com/0042.接雨水.html)是一致的,但要比[42. 接雨水](https://programmercarl.com/0042.接雨水.html)难一些。 @@ -98,7 +98,7 @@ public: }; ``` -## 单调栈 +### 单调栈 本地单调栈的解法和接雨水的题目是遥相呼应的。 @@ -169,7 +169,7 @@ public: } }; -``` +``` 细心的录友会发现,我在 height数组上后,都加了一个元素0, 为什么这么做呢? @@ -229,7 +229,7 @@ public: ## 其他语言版本 -Java: +### Java: 暴力解法: ```java @@ -335,7 +335,7 @@ class Solution { } ``` -Python3: +### Python3: ```python @@ -468,7 +468,7 @@ class Solution: ``` -Go: +### Go:> 单调栈 @@ -506,7 +506,8 @@ func largestRectangleArea(heights []int) int { ``` -JavaScript: +### JavaScript: + ```javascript //双指针 js中运行速度最快 var largestRectangleArea = function(heights) { @@ -581,7 +582,7 @@ var largestRectangleArea = function(heights) { return maxArea; }; ``` -TypeScript: +### TypeScript: > 暴力法(会超时): @@ -669,8 +670,7 @@ function largestRectangleArea(heights: number[]): number { }; ``` - -Rust +### Rust: 双指针预处理 ```rust @@ -730,3 +730,4 @@ impl Solution { + diff --git "a/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" "b/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" index 411a47df9b..6bcafafba2 100644 --- "a/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" +++ "b/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" @@ -37,7 +37,7 @@ nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位 * nums1和nums2中所有整数 互不相同 * nums1 中的所有整数同样出现在 nums2 中 -# 思路 +## 思路 做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html) @@ -191,7 +191,8 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 -Java +### Java + ```java class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { @@ -248,7 +249,8 @@ class Solution { } } ``` -Python3: +### Python3 + ```python class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: @@ -269,7 +271,7 @@ class Solution: return result ``` -Go: +### Go > 未精简版本 ```go @@ -335,7 +337,7 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int { } ``` -JavaScript: +### JavaScript ```JS var nextGreaterElement = function (nums1, nums2) { @@ -358,7 +360,7 @@ var nextGreaterElement = function (nums1, nums2) { }; ``` -TypeScript: +### TypeScript ```typescript function nextGreaterElement(nums1: number[], nums2: number[]): number[] { @@ -387,7 +389,7 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { }; ``` -Rust +### Rust ```rust impl Solution { @@ -419,3 +421,4 @@ impl Solution { + diff --git "a/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" "b/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" index a090f32c3e..023e4d7e0e 100644 --- "a/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" +++ "b/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" @@ -22,7 +22,7 @@ * -10^9 <= nums[i] <= 10^9 -# 思路 +## 思路 做本题之前建议先做[739. 每日温度](https://programmercarl.com/0739.每日温度.html) 和 [496.下一个更大元素 I](https://programmercarl.com/0496.下一个更大元素I.html)。 @@ -138,7 +138,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```Java class Solution { public int[] nextGreaterElements(int[] nums) { @@ -162,7 +163,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: class Solution: @@ -196,7 +198,8 @@ class Solution: stack.append(i) return ans ``` -Go: +### Go: + ```go func nextGreaterElements(nums []int) []int { length := len(nums) @@ -218,7 +221,7 @@ func nextGreaterElements(nums []int) []int { } ``` -JavaScript: +### JavaScript: ```JS /** @@ -242,7 +245,7 @@ var nextGreaterElements = function (nums) { return res; }; ``` -TypeScript: +### TypeScript: ```typescript function nextGreaterElements(nums: number[]): number[] { @@ -266,7 +269,8 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` -Rust +### Rust: + ```rust impl Solution { pub fn next_greater_elements(nums: Vec) -> Vec { @@ -290,3 +294,4 @@ impl Solution { + diff --git "a/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" "b/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" index d2da37371a..fc1a80631c 100644 --- "a/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" +++ "b/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" @@ -211,8 +211,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java class Solution { @@ -270,7 +269,8 @@ class Solution { } ``` -Python: +### Python: + > 未精简版本 ```python @@ -307,7 +307,7 @@ class Solution: return answer ``` -Go: +### Go: > 暴力法 @@ -384,7 +384,7 @@ func dailyTemperatures(num []int) []int { } ``` -JavaScript: +### JavaScript: ```javascript // 版本一 @@ -429,7 +429,7 @@ var dailyTemperatures = function(temperatures) { }; ``` -TypeScript: +### TypeScript: > 精简版: @@ -455,7 +455,7 @@ function dailyTemperatures(temperatures: number[]): number[] { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -482,3 +482,4 @@ impl Solution { + From dc9fb7cb0bc7bff583e66bf3e3699af961f60a8c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: 2023年7月27日 14:28:14 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13350円275円254円346円225円260円347円273円204円.md" | 15 ++++++------- ...3.347円247円273円345円212円250円351円233円266円.md" | 21 +++++++++---------- ...345円272円217円346円225円260円347円273円204円II.md" | 3 +-- ...61350円204円211円346円225円260円347円273円204円.md" | 17 ++++++++------- ...72347円216円260円346円254円241円346円225円260円.md" | 16 +++++++------- ...27347円232円204円346円225円260円345円255円227円.md" | 17 ++++++++------- 6 files changed, 46 insertions(+), 43 deletions(-) diff --git "a/problems/0189.346円227円213円350円275円254円346円225円260円347円273円204円.md" "b/problems/0189.346円227円213円350円275円254円346円225円260円347円273円204円.md" index 3581969439..d60612e92b 100644 --- "a/problems/0189.346円227円213円350円275円254円346円225円260円347円273円204円.md" +++ "b/problems/0189.346円227円213円350円275円254円346円225円260円347円273円204円.md" @@ -33,7 +33,7 @@ 向右旋转 2 步: [3,99,-1,-100]。 -# 思路 +## 思路 这道题目在字符串里其实很常见,我把字符串反转相关的题目列一下: @@ -83,9 +83,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -106,7 +106,7 @@ class Solution { } ``` -## Python +### Python 方法一:局部翻转 + 整体翻转 ```python @@ -139,7 +139,7 @@ class Solution: # 备注:这个方法会导致空间复杂度变成 O(n) 因为我们要创建一个 copy 数组。但是不失为一种思路。 ``` -## Go +### Go ```go func rotate(nums []int, k int) { @@ -157,7 +157,7 @@ func reverse(nums []int){ } ``` -## JavaScript +### JavaScript ```js var rotate = function (nums, k) { @@ -178,7 +178,7 @@ var rotate = function (nums, k) { }; ``` -## TypeScript +### TypeScript ```typescript function rotate(nums: number[], k: number): void { @@ -205,3 +205,4 @@ function reverseByRange(nums: number[], left: number, right: number): void { + diff --git "a/problems/0283.347円247円273円345円212円250円351円233円266円.md" "b/problems/0283.347円247円273円345円212円250円351円233円266円.md" index 22d6428c10..ee3f429150 100644 --- "a/problems/0283.347円247円273円345円212円250円351円233円266円.md" +++ "b/problems/0283.347円247円273円345円212円250円351円233円266円.md" @@ -3,10 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- -# 动态规划:一样的套路,再求一次完全平方数 - -# 283. 移动零 +# 283. 移动零:动态规划:一样的套路,再求一次完全平方数 [力扣题目链接](https://leetcode.cn/problems/move-zeroes/) @@ -22,7 +19,7 @@ 尽量减少操作次数。 -# 思路 +## 思路 做这道题目之前,大家可以做一做[27.移除元素](https://programmercarl.com/0027.移除元素.html) @@ -58,9 +55,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java public void moveZeroes(int[] nums) { @@ -77,7 +74,7 @@ public void moveZeroes(int[] nums) { } ``` -Python: +### Python: ```python def moveZeroes(self, nums: List[int]) -> None: @@ -100,7 +97,7 @@ Python: fast += 1 ``` -Go: +### Go: ```go func moveZeroes(nums []int) { @@ -116,7 +113,8 @@ func moveZeroes(nums []int) { } ``` -JavaScript: +### JavaScript: + ```javascript var moveZeroes = function(nums) { let slow = 0; @@ -133,7 +131,7 @@ var moveZeroes = function(nums) { }; ``` -TypeScript: +### TypeScript: ```typescript function moveZeroes(nums: number[]): void { @@ -159,3 +157,4 @@ function moveZeroes(nums: number[]): void { + diff --git "a/problems/0922.346円214円211円345円245円207円345円201円266円346円216円222円345円272円217円346円225円260円347円273円204円II.md" "b/problems/0922.346円214円211円345円245円207円345円201円266円346円216円222円345円272円217円346円225円260円347円273円204円II.md" index c4654f16d1..72be8fa732 100644 --- "a/problems/0922.346円214円211円345円245円207円345円201円266円346円216円222円345円272円217円346円225円260円347円273円204円II.md" +++ "b/problems/0922.346円214円211円345円245円207円345円201円266円346円216円222円345円272円217円346円225円260円347円273円204円II.md" @@ -147,8 +147,6 @@ class Solution { } ``` -### java - ```java //方法一:采用额外的数组空间 class Solution { @@ -384,3 +382,4 @@ function sortArrayByParityII(nums: number[]): number[] { + diff --git "a/problems/0941.346円234円211円346円225円210円347円232円204円345円261円261円350円204円211円346円225円260円347円273円204円.md" "b/problems/0941.346円234円211円346円225円210円347円232円204円345円261円261円350円204円211円346円225円260円347円273円204円.md" index f43a210837..48c29eb479 100644 --- "a/problems/0941.346円234円211円346円225円210円347円232円204円345円261円261円350円204円211円346円225円260円347円273円204円.md" +++ "b/problems/0941.346円234円211円346円225円210円347円232円204円345円261円261円350円204円211円346円225円260円347円273円204円.md" @@ -33,7 +33,7 @@ * 输出:true -# 思路 +## 思路 判断是山峰,主要就是要严格的保存左边到中间,和右边到中间是递增的。 @@ -71,9 +71,9 @@ public: 如果想系统学一学双指针的话, 可以看一下这篇[双指针法:总结篇!](https://programmercarl.com/双指针总结.html) -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -101,7 +101,7 @@ class Solution { } ``` -## Python3 +### Python3 ```python class Solution: @@ -118,7 +118,7 @@ class Solution: ``` -## Go +### Go ```go func validMountainArray(arr []int) bool { @@ -142,7 +142,7 @@ func validMountainArray(arr []int) bool { } ``` -## JavaScript +### JavaScript ```js var validMountainArray = function(arr) { @@ -157,7 +157,7 @@ var validMountainArray = function(arr) { }; ``` -## TypeScript +### TypeScript ```typescript function validMountainArray(arr: number[]): boolean { @@ -177,7 +177,7 @@ function validMountainArray(arr: number[]): boolean { }; ``` -## C# +### C# ```csharp public class Solution { @@ -201,3 +201,4 @@ public class Solution { + diff --git "a/problems/1207.347円213円254円344円270円200円346円227円240円344円272円214円347円232円204円345円207円272円347円216円260円346円254円241円346円225円260円.md" "b/problems/1207.347円213円254円344円270円200円346円227円240円344円272円214円347円232円204円345円207円272円347円216円260円346円254円241円346円225円260円.md" index 1a7a001989..83ebbcb7bc 100644 --- "a/problems/1207.347円213円254円344円270円200円346円227円240円344円272円214円347円232円204円345円207円272円347円216円260円346円254円241円346円225円260円.md" +++ "b/problems/1207.347円213円254円344円270円200円346円227円240円344円272円214円347円232円204円345円207円272円347円216円260円346円254円241円346円225円260円.md" @@ -31,7 +31,7 @@ * -1000 <= arr[i] <= 1000 -# 思路 +## 思路 这道题目数组在是哈希法中的经典应用,如果对数组在哈希法中的使用还不熟悉的同学可以看这两篇:[数组在哈希法中的应用](https://programmercarl.com/0242.有效的字母异位词.html)和[哈希法:383. 赎金信](https://programmercarl.com/0383.赎金信.html) @@ -71,9 +71,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -97,7 +97,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: 数组在哈西法的应用 class Solution: @@ -133,10 +134,8 @@ class Solution: ``` +### JavaScript: -Go: - -JavaScript: ``` javascript // 方法一:使用数组记录元素出现次数 var uniqueOccurrences = function(arr) { @@ -171,7 +170,7 @@ var uniqueOccurrences = function(arr) { }; ``` -TypeScript: +### TypeScript:> 借用数组: @@ -209,3 +208,4 @@ function uniqueOccurrences(arr: number[]): boolean { + diff --git "a/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" "b/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" index 7c26876957..c706ba216e 100644 --- "a/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" +++ "b/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" @@ -39,7 +39,7 @@ * 2 <= nums.length <= 500 * 0 <= nums[i] <= 100 -# 思路 +## 思路 两层for循环暴力查找,时间复杂度明显为$O(n^2)$。 @@ -113,9 +113,9 @@ public: 可以排序之后加哈希,时间复杂度为$O(n\log n)$ -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```Java public int[] smallerNumbersThanCurrent(int[] nums) { @@ -136,7 +136,8 @@ public int[] smallerNumbersThanCurrent(int[] nums) { } ``` -Python: +### Python: + ```python class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: @@ -151,7 +152,8 @@ class Solution: return res ``` -Go: +### Go: + ```go func smallerNumbersThanCurrent(nums []int) []int { // map,key[数组中出现的数] value[比这个数小的个数] @@ -180,7 +182,8 @@ func smallerNumbersThanCurrent(nums []int) []int { } ``` -JavaScript: +### JavaScript: + ```javascript // 方法一:使用哈希表记录位置 var smallerNumbersThanCurrent = function(nums) { @@ -217,7 +220,7 @@ var smallerNumbersThanCurrent = function(nums) { }; ``` -TypeScript: +### TypeScript: > 暴力法: From 56f37806cac317895ca1785b2e526343ff07426d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: 2023年7月27日 14:32:16 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E8=A1=A8=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04345円255円227円347円254円246円344円270円262円.md" | 15 +++++----- ...36346円226円207円351円223円276円350円241円250円.md" | 1 + ...70347円224円250円345円255円227円347円254円246円.md" | 28 ++++++++++++------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git "a/problems/0205.345円220円214円346円236円204円345円255円227円347円254円246円344円270円262円.md" "b/problems/0205.345円220円214円346円236円204円345円255円227円347円254円246円344円270円262円.md" index a507638c35..e07ab746d9 100644 --- "a/problems/0205.345円220円214円346円236円204円345円255円227円347円254円246円344円270円262円.md" +++ "b/problems/0205.345円220円214円346円236円204円345円255円227円347円254円246円344円270円262円.md" @@ -29,7 +29,7 @@ 提示:可以假设 s 和 t 长度相同。 -# 思路 +## 思路 字符串没有说都是小写字母之类的,所以用数组不合适了,用map来做映射。 @@ -61,9 +61,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -87,7 +87,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -110,7 +110,7 @@ class Solution: return True ``` -## Go +### Go ```go func isIsomorphic(s string, t string) bool { @@ -132,7 +132,7 @@ func isIsomorphic(s string, t string) bool { } ``` -## JavaScript +### JavaScript ```js var isIsomorphic = function(s, t) { @@ -156,7 +156,7 @@ var isIsomorphic = function(s, t) { }; ``` -## TypeScript +### TypeScript ```typescript function isIsomorphic(s: string, t: string): boolean { @@ -183,3 +183,4 @@ function isIsomorphic(s: string, t: string): boolean { + diff --git "a/problems/0234.345円233円236円346円226円207円351円223円276円350円241円250円.md" "b/problems/0234.345円233円236円346円226円207円351円223円276円350円241円250円.md" index 18b397e32f..fef942fc49 100644 --- "a/problems/0234.345円233円236円346円226円207円351円223円276円350円241円250円.md" +++ "b/problems/0234.345円233円236円346円226円207円351円223円276円350円241円250円.md" @@ -432,3 +432,4 @@ function reverseList(head: ListNode | null): ListNode | null { + diff --git "a/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" "b/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" index a53148b313..1138d7fc2f 100644 --- "a/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" +++ "b/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" @@ -30,7 +30,7 @@ words[i] 由小写英文字母组成 -# 思路 +## 思路 这道题意一起就有点绕,不是那么容易懂,其实就是26个小写字符中有字符 在所有字符串里都出现的话,就输出,重复的也算。 @@ -140,7 +140,7 @@ public: ## 其他语言版本 -Java: +### Java: ```Java class Solution { @@ -174,7 +174,8 @@ class Solution { } } ``` -Python +### Python + ```python class Solution: def commonChars(self, words: List[str]) -> List[str]: @@ -218,7 +219,8 @@ class Solution: return l ``` -javaScript +### JavaScript + ```js var commonChars = function (words) { let res = [] @@ -285,7 +287,8 @@ var commonChars = function(words) { } ``` -TypeScript +### TypeScript + ```ts console.time("test") let str: string = "" @@ -321,7 +324,8 @@ TypeScript return str.split("") ``` -GO +### GO + ```golang func commonChars(words []string) []string { length:=len(words) @@ -357,7 +361,8 @@ func min(a,b int)int{ } ``` -Swift: +### Swift: + ```swift func commonChars(_ words: [String]) -> [String] { var res = [String]() @@ -397,7 +402,8 @@ func commonChars(_ words: [String]) -> [String] { } ``` -C: +### C: + ```c //若两个哈希表定义为char数组(每个单词的最大长度不会超过100,因此可以用char表示),可以提高时间和空间效率 void updateHashTable(int* hashTableOne, int* hashTableTwo) { @@ -449,7 +455,8 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){ return ret; } ``` -Scala: +### Scala: + ```scala object Solution { def commonChars(words: Array[String]): List[String] = { @@ -483,7 +490,7 @@ object Solution { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -522,3 +529,4 @@ impl Solution { + From bbb2a60f8a68bf03b466e3b567ada9cfcd06d24f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: 2023年7月27日 14:37:16 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70345円220円214円347円232円204円346円240円221円.md" | 21 +++++++++------- ...02347円202円271円346円214円207円351円222円210円.md" | 19 +++++++------- ...60345円255円227円344円271円213円345円222円214円.md" | 25 +++++++++++-------- ...21345円217円230円345円271円263円350円241円241円.md" | 18 +++++++------ 4 files changed, 47 insertions(+), 36 deletions(-) diff --git "a/problems/0100.347円233円270円345円220円214円347円232円204円346円240円221円.md" "b/problems/0100.347円233円270円345円220円214円347円232円204円346円240円221円.md" index 96acacf61c..56a6c8840f 100644 --- "a/problems/0100.347円233円270円345円220円214円347円232円204円346円240円221円.md" +++ "b/problems/0100.347円233円270円345円220円214円347円232円204円346円240円221円.md" @@ -19,7 +19,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png) -# 思路 +## 思路 在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。 @@ -115,7 +115,7 @@ public: 当然我可以把如上代码整理如下: -## 递归 +### 递归 ```CPP class Solution { @@ -134,7 +134,7 @@ public: }; ``` -## 迭代法 +### 迭代法 ```CPP class Solution { @@ -166,9 +166,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java // 递归法 @@ -205,7 +205,8 @@ class Solution { } } ``` -Python: +### Python: + ```python # 递归法 class Solution: @@ -236,7 +237,8 @@ class Solution: que.append(rightNode.right) return True ``` -Go: +### Go: + > 递归法 ```go func isSameTree(p *TreeNode, q *TreeNode) bool { @@ -258,7 +260,7 @@ func isSameTree(p *TreeNode, q *TreeNode) bool { } ``` -JavaScript: +### JavaScript: > 递归法 @@ -296,7 +298,7 @@ var isSameTree = (p, q) => { }; ``` -TypeScript: +### TypeScript: > 递归法-先序遍历 @@ -341,3 +343,4 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + diff --git "a/problems/0116.345円241円253円345円205円205円346円257円217円344円270円252円350円212円202円347円202円271円347円232円204円344円270円213円344円270円200円344円270円252円345円217円263円344円276円247円350円212円202円347円202円271円346円214円207円351円222円210円.md" "b/problems/0116.345円241円253円345円205円205円346円257円217円344円270円252円350円212円202円347円202円271円347円232円204円344円270円213円344円270円200円344円270円252円345円217円263円344円276円247円350円212円202円347円202円271円346円214円207円351円222円210円.md" index 31bb6822cc..003ef75afe 100644 --- "a/problems/0116.345円241円253円345円205円205円346円257円217円344円270円252円350円212円202円347円202円271円347円232円204円344円270円213円344円270円200円344円270円252円345円217円263円344円276円247円350円212円202円347円202円271円346円214円207円351円222円210円.md" +++ "b/problems/0116.345円241円253円345円205円205円346円257円217円344円270円252円350円212円202円347円202円271円347円232円204円344円270円213円344円270円200円344円270円252円345円217円263円344円276円247円350円212円202円347円202円271円346円214円207円351円222円210円.md" @@ -30,7 +30,7 @@ struct Node { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png) -# 思路 +## 思路 注意题目提示内容,: * 你只能使用常量级额外空间。 @@ -38,7 +38,7 @@ struct Node { 基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。 -## 递归 +### 递归 一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。 @@ -83,7 +83,7 @@ public: }; ``` -## 迭代(层序遍历) +### 迭代(层序遍历) 本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。 @@ -114,9 +114,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java // 递归法 @@ -169,7 +169,7 @@ class Solution { } ``` -## Python +### Python ```python # 递归法 @@ -210,7 +210,7 @@ class Solution: nodePre.next = None # 本层最后一个节点指向None return root ``` -## Go +### Go ```go // 迭代法 func connect(root *Node) *Node { @@ -259,7 +259,7 @@ func connect(root *Node) *Node { } ``` -## JavaScript +### JavaScript ```js const connect = root => { @@ -287,7 +287,7 @@ const connect = root => { }; ``` -## TypeScript +### TypeScript (注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’) @@ -365,3 +365,4 @@ function connect(root: NodePro | null): NodePro | null { + diff --git "a/problems/0129.346円261円202円346円240円271円345円210円260円345円217円266円345円255円220円350円212円202円347円202円271円346円225円260円345円255円227円344円271円213円345円222円214円.md" "b/problems/0129.346円261円202円346円240円271円345円210円260円345円217円266円345円255円220円350円212円202円347円202円271円346円225円260円345円255円227円344円271円213円345円222円214円.md" index 445e108aa9..ebb36071cf 100644 --- "a/problems/0129.346円261円202円346円240円271円345円210円260円345円217円266円345円255円220円350円212円202円347円202円271円346円225円260円345円255円227円344円271円213円345円222円214円.md" +++ "b/problems/0129.346円261円202円346円240円271円345円210円260円345円217円266円345円255円220円350円212円202円347円202円271円346円225円260円345円255円227円344円271円213円345円222円214円.md" @@ -10,7 +10,7 @@ [力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/) -# 思路 +## 思路 本题和[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii)是类似的思路,做完这道题,可以顺便把[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii) 和 [112.路径总和](https://programmercarl.com/0112.路径总和.html#_112-路径总和) 做了。 @@ -24,7 +24,7 @@ 那么先按递归三部曲来分析: -## 递归三部曲 +### 递归三部曲 如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html) @@ -116,7 +116,7 @@ path.pop_back(); // 回溯 ``` **把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。 -## 整体C++代码 +整体C++代码 关键逻辑分析完了,整体C++代码如下: @@ -162,16 +162,16 @@ public: }; ``` -# 总结 +## 总结 过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。 **我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!** -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -219,7 +219,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def sumNumbers(self, root: TreeNode) -> int: @@ -246,7 +247,7 @@ class Solution: backtrace(root) return res ``` -Go: +### Go: ```go func sumNumbers(root *TreeNode) int { @@ -271,7 +272,8 @@ func dfs(root *TreeNode, tmpSum int, sum *int) { -JavaScript: +### JavaScript: + ```javascript var sumNumbers = function(root) { const listToInt = path => { @@ -315,7 +317,7 @@ var sumNumbers = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript function sumNumbers(root: TreeNode | null): number { @@ -351,7 +353,7 @@ function sumNumbers(root: TreeNode | null): number { }; ``` -C: +### C: ```c //sum记录总和 @@ -384,3 +386,4 @@ int sumNumbers(struct TreeNode* root){ + diff --git "a/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" "b/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" index 0f81745c36..57e56b8fe3 100644 --- "a/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" +++ "b/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" @@ -28,7 +28,7 @@ * 树节点的数目在 1 到 10^4 之间。 * 树节点的值互不相同,且在 1 到 10^5 之间。 -# 思路 +## 思路 这道题目,可以中序遍历把二叉树转变为有序数组,然后在根据有序数组构造平衡二叉搜索树。 @@ -71,9 +71,10 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 + +### Java: -Java: ```java class Solution { ArrayList res = new ArrayList(); @@ -99,7 +100,8 @@ class Solution { } } ``` -Python: +### Python: + ```python class Solution: def balanceBST(self, root: TreeNode) -> TreeNode: @@ -121,7 +123,7 @@ class Solution: traversal(root) return getTree(res, 0, len(res) - 1) ``` -Go: +### Go: ```go /** @@ -163,7 +165,8 @@ func balanceBST(root *TreeNode) *TreeNode { ``` -JavaScript: +### JavaScript: + ```javascript var balanceBST = function(root) { const res = []; @@ -188,7 +191,7 @@ var balanceBST = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript function balanceBST(root: TreeNode | null): TreeNode | null { @@ -218,3 +221,4 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null + From e9b0d46f3535546063192c170606bffa8ff75a32 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: 2023年7月27日 14:41:58 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E8=B4=AA=E5=BF=83?= =?UTF-8?q?=E4=B8=8E=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=20=E9=A2=9D?= =?UTF-8?q?=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36346円226円207円345円255円220円344円270円262円.md" | 23 ++++++++++--------- ...345円233円236円346円226円207円344円270円262円II.md" | 13 ++++++----- ...a2345円217円202円350円256円256円351円231円242円.md" | 17 +++++++------- ...27347円232円204円344円270円252円346円225円260円.md" | 13 ++++++----- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git "a/problems/0005.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円344円270円262円.md" "b/problems/0005.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円344円270円262円.md" index b1987b87b3..614f60514c 100644 --- "a/problems/0005.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円344円270円262円.md" +++ "b/problems/0005.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円344円270円262円.md" @@ -30,17 +30,17 @@ * 输出:"a" -# 思路 +## 思路 本题和[647.回文子串](https://programmercarl.com/0647.回文子串.html) 差不多是一样的,但647.回文子串更基本一点,建议可以先做647.回文子串 -## 暴力解法 +### 暴力解法 两层for循环,遍历区间起始位置和终止位置,然后判断这个区间是不是回文。 时间复杂度:O(n^3) -## 动态规划 +### 动态规划 动规五部曲: @@ -208,7 +208,7 @@ public: * 时间复杂度:O(n^2) * 空间复杂度:O(n^2) -## 双指针 +### 双指针 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 @@ -258,9 +258,9 @@ public: -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java // 双指针 动态规划 @@ -327,7 +327,7 @@ class Solution { } ``` -Python: +### Python: ```python class Solution: @@ -377,7 +377,7 @@ class Solution: return s[start:end] ``` -Go: +### Go: ```go func longestPalindrome(s string) string { @@ -411,7 +411,7 @@ func longestPalindrome(s string) string { ``` -JavaScript: +### JavaScript: ```js //动态规划解法 @@ -527,7 +527,7 @@ var longestPalindrome = function(s) { }; ``` -C: +### C: 动态规划: ```c @@ -615,7 +615,7 @@ char * longestPalindrome(char * s){ } ``` -C#: +### C#: 動態規則: ```c# @@ -681,3 +681,4 @@ public class Solution { + diff --git "a/problems/0132.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円II.md" "b/problems/0132.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円II.md" index 9b164dfbb5..eb91a1899f 100644 --- "a/problems/0132.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円II.md" +++ "b/problems/0132.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円II.md" @@ -34,7 +34,7 @@ * 1 <= s.length <= 2000 * s 仅由小写英文字母组成 -# 思路 +## 思路 我们在讲解回溯法系列的时候,讲过了这道题目[回溯算法:131.分割回文串](https://programmercarl.com/0131.分割回文串.html)。 @@ -201,9 +201,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -257,7 +257,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -286,7 +286,7 @@ class Solution: return dp[-1] ``` -## Go +### Go ```go func minCut(s string) int { @@ -330,7 +330,7 @@ func min(i, j int) int { } ``` -## JavaScript +### JavaScript ```js var minCut = function(s) { @@ -376,3 +376,4 @@ var minCut = function(s) { + diff --git "a/problems/0649.Dota2345円217円202円350円256円256円351円231円242円.md" "b/problems/0649.Dota2345円217円202円350円256円256円351円231円242円.md" index a1420a66db..db6b43df8e 100644 --- "a/problems/0649.Dota2345円217円202円350円256円256円351円231円242円.md" +++ "b/problems/0649.Dota2345円217円202円350円256円256円351円231円242円.md" @@ -42,7 +42,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利。 -# 思路 +## 思路 这道题 题意太绕了,我举一个更形象的例子给大家捋顺一下。 @@ -70,7 +70,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 如果对贪心算法理论基础还不了解的话,可以看看这篇:[关于贪心算法,你该了解这些!](https://programmercarl.com/贪心算法理论基础.html) ,相信看完之后对贪心就有基本的了解了。 -# 代码实现 +## 代码实现 实现代码,在每一轮循环的过程中,去过模拟优先消灭身后的对手,其实是比较麻烦的。 @@ -111,9 +111,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -145,7 +145,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -173,7 +173,7 @@ class Solution: return "Radiant" if R else "Dire" ``` -## Go +### Go ```go @@ -214,7 +214,7 @@ func predictPartyVictory(senateStr string) string { } ``` -## JavaScript +### JavaScript ```js var predictPartyVictory = function(senateStr) { @@ -244,7 +244,7 @@ var predictPartyVictory = function(senateStr) { }; ``` -## TypeScript +### TypeScript ```typescript function predictPartyVictory(senate: string): string { @@ -287,3 +287,4 @@ function predictPartyVictory(senate: string): string { + diff --git "a/problems/0673.346円234円200円351円225円277円351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円347円232円204円344円270円252円346円225円260円.md" "b/problems/0673.346円234円200円351円225円277円351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円347円232円204円344円270円252円346円225円260円.md" index da80a4e039..0277f24989 100644 --- "a/problems/0673.346円234円200円351円225円277円351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円347円232円204円344円270円252円346円225円260円.md" +++ "b/problems/0673.346円234円200円351円225円277円351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円347円232円204円344円270円252円346円225円260円.md" @@ -24,7 +24,7 @@ * 解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。 -# 思路 +## 思路 这道题可以说是 [300.最长上升子序列](https://programmercarl.com/0300.最长上升子序列.html) 的进阶版本 @@ -221,9 +221,9 @@ public: 还有O(nlog n)的解法,使用树状数组,今天有点忙就先不写了,感兴趣的同学可以自行学习一下,这里有我之前写的树状数组系列博客:https://blog.csdn.net/youngyangyang04/category_871105.html (十年前的陈年老文了) -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -257,7 +257,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -286,7 +286,7 @@ class Solution: return result; ``` -## Go +### Go ```go @@ -332,7 +332,7 @@ func findNumberOfLIS(nums []int) int { } ``` -## JavaScript +### JavaScript ```js var findNumberOfLIS = function(nums) { @@ -364,3 +364,4 @@ var findNumberOfLIS = function(nums) { + From a5eb340ee630091b199407bea8d3191b546708a0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: 2023年7月27日 14:48:04 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=BE=E8=AE=BA?= =?UTF-8?q?=20=E5=B9=B6=E6=9F=A5=E9=9B=86=20=E6=A8=A1=E6=8B=9F=20=E4=BD=8D?= =?UTF-8?q?=E8=BF=90=E7=AE=97=20=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70200円344円270円252円346円216円222円345円210円227円.md" | 13 +++++++------ ...15225円350円257円215円346円216円245円351円276円231円.md" | 15 +++++++-------- ...61277円347円232円204円345円221円250円351円225円277円.md" | 13 ++++++++----- ...77224円345円233円236円345円216円237円347円202円271円.md" | 15 ++++++++------- ...06227円344円275円231円350円277円236円346円216円245円.md" | 13 +++++++------ ...227円344円275円231円350円277円236円346円216円245円II.md" | 11 ++++++----- ...25260円347円233円256円346円216円222円345円272円217円.md" | 15 ++++++++------- 7 files changed, 51 insertions(+), 44 deletions(-) diff --git "a/problems/0031.344円270円213円344円270円200円344円270円252円346円216円222円345円210円227円.md" "b/problems/0031.344円270円213円344円270円200円344円270円252円346円216円222円345円210円227円.md" index 34aa1086c0..3cfb673a29 100644 --- "a/problems/0031.344円270円213円344円270円200円344円270円252円346円216円222円345円210円227円.md" +++ "b/problems/0031.344円270円213円344円270円200円344円270円252円346円216円222円345円210円227円.md" @@ -34,7 +34,7 @@ * 输出:[1] -# 思路 +## 思路 一些同学可能手动写排列的顺序,都没有写对,那么写程序的话思路一定是有问题的了,我这里以1234为例子,把全排列都列出来。可以参考一下规律所在: @@ -92,9 +92,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -159,7 +159,7 @@ class Solution { } ``` -## Python +### Python >直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数 ```python class Solution: @@ -191,7 +191,7 @@ class Solution: """ ``` -## Go +### Go ```go //卡尔的解法 @@ -216,7 +216,7 @@ func reverse(a []int,begin,end int){ } ``` -## JavaScript +### JavaScript ```js //卡尔的解法(吐槽一下JavaScript的sort和其他语言的不太一样,只想到了拷贝数组去排序再替换原数组来实现nums的[i + 1, nums.length)升序排序) @@ -272,3 +272,4 @@ var nextPermutation = function(nums) { + diff --git "a/problems/0127.345円215円225円350円257円215円346円216円245円351円276円231円.md" "b/problems/0127.345円215円225円350円257円215円346円216円245円351円276円231円.md" index 20ad518295..97bc66d096 100644 --- "a/problems/0127.345円215円225円350円257円215円346円216円245円351円276円231円.md" +++ "b/problems/0127.345円215円225円350円257円215円346円216円245円351円276円231円.md" @@ -29,7 +29,7 @@ * 解释:endWord "cog" 不在字典中,所以无法进行转换。 -# 思路 +## 思路 以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。 @@ -97,9 +97,9 @@ public: 当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java public int ladderLength(String beginWord, String endWord, List wordList) { @@ -196,7 +196,7 @@ class Solution { } ``` -## Python +### Python ``` class Solution: @@ -221,7 +221,7 @@ class Solution: queue.append(newWord) return 0 ``` -## Go +### Go ```go func ladderLength(beginWord string, endWord string, wordList []string) int { wordMap, que, depth := getWordMap(wordList, beginWord), []string{beginWord}, 0 @@ -274,7 +274,7 @@ func getCandidates(word string) []string { } ``` -## JavaScript +### JavaScript ```javascript var ladderLength = function(beginWord, endWord, wordList) { // 将wordList转成Set,提高查询速度 @@ -310,7 +310,7 @@ var ladderLength = function(beginWord, endWord, wordList) { }; ``` -## TypeScript +### TypeScript ```typescript function ladderLength( beginWord: string, @@ -364,4 +364,3 @@ function diffonechar(word1: string, word2: string): boolean { - diff --git "a/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" "b/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" index 18f1d01eb2..14fa98dc10 100644 --- "a/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" +++ "b/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" @@ -90,7 +90,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 解法一 @@ -191,8 +191,8 @@ class Solution { ``` -Python: -### 解法1: +### Python: + 扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。 ```python @@ -228,7 +228,8 @@ class Solution: ``` -Go: +### Go: + ```go func islandPerimeter(grid [][]int) int { m, n := len(grid), len(grid[0]) @@ -249,7 +250,8 @@ func islandPerimeter(grid [][]int) int { } ``` -JavaScript: +### JavaScript: + ```javascript //解法一 var islandPerimeter = function(grid) { @@ -305,3 +307,4 @@ var islandPerimeter = function(grid) { + diff --git "a/problems/0657.346円234円272円345円231円250円344円272円272円350円203円275円345円220円246円350円277円224円345円233円236円345円216円237円347円202円271円.md" "b/problems/0657.346円234円272円345円231円250円344円272円272円350円203円275円345円220円246円350円277円224円345円233円236円345円216円237円347円202円271円.md" index ab4bf1525c..f0d3339173 100644 --- "a/problems/0657.346円234円272円345円231円250円344円272円272円350円203円275円345円220円246円350円277円224円345円233円236円345円216円237円347円202円271円.md" +++ "b/problems/0657.346円234円272円345円231円250円344円272円272円350円203円275円345円220円246円350円277円224円345円233円236円345円216円237円347円202円271円.md" @@ -29,7 +29,7 @@ -# 思路 +## 思路 这道题目还是挺简单的,大家不要想复杂了,一波哈希法又一波图论算法啥的,哈哈。 @@ -64,9 +64,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java // 时间复杂度:O(n) @@ -86,7 +86,7 @@ class Solution { } ``` -## Python +### Python ```python # 时间复杂度:O(n) @@ -107,7 +107,7 @@ class Solution: return x == 0 and y == 0 ``` -## Go +### Go ```go func judgeCircle(moves string) bool { @@ -131,7 +131,7 @@ func judgeCircle(moves string) bool { } ``` -## JavaScript +### JavaScript ```js // 时间复杂度:O(n) @@ -150,7 +150,7 @@ var judgeCircle = function(moves) { ``` -## TypeScript +### TypeScript ```ts var judgeCircle = function (moves) { @@ -185,3 +185,4 @@ var judgeCircle = function (moves) { + diff --git "a/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" "b/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" index c4d62d9b46..8124cc7eea 100644 --- "a/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" +++ "b/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" @@ -25,7 +25,7 @@ * edges 中无重复元素 * 给定的图是连通的 -# 思路 +## 思路 这道题目也是并查集基础题目。 @@ -150,9 +150,9 @@ public: 可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -205,7 +205,7 @@ class Solution { } ``` -## Python +### Python ```python @@ -256,7 +256,7 @@ class Solution: return [] ``` -## Go +### Go ```go @@ -312,7 +312,7 @@ func findRedundantConnection(edges [][]int) []int { } ``` -## JavaScript +### JavaScript ```js const n = 1005; @@ -365,3 +365,4 @@ var findRedundantConnection = function(edges) { + diff --git "a/problems/0685.345円206円227円344円275円231円350円277円236円346円216円245円II.md" "b/problems/0685.345円206円227円344円275円231円350円277円236円346円216円245円II.md" index 8c56afdc08..31b2ad247e 100644 --- "a/problems/0685.345円206円227円344円275円231円350円277円236円346円216円245円II.md" +++ "b/problems/0685.345円206円227円344円275円231円350円277円236円346円216円245円II.md" @@ -213,9 +213,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java @@ -335,7 +335,7 @@ class Solution { } ``` -## Python +### Python ```python @@ -426,7 +426,7 @@ class Solution: return self.getRemoveEdge(edges) ``` -## Go +### Go ```go @@ -527,7 +527,7 @@ func findRedundantDirectedConnection(edges [][]int) []int { ``` -## JavaScript +### JavaScript ```js const N = 1010; // 如题:二维数组大小的在3到1000范围内 @@ -623,3 +623,4 @@ var findRedundantDirectedConnection = function(edges) { + diff --git "a/problems/1356.346円240円271円346円215円256円346円225円260円345円255円227円344円272円214円350円277円233円345円210円266円344円270円2131円347円232円204円346円225円260円347円233円256円346円216円222円345円272円217円.md" "b/problems/1356.346円240円271円346円215円256円346円225円260円345円255円227円344円272円214円350円277円233円345円210円266円344円270円2131円347円232円204円346円225円260円347円233円256円346円216円222円345円272円217円.md" index b898b7f25c..cc7a7007c7 100644 --- "a/problems/1356.346円240円271円346円215円256円346円225円260円345円255円227円344円272円214円350円277円233円345円210円266円344円270円2131円347円232円204円346円225円260円347円233円256円346円216円222円345円272円217円.md" +++ "b/problems/1356.346円240円271円346円215円256円346円225円260円345円255円227円344円272円214円350円277円233円345円210円266円344円270円2131円347円232円204円346円225円260円347円233円256円346円216円222円345円272円217円.md" @@ -46,7 +46,7 @@ -# 思路 +## 思路 这道题其实是考察如何计算一个数的二进制中1的数量。 @@ -87,7 +87,7 @@ int bitCount(int n) { 下面我就使用方法二,来做这道题目: -## C++代码 + ```C++ class Solution { @@ -116,9 +116,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -151,7 +151,7 @@ class Solution { -## Python +### Python ```python class Solution: @@ -167,7 +167,7 @@ class Solution: return count ``` -## Go +### Go ```go func sortByBits(arr []int) []int { @@ -205,7 +205,7 @@ func bitCount(n int) int { } ``` -## JavaScript +### JavaScript ```js var sortByBits = function(arr) { @@ -227,3 +227,4 @@ var sortByBits = function(arr) { + From 9693ad4e456bf583b96e1f8a5cb965ee0f6b4f63 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: 2023年7月27日 15:40:30 +0800 Subject: [PATCH 13/13] Update --- README.md | 25 +- problems/qita/join.md | 249 ++++++++++++++++++ ...16346円234円254円351円241円271円347円233円256円.md" | 15 -- ...72344円272円214円345円217円211円346円240円221円.md" | 2 + ...CM346円250円241円345円274円217円357円274円237円.md" | 4 + ...43347円240円201円351円243円216円346円240円274円.md" | 2 +- 6 files changed, 263 insertions(+), 34 deletions(-) create mode 100644 problems/qita/join.md delete mode 100644 "problems/qita/345円217円202円344円270円216円346円234円254円351円241円271円347円233円256円.md" diff --git a/README.md b/README.md index dd70d8cb6b..e5eb97bc7a 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ > 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) > 2. **正式出版** :[《代码随想录》](https://programmercarl.com/other/publish.html) 。 > 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。 -> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。 +> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.programmercarl.com/other/gongkaike.html) 。 > 5. **最强八股文** :[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。 > 6. **刷题顺序** :README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 > 7. **学习社区** :一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 -> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。 +> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://www.programmercarl.com/qita/join.html)了解提交代码的方式。 > 9. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境! @@ -51,19 +51,12 @@ ## 如何使用该刷题攻略 -电脑端还看不到留言,大家可以在公众号[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png),左下角有「刷题攻略」,这是手机版刷题攻略,看完就会发现有很多录友(代码随想录的朋友们)在文章下留言打卡,这份刷题顺序和题解已经陪伴了上万录友了,同时也说明文章的质量是经过上万人的考验! - -欢迎每一位学习算法的小伙伴加入到这个学习阵营来! - -**目前已经更新了,数组-> 链表-> 哈希表->字符串->栈与队列->树->回溯->贪心,八个专题了,正在讲解动态规划!** +按照先面的排列顺序,从数组开始刷起就可以了,顺序都安排好了,按顺序刷就好。 在刷题攻略中,每个专题开始都有理论基础篇,并不像是教科书般的理论介绍,而是从实战中归纳需要的基础知识。每个专题结束都有总结篇,最这个专题的归纳总结。 如果你是算法老手,这篇攻略也是复习的最佳资料,如果把每个系列对应的总结篇,快速过一遍,整个算法知识体系以及各种解法就重现脑海了。 - -目前「代码随想录」刷题攻略更新了:**200多篇文章,精讲了200道经典算法题目,共60w字的详细图解,大部分题目都搭配了20分钟左右的视频讲解**,视频质量很好,口碑很好,大家可以去看看,视频列表:[代码随想录视频讲解](https://www.bilibili.com/video/BV1fA4y1o715)。 - **这里每一篇题解,都是精品,值得仔细琢磨**。 我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)贡献的代码,当然我也会严格把控代码质量。 @@ -100,14 +93,11 @@ * [程序员应该用什么用具来写文档?](./problems/前序/程序员写文档工具.md) * 求职 + * [ACM模式练习网站,卡码网](https://kamacoder.com/) * [程序员的简历应该这么写!!(附简历模板)](./problems/前序/程序员简历.md) + * [【专业技能】应该这样写!](https://programmercarl.com/other/jianlizhuanye.html) + * [【项目经历】应该这样写!](https://programmercarl.com/other/jianlixiangmu.html) * [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) - * [北京有这些互联网公司,你都知道么?](./problems/前序/北京互联网公司总结.md) - * [上海有这些互联网公司,你都知道么?](./problems/前序/上海互联网公司总结.md) - * [深圳有这些互联网公司,你都知道么?](./problems/前序/深圳互联网公司总结.md) - * [广州有这些互联网公司,你都知道么?](./problems/前序/广州互联网公司总结.md) - * [成都有这些互联网公司,你都知道么?](./problems/前序/成都互联网公司总结.md) - * [杭州有这些互联网公司,你都知道么?](./problems/前序/杭州互联网公司总结.md) * 算法性能分析 * [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md) @@ -506,7 +496,7 @@ # 关于作者 -大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 +大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信。 @@ -527,4 +517,3 @@
- diff --git a/problems/qita/join.md b/problems/qita/join.md new file mode 100644 index 0000000000..3b3e2d4029 --- /dev/null +++ b/problems/qita/join.md @@ -0,0 +1,249 @@ + + +# 如何在Github上提交PR(pull request) + + +* 如何提交代码 +* 合入不规范 + * 提交信息不规范 + * Markdown 代码格式 + * pull request里的commit数量 + * 代码注释 + * 说明具体是哪种方法 + * 代码规范 + * 代码逻辑 + * 处理冲突 + +以下在 [https://github.com/youngyangyang04/leetcode-master](https://github.com/youngyangyang04/leetcode-master) 上提交pr为为例 + +## 如何合入代码 + +首先来说一说如何合入代码,不少录友还不太会使用github,所以这里也做一下科普。 + +我特意申请一个新的Github账号,给大家做一个示范。 + +需要强调一下,一个commit就只更新一道题目,不要很多题目一起放在一个commit里,那样就很乱。 + +首先LeetCode-Master每天都有更新,如何保持你fork到自己的仓库是最新的版本呢。 + +点击这里Fetch upstream。 + +
+ +点击之后,这里就会显示最新的信息了 +
+ +注意这时是你的远端仓库为最新版本,本地还不是最新的,本地要git pull一下。 + +基于最新的版本,大家在去提交代码。 + +如何提交代码呢,首先把自己的代码提交到自己的fork的远端仓库中,然后open pull request,如图: + +
+ +点击 open pull request之后,就是如下画面,一个pull request有多个commit。 + +
+ +然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一次。 然后点击 create pull request 就可以了 + +
+ +此时你就提交成功了,我会在项目中的pull requests 处理列表里看到你的请求。 +
+ +然后如果你发现自己的代码没有合入多半是有问题,如果有问题都有会在pull request里给出留言的, + +## 注意事项 + +### 提交信息不规范 + + +大家提交代码的时候有两个地方需要写备注,一个是commit,一个是pull request,pull request包好多个commit。 + +commit 说清楚本文件多了哪些修改,而pull request则是对本次合入的所有commit做一个总结性描述。 + +commit备注,举例:添加Rust python3,那么commit备注就是:添加0001两数之和 Rust python3 版本 + +而pull request 如果只有一个commit,那么就也是:添加0001两数之和 Rust python3 版本 + +如果是多个commit ,则把本次commit都描述一遍。 + +### Markdown 语法 + +关于 Markdown 代码格式,例如 添加C++代码,需要有代码块语法 + +\`\`\`C++ +C++代码 +\`\`\` + +例如这个commit,在添加java代码的时候,就直接添加代码 +
+ +正确的格式应该是这样: +
+ +一般发现问题,我也会在代码中给出评论: + +
+ +这样大家也可以学习一些 提交代码的规范方面的知识 + + +有的录友 是添加的代码块语法,但没有标记是哪种语言,这样的话 代码就不会针对某种语言高亮显示了,也比较影响阅读,例如: + +
+ +提交python代码的话,要注释好,是python2还是python3 + +例如这样: + +
+ +当然python2的话,只这么写就行 + +\`\`\`python +python代码 +\`\`\` + +### pull request里的commit数量 + + +有的录友是一个pull request 里有很多commit (一个commit是一道题目的代码)。 + +有的录友是一个pull request 里有有一个commit。 + +
+ +其实如果大家是平时一天写了两三道题目的话,那么分三个commit,一个pull request提交上来就行。 + +一个pull request 一个commit也可以,这样大家就会麻烦一点。 + +但注意一个pull request也不要放太多的commit,一旦有一道题目代码不合格,我没有合入,就这个pull request里影响其他所有代码的合入了。 + +### 代码注释 + +提交的代码最好要有注释,这样也方便读者理解。 + +例如这位录友,在提交Java代码的时候,按照题解的意思对Java版本的代码进行的注释,这就很棒👍 + +
+ +
+ +当然如果大家感觉 已有的代码 不符合以上要求的话,例如 代码思路不够清晰不够规范,注释不够友好,依然欢迎提交优化代码,要记得详细注释哦。 + +
+ +### 说明具体是哪种方法 + +有的题解有两种甚至三四种解法,在添加代码的时候,注释上也清楚具体是哪一种方法的版本。 + +下面这位录友做的就很好 + +
+ + +
+ +有的题解,是一起给出了多道题目的讲解,例如项目中0102.二叉树的层序遍历.md 中有八道题目,那么大家添加代码的时候 应该在代码注释上,或者 直接写上 是哪个题目的代码。 + + +### 代码规范 + + +大家提交代码要规范,当然代码可以在力扣上运行通过是最基本的。 + +虽然我主张没有绝对正确的代码风格,但既然是给LeetCode-Master提交代码,尽量遵循Google编程规范。 + +经常看我的代码的录友应该都知道,我的代码格严格按照 Google C++ 编程规范来的,这样看上去会比较整洁。 + +大家提交代码的时候遇到规范性问题,例如哪里应该由空格,哪里没有空格,可以参考我的代码来。 + +有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 + +
+ +**代码规范从你我做起!** + + +### 代码逻辑 + +**提交的代码要按照题解思路来写**。 + +虽然大家自己发挥想象空间是好的,但是题解还是要一脉相承,读者看完题解,发现代码和题解不是一个思路的话,那和重新读代码有啥区别了。 + +所以和题解不是一个思路的代码,除非详细注释了自己的思路 或者 写一段自己代码的描述说明思路和优化的地方,否则我就不会通过合入了哈。 + +大家的代码 最好也将关键地方放上注释,这样有助于别人快速理解你的代码。 + + +### 处理冲突 + +在合入的过程中还要处理冲突的代码, 理解大家代码的思路,解决冲突,然后在力扣提交一下,确保是没问题。 + +例如同一道题目, 一位录友提交了, 我还没处理如何,另一位录友也对这道题也提交了代码,这样就会发生冲突 +
+ +大家提交代码的热情太高了,我有时候根本处理不过来,但我必须当天处理完,否则第二天代码冲突会越来越多。 +
+ +一天晚分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] + +那么在处理冲突的时候 保留谁的代码,删点谁的代码呢? + +我一定是看谁 代码逻辑和题解一致,代码风格好,注释友好,就保留谁的。 + +所以例如当你想提交Java代码的时候,即使发现该题解已经有Java版本了,只要你的代码写的好,一样可以提交,我评审合格一样可以合入代码库。 + + +### 不要做额外修改 + +确保这种额外文件不要提交。 + +
+ +还有添加不同方法的时候,直接用正文格式写,哪种方法就可以了,不要添加目录 ,例如这样,这样整篇文章目录结构就有影响了。 + +
+ +前面不要加 `## 前序遍历(迭代法)`,直接写`前序遍历(迭代法)`就可以了。 + +当然这里也没有给代码块标记上对应的语言,应该是 + +\`\`\` Go +Go语言代码 +\`\`\` + + +## 对代码保持敬畏 + +有的录友甚至提交的代码并不是本题的代码,虽然我是鼓励大家提交代码的,但是大家贡献代码的时候也要对 自己的代码有敬畏之心,自己的代码是要给很多读者看的。 + +* 代码运行无误 +* 写的够不够简洁 +* 注释清不清晰 +* 备注规不规范 + +这也是培养大家以后协调工作的一种能力。 + +## 优化 + +目前 leetcode-master中大部分题解已经补充了其他语言,但如果你发现了可以优化的地方,依然可以提交PR来优化。 + +甚至发现哪里有语病,也欢迎提交PR来修改,例如下面:就是把【下表】 纠正为【下标】 + +
+ +不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 + +
+ +## 特别注意 + +git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 + +提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 + + + diff --git "a/problems/qita/345円217円202円344円270円216円346円234円254円351円241円271円347円233円256円.md" "b/problems/qita/345円217円202円344円270円216円346円234円254円351円241円271円347円233円256円.md" deleted file mode 100644 index 76a2c61e8a..0000000000 --- "a/problems/qita/345円217円202円344円270円216円346円234円254円351円241円271円347円233円256円.md" +++ /dev/null @@ -1,15 +0,0 @@ - -优化已有代码 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210821161813.png) - -**push代码之前 一定要 先pull最新代码**,否则提交的pr可能会有删除其他录友代码的操作。 - -一个pr 不要修改过多文件,因为一旦有一个 文件修改有问题,就不能合入,影响其他文件的合入了。 - -git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 - -提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 - - -不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210927113149.png) diff --git "a/problems/345円211円215円345円272円217円/ACM346円250円241円345円274円217円345円246円202円344円275円225円346円236円204円345円273円272円344円272円214円345円217円211円346円240円221円.md" "b/problems/345円211円215円345円272円217円/ACM346円250円241円345円274円217円345円246円202円344円275円225円346円236円204円345円273円272円344円272円214円345円217円211円346円240円221円.md" index b64464031f..48781eda88 100644 --- "a/problems/345円211円215円345円272円217円/ACM346円250円241円345円274円217円345円246円202円344円275円225円346円236円204円345円273円272円344円272円214円345円217円211円346円240円221円.md" +++ "b/problems/345円211円215円345円272円217円/ACM346円250円241円345円274円217円345円246円202円344円275円225円346円236円204円345円273円272円344円272円214円345円217円211円346円240円221円.md" @@ -1,6 +1,8 @@ # 力扣上如何自己构造二叉树输入用例? +**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 + 经常有录友问,二叉树的题目中输入用例在ACM模式下应该怎么构造呢? 力扣上的题目,输入用例就给了一个数组,怎么就能构造成二叉树呢? diff --git "a/problems/345円211円215円345円272円217円/344円273円200円344円271円210円346円230円257円346円240円270円345円277円203円344円273円243円347円240円201円346円250円241円345円274円217円357円274円214円344円273円200円344円271円210円345円217円210円346円230円257円ACM346円250円241円345円274円217円357円274円237円.md" "b/problems/345円211円215円345円272円217円/344円273円200円344円271円210円346円230円257円346円240円270円345円277円203円344円273円243円347円240円201円346円250円241円345円274円217円357円274円214円344円273円200円344円271円210円345円217円210円346円230円257円ACM346円250円241円345円274円217円357円274円237円.md" index 0b9d230ff9..0012f88e67 100644 --- "a/problems/345円211円215円345円272円217円/344円273円200円344円271円210円346円230円257円346円240円270円345円277円203円344円273円243円347円240円201円346円250円241円345円274円217円357円274円214円344円273円200円344円271円210円345円217円210円346円230円257円ACM346円250円241円345円274円217円357円274円237円.md" +++ "b/problems/345円211円215円345円272円217円/344円273円200円344円271円210円346円230円257円346円240円270円345円277円203円344円273円243円347円240円201円346円250円241円345円274円217円357円274円214円344円273円200円344円271円210円345円217円210円346円230円257円ACM346円250円241円345円274円217円357円274円237円.md" @@ -5,6 +5,10 @@ 什么是ACM输入模式呢? 就是自己构造输入数据格式,把要需要处理的容器填充好,OJ不会给你任何代码,包括include哪些函数都要自己写,最后也要自己控制返回数据的格式。 + +**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 + + 而力扣上是核心代码模式,就是把要处理的数据都已经放入容器里,可以直接写逻辑,例如这样: ```CPP diff --git "a/problems/345円211円215円345円272円217円/344円273円243円347円240円201円351円243円216円346円240円274円.md" "b/problems/345円211円215円345円272円217円/344円273円243円347円240円201円351円243円216円346円240円274円.md" index b508346080..8fd8b40ca9 100644 --- "a/problems/345円211円215円345円272円217円/344円273円243円347円240円201円351円243円216円346円240円274円.md" +++ "b/problems/345円211円215円345円272円217円/344円273円243円347円240円201円351円243円216円346円240円274円.md" @@ -29,7 +29,7 @@ 这里我简单说一说规范问题。 -**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:googlec++编程规范,就可以领取。(涉及到微信后台的回复,没更改) +**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:编程规范,就可以领取。 **具体的规范要以自己团队风格为主**,融入团队才是最重要的。

AltStyle によって変換されたページ (->オリジナル) /