From f0b771af5c8d1fe5d5a298f00a2dbc05a1e23b42 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 30 Mar 2022 16:40:18 +0800 Subject: [PATCH 01/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index f50f1c1d..10b747cb 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -450,6 +450,38 @@ var partition = function(s) { }; ``` +## TypeScript + +```typescript +function partition(s: string): string[][] { + function isPalindromeStr(s: string, left: number, right: number): boolean { + while (left < right) { + if (s[left++] !== s[right--]) { + return false; + } + } + return true; + } + function backTracking(s: string, startIndex: number, route: string[]): void { + let length: number = s.length; + if (length === startIndex) { + resArr.push(route.slice()); + return; + } + for (let i = startIndex; i < length; i++) { + if (isPalindromeStr(s, startIndex, i)) { + route.push(s.slice(startIndex, i + 1)); + backTracking(s, i + 1, route); + route.pop(); + } + } + } + const resArr: string[][] = []; + backTracking(s, 0, []); + return resArr; +}; +``` + ## C ```c From 7ee6bdbbfac43194c2ce3d5feb1f322824c3c7c7 Mon Sep 17 00:00:00 2001 From: h-yx-blog <2041290842@qq.com> Date: Thu, 31 Mar 2022 08:59:58 +0800 Subject: [PATCH 02/20] =?UTF-8?q?416=E5=88=86=E5=89=B2=E7=AD=89=E5=92=8C?= =?UTF-8?q?=E5=AD=90=E9=9B=86java=E7=89=88=E5=8F=A6=E4=B8=80=E7=A7=8D?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index c8d9bc04..b24fb365 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -208,6 +208,75 @@ class Solution { } ``` +```java +public class Solution { + public static void main(String[] args) { + int num[] = {1,5,11,5}; + canPartition(num); + + } + public static boolean canPartition(int[] nums) { + int len = nums.length; + // 题目已经说非空数组,可以不做非空判断 + int sum = 0; + for (int num : nums) { + sum += num; + } + // 特判:如果是奇数,就不符合要求 + if ((sum %2 ) != 0) { + return false; + } + + int target = sum / 2; //目标背包容量 + // 创建二维状态数组,行:物品索引,列:容量(包括 0) + /* + dp[i][j]表示从数组的 [0, i] 这个子区间内挑选一些正整数 + 每个数只能用一次,使得这些数的和恰好等于 j。 + */ + boolean[][] dp = new boolean[len][target + 1]; + + // 先填表格第 0 行,第 1 个数只能让容积为它自己的背包恰好装满 (这里的dp[][]数组的含义就是“恰好”,所以就算容积比它大的也不要) + if (nums[0] <= target) { + dp[0][nums[0]] = true; + } + // 再填表格后面几行 + //外层遍历物品 + for (int i = 1; i < len; i++) { + //内层遍历背包 + for (int j = 0; j <= target; j++) { + // 直接从上一行先把结果抄下来,然后再修正 + dp[i][j] = dp[i - 1][j]; + + //如果某个物品单独的重量恰好就等于背包的重量,那么也是满足dp数组的定义的 + if (nums[i] == j) { + dp[i][j] = true; + continue; + } + //如果某个物品的重量小于j,那就可以看该物品是否放入背包 + //dp[i - 1][j]表示该物品不放入背包,如果在 [0, i - 1] 这个子区间内已经有一部分元素,使得它们的和为 j ,那么 dp[i][j] = true; + //dp[i - 1][j - nums[i]]表示该物品放入背包。如果在 [0, i - 1] 这个子区间内就得找到一部分元素,使得它们的和为 j - nums[i]。 + if (nums[i] < j) { + dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i]]; + } + } + } + for (int i = 0; i < len; i++) { + for (int j = 0; j <= target; j++) { + System.out.print(dp[i][j]+" "); + } + System.out.println(); + } + return dp[len - 1][target]; + } +} +//dp数组的打印结果 +false true false false false false false false false false false false +false true false false false true true false false false false false +false true false false false true true false false false false true +false true false false false true true false false false true true +``` + + 二维数组版本(易于理解): ```Java class Solution { From 00dc57faaca1a77fd7c371f2975df5939a7edb0c Mon Sep 17 00:00:00 2001 From: MoonLight-Sherry <3397446353@qq.com> Date: Thu, 31 Mar 2022 10:34:34 +0800 Subject: [PATCH 03/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=860509.=E6=96=90?= =?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0=E5=88=97Rust=E4=B8=A4?= =?UTF-8?q?=E7=A7=8D=E8=A7=A3=E6=B3=95=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index c6ce76c0..e60b8a9e 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -277,7 +277,30 @@ int fib(int n){ return fib(n-1) + fib(n-2); } ``` +### Rust +动态规划: +```Rust +pub fn fib(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![0; 31]; + dp[1] = 1; + for i in 2..=n { + dp[i] = dp[i - 1] + dp[i - 2]; + } + dp[n] +} +``` - +递归实现: +```Rust +pub fn fib(n: i32) -> i32 { + //若n小于等于1,返回n + f n <= 1 { + return n; + } + //否则返回fib(n-1) + fib(n-2) + return fib(n - 1) + fib(n - 2); +} +``` -----------------------
From b6688d7a8949694c2988d48c79f4ebd75d5229ed Mon Sep 17 00:00:00 2001 From: Qianzhengjun <55390356+Qianzhengjun@users.noreply.github.com> Date: Thu, 31 Mar 2022 11:37:47 +0800 Subject: [PATCH 04/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=860844.=20?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E5=8F=8C=E6=8C=87=E9=92=88=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0844.比较含退格的字符串.md | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index 00d52e42..0d83d425 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -185,6 +185,36 @@ class Solution { } ``` +双指针: + +```java +class Solution { +public static boolean backspaceCompare(String s, String t) { + char[] sarray = s.toCharArray(); + char[] tarray = t.toCharArray(); + return generate(sarray).equals(generate(tarray)); + } + public static String generate(char[] a){ + int slow = -1; + int fast = 0; + if(a.length == 1){ + return new String(a); + } else{ + for(fast = 0; fast < a.length; fast++){ + if(a[fast] != '#') + a[++slow] = a[fast]; + else{ + if(slow >= 0) + slow--; + } + } + return new String(a,0,slow + 1); + } + } +} +``` + + ### python From d1ea59aebd4659274165c5a352166696275faddc Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 31 Mar 2022 22:14:20 +0800 Subject: [PATCH 05/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880093.=E5=A4=8D?= =?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 714dcb4f..7910fc50 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -455,6 +455,45 @@ var restoreIpAddresses = function(s) { }; ``` +## TypeScript + +```typescript +function isValidIpSegment(str: string): boolean { + let resBool: boolean = true; + let tempVal: number = Number(str); + if ( + str.length === 0 || isNaN(tempVal) || + tempVal > 255 || tempVal < 0 || + (str.length > 1 && str[0] === '0') + ) { + resBool = false; + } + return resBool; +} +function restoreIpAddresses(s: string): string[] { + const resArr: string[] = []; + backTracking(s, 0, []); + return resArr; + function backTracking(s: string, startIndex: number, route: string[]): void { + let length: number = s.length; + if (route.length === 4 && startIndex >= length) { + resArr.push(route.join('.')); + return; + } + if (route.length === 4 || startIndex >= length) return; + let tempStr: string = ''; + for (let i = startIndex + 1; i <= Math.min(length, startIndex + 3); i++) { + tempStr = s.slice(startIndex, i); + if (isValidIpSegment(tempStr)) { + route.push(s.slice(startIndex, i)); + backTracking(s, i, route); + route.pop(); + } + } + } +}; +``` + ## Go 回溯(对于前导 0的IP(特别注意s[startIndex]=='0'的判断,不应该写成s[startIndex]==0,因为s截取出来不是数字)) From 48de0f8bd55e24b2b9233648e0f85e4fcb75c0c0 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 1 Apr 2022 11:02:47 +0800 Subject: [PATCH 06/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880078.=E5=AD=90?= =?UTF-8?q?=E9=9B=86.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0078.子集.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0078.子集.md b/problems/0078.子集.md index cdb5f548..e1c52b5b 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -272,7 +272,28 @@ var subsets = function(nums) { }; ``` +## TypeScript + +```typescript +function subsets(nums: number[]): number[][] { + const resArr: number[][] = []; + backTracking(nums, 0, []); + return resArr; + function backTracking(nums: number[], startIndex: number, route: number[]): void { + resArr.push(route.slice()); + let length = nums.length; + if (startIndex === length) return; + for (let i = startIndex; i < length; i++) { + route.push(nums[i]); + backTracking(nums, i + 1, route); + route.pop(); + } + } +}; +``` + ## C + ```c int* path; int pathTop; From b85700890227fa69a7ca506384e7e8a821530675 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 1 Apr 2022 12:08:26 +0800 Subject: [PATCH 07/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880090.=E5=AD=90?= =?UTF-8?q?=E9=9B=86II.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0090.子集II.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 16bd1f2c..6469f4ba 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -319,6 +319,28 @@ var subsetsWithDup = function(nums) { ``` +### TypeScript + +```typescript +function subsetsWithDup(nums: number[]): number[][] { + nums.sort((a, b) => a - b); + const resArr: number[][] = []; + backTraking(nums, 0, []); + return resArr; + function backTraking(nums: number[], startIndex: number, route: number[]): void { + resArr.push(route.slice()); + let length: number = nums.length; + if (startIndex === length) return; + for (let i = startIndex; i < length; i++) { + if (i > startIndex && nums[i] === nums[i - 1]) continue; + route.push(nums[i]); + backTraking(nums, i + 1, route); + route.pop(); + } + } +}; +``` + ### C ```c @@ -388,7 +410,7 @@ int** subsetsWithDup(int* nums, int numsSize, int* returnSize, int** returnColum } ``` -## Swift +### Swift ```swift func subsetsWithDup(_ nums: [Int]) -> [[Int]] { From c4a4a0323127f3b47e89fdeefd7bc8a57d404e1e Mon Sep 17 00:00:00 2001 From: Effy Wang Date: Fri, 1 Apr 2022 17:26:47 +0800 Subject: [PATCH 08/20] =?UTF-8?q?Update=200704.=E4=BA=8C=E5=88=86=E6=9F=A5?= =?UTF-8?q?=E6=89=BE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add detailed comments to the Javascript version of Binary Search --- problems/0704.二分查找.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index 15e096a0..55625130 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -276,7 +276,7 @@ func search(nums []int, target int) int { ``` **JavaScript:** -(版本一)左闭右闭区间 +(版本一)左闭右闭区间 [left, right] ```js /** @@ -285,10 +285,12 @@ func search(nums []int, target int) int { * @return {number} */ var search = function(nums, target) { + // right是数组最后一个数的下标,num[right]在查找范围内,是左闭右闭区间 let left = 0, right = nums.length - 1; - // 使用左闭右闭区间 + // 当left=right时,由于nums[right]在查找范围内,所以要包括此情况 while (left <= right) { let mid = left + Math.floor((right - left)/2); + // 如果中间数大于目标值,要把中间数排除查找范围,所以右边界更新为mid-1;如果右边界更新为mid,那中间数还在下次查找范围内 if (nums[mid] > target) { right = mid - 1; // 去左面闭区间寻找 } else if (nums[mid] < target) { @@ -300,7 +302,7 @@ var search = function(nums, target) { return -1; }; ``` -(版本二)左闭右开区间 +(版本二)左闭右开区间 [left, right) ```js /** @@ -309,10 +311,13 @@ var search = function(nums, target) { * @return {number} */ var search = function(nums, target) { - let left = 0, right = nums.length; - // 使用左闭右开区间 [left, right) + // right是数组最后一个数的下标+1,nums[right]不在查找范围内,是左闭右开区间 + let left = 0, right = nums.length; + // 当left=right时,由于nums[right]不在查找范围,所以不必包括此情况 while (left < right) { let mid = left + Math.floor((right - left)/2); + // 如果中间值大于目标值,中间值不应在下次查找的范围内,但中间值的前一个值应在; + // 由于right本来就不在查找范围内,所以将右边界更新为中间值,如果更新右边界为mid-1则将中间值的前一个值也踢出了下次寻找范围 if (nums[mid] > target) { right = mid; // 去左区间寻找 } else if (nums[mid] < target) { From 3f7dd67a8031663d2b673a41587e3a640e0a4b1f Mon Sep 17 00:00:00 2001 From: FrankLin Date: Fri, 1 Apr 2022 10:57:25 -0400 Subject: [PATCH 09/20] Add C version for LeetCode349 using array --- problems/0349.两个数组的交集.md | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 82be1829..64d80a37 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -281,6 +281,38 @@ impl Solution { } } ``` + +C: +```C +int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){ + + int nums1Cnt[1000] = {0}; + int lessSize = nums1Size < nums2Size ? nums1Size : nums2Size; + int * result = (int *) calloc(lessSize, sizeof(int)); + int resultIndex = 0; + int* tempNums; + + int i; + + //Calculate the number's counts for nums1 array + for(i = 0; i < nums1Size; i ++) { + nums1Cnt[nums1[i]]++; + } + + //Check if the value existing in nums1 count array + for(i = 0; i < nums2Size; i ++) { + if(nums1Cnt[nums2[i]] > 0) { + result[resultIndex] = nums2[i]; + resultIndex ++; + //Clear this count to avoid duplicated value + nums1Cnt[nums2[i]] = 0; + } + } + * returnSize = resultIndex; + return result; +} +``` + ## 相关题目 * 350.两个数组的交集 II From abc08b6bb6f698135efe52ad0a7f20b333d32173 Mon Sep 17 00:00:00 2001 From: FrankLin Date: Fri, 1 Apr 2022 11:01:00 -0400 Subject: [PATCH 10/20] correct comments of C version of Leetcode349 --- problems/0349.两个数组的交集.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 64d80a37..45f19b6e 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -294,17 +294,17 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re int i; - //Calculate the number's counts for nums1 array + /* Calculate the number's counts for nums1 array */ for(i = 0; i < nums1Size; i ++) { nums1Cnt[nums1[i]]++; } - //Check if the value existing in nums1 count array + /* Check if the value in nums2 is existing in nums1 count array */ for(i = 0; i < nums2Size; i ++) { if(nums1Cnt[nums2[i]] > 0) { result[resultIndex] = nums2[i]; resultIndex ++; - //Clear this count to avoid duplicated value + /* Clear this count to avoid duplicated value */ nums1Cnt[nums2[i]] = 0; } } From 297a22fd880f207bd9b621b7dbd5e8dfd84340a1 Mon Sep 17 00:00:00 2001 From: FrankLin Date: Fri, 1 Apr 2022 15:11:00 -0400 Subject: [PATCH 11/20] Add C version for Leetcode202 passed Leetcode submission --- problems/0202.快乐数.md | 70 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index f0a46a40..741a735a 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -315,5 +315,75 @@ class Solution { } ``` +C: +```C +typedef struct HashNodeTag { + int key; /* num */ + struct HashNodeTag *next; +}HashNode; + +/* Calcualte the hash key */ +static inline int hash(int key, int size) { + int index = key % size; + return (index > 0) ? (index) : (-index); +} + +/* Calculate the sum of the squares of its digits*/ +static inline int calcSquareSum(int num) { + unsigned int sum = 0; + while(num > 0) { + sum += (num % 10) * (num % 10); + num = num/10; + } + return sum; +} + +#define HASH_TABLE_SIZE (32) + +bool isHappy(int n){ + int sum = n; + int index = 0; + bool bHappy = false; + bool bExit = false; + /* allocate the memory for hash table with chaining method*/ + HashNode ** hashTable = (HashNode **)calloc(HASH_TABLE_SIZE, sizeof(HashNode)); + + while(bExit == false) { + /* check if n has been calculated */ + index = hash(n, HASH_TABLE_SIZE); + + HashNode ** p = hashTable + index; + + while((*p) && (bExit == false)) { + /* Check if this num was calculated, if yes, this will be endless loop */ + if((*p)->key == n) { + bHappy = false; + bExit = true; + } + /* move to next node of the same index */ + p = &((*p)->next); + } + + /* put n intot hash table */ + HashNode * newNode = (HashNode *)malloc(sizeof(HashNode)); + newNode->key = n; + newNode->next = NULL; + + *p = newNode; + + sum = calcSquareSum(n); + if(sum == 1) { + bHappy = true; + bExit = true; + } + else { + n = sum; + + } + } + + return bHappy; +} +``` -----------------------
From fb5571f16576d9d16274219d60674662970366ec Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 2 Apr 2022 11:24:06 +0800 Subject: [PATCH 12/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880491.=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0491.递增子序列.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 6103c3d0..3ea2382b 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -396,7 +396,35 @@ var findSubsequences = function(nums) { ``` +## TypeScript + +```typescript +function findSubsequences(nums: number[]): number[][] { + const resArr: number[][] = []; + backTracking(nums, 0, []); + return resArr; + function backTracking(nums: number[], startIndex: number, route: number[]): void { + let length: number = nums.length; + if (route.length >= 2) { + resArr.push(route.slice()); + } + const usedSet: Set = new Set(); + for (let i = startIndex; i < length; i++) { + if ( + nums[i] < route[route.length - 1] || + usedSet.has(nums[i]) + ) continue; + usedSet.add(nums[i]); + route.push(nums[i]); + backTracking(nums, i + 1, route); + route.pop(); + } + } +}; +``` + ### C + ```c int* path; int pathTop; From 77f1e2c85da13846b26cd33159e0dee565ef1d9d Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 2 Apr 2022 19:24:03 +0800 Subject: [PATCH 13/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880046.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesr?= =?UTF-8?q?ipt=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0046.全排列.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index c5369ddd..836c3646 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -331,6 +331,34 @@ var permute = function(nums) { ``` +## TypeScript + +```typescript +function permute(nums: number[]): number[][] { + const resArr: number[][] = []; + const helperSet: Set = new Set(); + backTracking(nums, []); + return resArr; + function backTracking(nums: number[], route: number[]): void { + if (route.length === nums.length) { + resArr.push(route.slice()); + return; + } + let tempVal: number; + for (let i = 0, length = nums.length; i < length; i++) { + tempVal = nums[i]; + if (!helperSet.has(tempVal)) { + route.push(tempVal); + helperSet.add(tempVal); + backTracking(nums, route); + route.pop(); + helperSet.delete(tempVal); + } + } + } +}; +``` + ### C ```c From c9dfda1c955c0665108d8aa7fc78b33d5edded99 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 2 Apr 2022 23:34:25 +0800 Subject: [PATCH 14/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880047.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97II.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0type?= =?UTF-8?q?script=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0047.全排列II.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index 0cecac50..cce25cd9 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -292,6 +292,34 @@ var permuteUnique = function (nums) { ``` +### TypeScript + +```typescript +function permuteUnique(nums: number[]): number[][] { + nums.sort((a, b) => a - b); + const resArr: number[][] = []; + const usedArr: boolean[] = new Array(nums.length).fill(false); + backTracking(nums, []); + return resArr; + function backTracking(nums: number[], route: number[]): void { + if (route.length === nums.length) { + resArr.push(route.slice()); + return; + } + for (let i = 0, length = nums.length; i < length; i++) { + if (i > 0 && nums[i] === nums[i - 1] && usedArr[i - 1] === false) continue; + if (usedArr[i] === false) { + route.push(nums[i]); + usedArr[i] = true; + backTracking(nums, route); + usedArr[i] = false; + route.pop(); + } + } + } +}; +``` + ### Swift ```swift From 710e816012bb0198a5a42686e94d24b50aa68760 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 3 Apr 2022 11:26:52 +0800 Subject: [PATCH 15/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=8820201112?= =?UTF-8?q?=E5=9B=9E=E6=BA=AF=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E8=A1=A5=E5=85=85=E5=88=86=E6=9E=90=E6=8E=92?= =?UTF-8?q?=E5=88=97=E9=97=AE=E9=A2=98=E6=97=B6=E9=97=B4=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/周总结/20201112回溯周末总结.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/周总结/20201112回溯周末总结.md b/problems/周总结/20201112回溯周末总结.md index c61de4bb..af08097b 100644 --- a/problems/周总结/20201112回溯周末总结.md +++ b/problems/周总结/20201112回溯周末总结.md @@ -76,7 +76,7 @@ * 空间复杂度:$O(n)$,递归深度为n,所以系统栈所用空间为$O(n)$,每一层递归所用的空间都是常数级别,注意代码里的result和path都是全局变量,就算是放在参数里,传的也是引用,并不会新申请内存空间,最终空间复杂度为$O(n)$。 排列问题分析: -* 时间复杂度:$O(n!)$,这个可以从排列的树形图中很明显发现,每一层节点为n,第二层每一个分支都延伸了n-1个分支,再往下又是n-2个分支,所以一直到叶子节点一共就是 n * n-1 * n-2 * ..... 1 = n!。 +* 时间复杂度:$O(n!)$,这个可以从排列的树形图中很明显发现,每一层节点为n,第二层每一个分支都延伸了n-1个分支,再往下又是n-2个分支,所以一直到叶子节点一共就是 n * n-1 * n-2 * ..... 1 = n!。每个叶子节点都会有一个构造全排列填进数组的操作(对应的代码:`result.push_back(path)`),该操作的复杂度为$O(n)$。所以,最终时间复杂度为:n * n!,简化为$O(n!)$。 * 空间复杂度:$O(n)$,和子集问题同理。 组合问题分析: From e48336238d25eac775f1897ad85b9be551ba1e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=B4=81?= <3224935686@qq.com> Date: Sun, 3 Apr 2022 14:03:31 +0800 Subject: [PATCH 16/20] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=B3=BB=E5=88=9719=20=E4=BB=8E=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91JS=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=98=E9=87=8F=E5=90=8D=20preorder->inorder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0106.从中序与后序遍历序列构造二叉树.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 496de431..7ecca773 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -790,7 +790,7 @@ func findRootIndex(target int,inorder []int) int{ ```javascript var buildTree = function(inorder, postorder) { - if (!preorder.length) return null; + if (!inorder.length) return null; const rootVal = postorder.pop(); // 从后序遍历的数组中获取中间节点的值, 即数组最后一个值 let rootIndex = inorder.indexOf(rootVal); // 获取中间节点在中序遍历中的下标 const root = new TreeNode(rootVal); // 创建中间节点 From 1dcbf7f6f0ba7b50da6685532a42d2ceac9aeccc Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 3 Apr 2022 19:43:34 +0800 Subject: [PATCH 17/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E5=9B=9E?= =?UTF-8?q?=E6=BA=AF=E7=AE=97=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E7=9A=84=E5=8F=A6=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...溯算法去重问题的另一种写法.md | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index b4bdda00..f48097e1 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.md @@ -365,6 +365,87 @@ class Solution: return res ``` +TypeScript: + +**90.子集II** + +```typescript +function subsetsWithDup(nums: number[]): number[][] { + nums.sort((a, b) => a - b); + const resArr: number[][] = []; + backTraking(nums, 0, []); + return resArr; + function backTraking(nums: number[], startIndex: number, route: number[]): void { + resArr.push(route.slice()); + const helperSet: Set = new Set(); + for (let i = startIndex, length = nums.length; i < length; i++) { + if (helperSet.has(nums[i])) continue; + helperSet.add(nums[i]); + route.push(nums[i]); + backTraking(nums, i + 1, route); + route.pop(); + } + } +}; +``` + +**40. 组合总和 II** + +```typescript +function combinationSum2(candidates: number[], target: number): number[][] { + candidates.sort((a, b) => a - b); + const resArr: number[][] = []; + backTracking(candidates, target, 0, 0, []); + return resArr; + function backTracking( + candidates: number[], target: number, + curSum: number, startIndex: number, route: number[] + ) { + if (curSum > target) return; + if (curSum === target) { + resArr.push(route.slice()); + return; + } + const helperSet: Set = new Set(); + for (let i = startIndex, length = candidates.length; i < length; i++) { + let tempVal: number = candidates[i]; + if (helperSet.has(tempVal)) continue; + helperSet.add(tempVal); + route.push(tempVal); + backTracking(candidates, target, curSum + tempVal, i + 1, route); + route.pop(); + + } + } +}; +``` + +**47. 全排列 II** + +```typescript +function permuteUnique(nums: number[]): number[][] { + const resArr: number[][] = []; + const usedArr: boolean[] = []; + backTracking(nums, []); + return resArr; + function backTracking(nums: number[], route: number[]): void { + if (nums.length === route.length) { + resArr.push(route.slice()); + return; + } + const usedSet: Set = new Set(); + for (let i = 0, length = nums.length; i < length; i++) { + if (usedArr[i] === true || usedSet.has(nums[i])) continue; + usedSet.add(nums[i]); + route.push(nums[i]); + usedArr[i] = true; + backTracking(nums, route); + usedArr[i] = false; + route.pop(); + } + } +}; +``` Go: From 3635751759fcf689f9baae10ab5fe4a5d642b5f8 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 3 Apr 2022 23:15:06 +0800 Subject: [PATCH 18/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880332.=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0332.重新安排行程.md | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 01f81c4d..370db45b 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -448,6 +448,50 @@ var findItinerary = function(tickets) { ``` +### TypeScript + +```typescript +function findItinerary(tickets: string[][]): string[] { + /** + TicketsMap 实例: + { NRT: Map(1) { 'JFK' => 1 }, JFK: Map(2) { 'KUL' => 1, 'NRT' => 1 } } + 这里选择Map数据结构的原因是:与Object类型的一个主要差异是,Map实例会维护键值对的插入顺序。 + */ + type TicketsMap = { + [index: string]: Map + }; + tickets.sort((a, b) => { + return a[1] < b[1] ? -1 : 1; + }); + const ticketMap: TicketsMap = {}; + for (const [from, to] of tickets) { + if (ticketMap[from] === undefined) { + ticketMap[from] = new Map(); + } + ticketMap[from].set(to, (ticketMap[from].get(to) || 0) + 1); + } + const resRoute = ['JFK']; + backTracking(tickets.length, ticketMap, resRoute); + return resRoute; + function backTracking(ticketNum: number, ticketMap: TicketsMap, route: string[]): boolean { + if (route.length === ticketNum + 1) return true; + const targetMap = ticketMap[route[route.length - 1]]; + if (targetMap !== undefined) { + for (const [to, count] of targetMap.entries()) { + if (count > 0) { + route.push(to); + targetMap.set(to, count - 1); + if (backTracking(ticketNum, ticketMap, route) === true) return true; + targetMap.set(to, count); + route.pop(); + } + } + } + return false; + } +}; +``` + ### Swift 直接迭代tickets数组: From d3a69ff358793c6c4f6f7636935b545437404f21 Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Mon, 4 Apr 2022 12:55:12 +0800 Subject: [PATCH 19/20] Add one more python code --- problems/0098.验证二叉搜索树.md | 23 ++++++++++++++++- problems/0101.对称二叉树.md | 35 -------------------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index 4ed29619..ff4335fc 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -408,7 +408,28 @@ class Solution: return True ``` -## Go +```python +# 遵循Carl的写法,只添加了节点判断的部分 +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + # method 2 + que, pre = [], None + while root or que: + while root: + que.append(root) + root = root.left + root = que.pop() + # 对第一个节点只做记录,对后面的节点进行比较 + if pre is None: + pre = root.val + else: + if pre >= root.val: return False + pre = root.val + root = root.right + return True +``` + +## Go ```Go import "math" diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 0007b4d4..e4e232c8 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -437,41 +437,6 @@ class Solution: return True ``` -层序遍历 - -```python -class Solution: - def isSymmetric(self, root: TreeNode) -> bool: - if not root: return True - que, cnt = [[root.left, root.right]], 1 - while que: - nodes, tmp, sign = que.pop(), [], False - for node in nodes: - if not node: - tmp.append(None) - tmp.append(None) - else: - if node.left: - tmp.append(node.left) - sign = True - else: - tmp.append(None) - if node.right: - tmp.append(node.right) - sign = True - else: - tmp.append(None) - p1, p2 = 0, len(nodes) - 1 - while p1 < p2: - if (not nodes[p1] and nodes[p2]) or (nodes[p1] and not nodes[p2]): return False - elif nodes[p1] and nodes[p2] and nodes[p1].val != nodes[p2].val: return False - p1 += 1 - p2 -= 1 - if sign: que.append(tmp) - cnt += 1 - return True -``` - ## Go ```go From 328b447cf44335883ce51109859f6f801e2515a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=B4=81?= <3224935686@qq.com> Date: Thu, 7 Apr 2022 21:16:00 +0800 Subject: [PATCH 20/20] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0108.=E5=B0=86?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md):=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0JavaScript=E8=BF=AD=E4=BB=A3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...将有序数组转换为二叉搜索树.md | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index bd5915fd..6ee3947b 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -355,6 +355,7 @@ func sortedArrayToBST(nums []int) *TreeNode { ``` ## JavaScript +递归 ```javascript var sortedArrayToBST = function (nums) { @@ -372,7 +373,44 @@ var sortedArrayToBST = function (nums) { return buildTree(nums, 0, nums.length - 1); }; ``` - +迭代 +```JavaScript +var sortedArrayToBST = function(nums) { + if(nums.length===0){ + return null; + } + let root=new TreeNode(0); //初始根节点 + let nodeQue=[root]; //放遍历的节点,并初始化 + let leftQue=[0]; //放左区间的下标,初始化 + let rightQue=[nums.length-1]; // 放右区间的下标 + + while(nodeQue.length){ + let curNode=nodeQue.pop(); + let left=leftQue.pop(); + let right=rightQue.pop(); + let mid=left+Math.floor((right-left)/2); + + curNode.val=nums[mid]; //将下标为mid的元素给中间节点 + +// 处理左区间 + if(left<=mid-1){ + curNode.left=new TreeNode(0); + nodeQue.push(curNode.left); + leftQue.push(left); + rightQue.push(mid-1); + } + +// 处理右区间 + if(right>=mid+1){ + curNode.right=new TreeNode(0); + nodeQue.push(curNode.right); + leftQue.push(mid+1); + rightQue.push(right); + } + } + return root; +}; +``` ## TypeScript ```typescript