From cfe17710d691abf971529f22c5187183d6b0f68f Mon Sep 17 00:00:00 2001 From: YanceyOfficial Date: Tue, 31 Oct 2023 17:50:22 +0800 Subject: [PATCH] style: code optimization --- leetcode-docs/easy/13-roman-to-int.mdx | 1 - .../easy/14-longest-common-prefix.mdx | 14 +++-- leetcode-docs/easy/20-is-valid.mdx | 2 +- leetcode-docs/hard/23-merge-k-lists.mdx | 1 + leetcode-docs/hard/76-min-window.mdx | 59 +++++++------------ leetcode-docs/medium/12-int-to-roman.mdx | 6 +- .../medium/3-length-of-longest-substring.mdx | 25 ++++---- leetcode-docs/medium/6-convert.mdx | 6 +- leetcode-docs/medium/7-reverse.mdx | 1 + src/leetcode/rust/src/easy/question_14.rs | 2 + src/leetcode/rust/src/main.rs | 6 +- 11 files changed, 55 insertions(+), 68 deletions(-) diff --git a/leetcode-docs/easy/13-roman-to-int.mdx b/leetcode-docs/easy/13-roman-to-int.mdx index dd8586a4..6ac1c1e3 100644 --- a/leetcode-docs/easy/13-roman-to-int.mdx +++ b/leetcode-docs/easy/13-roman-to-int.mdx @@ -115,7 +115,6 @@ pub fn roman_to_int(s: String) -> i32 { }) .0 } - ``` diff --git a/leetcode-docs/easy/14-longest-common-prefix.mdx b/leetcode-docs/easy/14-longest-common-prefix.mdx index da5efada..056309c0 100644 --- a/leetcode-docs/easy/14-longest-common-prefix.mdx +++ b/leetcode-docs/easy/14-longest-common-prefix.mdx @@ -25,8 +25,7 @@ sidebar_label: 14. 最长公共前缀 ## 题解 -先假设第一个单词为 `prefix`, 如果 `strs` 里所有单词的前缀都是 `prefix`, 那就返回之; -否则将 `prefix` 的最后一位删掉继续循环, 直到找出前缀. +先假设第一个单词为 `prefix`, 如果 `strs` 里所有单词的前缀都是 `prefix`, 那就返回之; 否则将 `prefix` 的最后一位删掉继续循环, 直到找出前缀. import Tabs from '@theme/Tabs' import TabItem from '@theme/TabItem' @@ -40,13 +39,14 @@ import TabItem from '@theme/TabItem' * @return {string} */ var longestCommonPrefix = function (strs) { + strs.sort((a, b) => a.length - b.length) let prefix = strs[0] - let hasFound = false + let flag = false - while (!hasFound) { - hasFound = strs.every((str) => str.startsWith(prefix)) + while (!flag) { + flag = strs.every((str) => str.startsWith(prefix)) - if (!hasFound) { + if (!flag) { prefix = prefix.slice(0, -1) } } @@ -60,6 +60,8 @@ var longestCommonPrefix = function (strs) { ```rust pub fn longest_common_prefix(strs: Vec) -> String { + let mut strs = strs; + strs.sort(); let mut prefix: &str = &strs[0]; let mut has_found = false; diff --git a/leetcode-docs/easy/20-is-valid.mdx b/leetcode-docs/easy/20-is-valid.mdx index 75a7f8e2..b74e26b1 100644 --- a/leetcode-docs/easy/20-is-valid.mdx +++ b/leetcode-docs/easy/20-is-valid.mdx @@ -57,7 +57,7 @@ var isValid = function (s) { const n = s.length // 如果是奇数, 一定不是合法的括号 - if (n % 2 == 1) return false + if (n % 2 === 1) return false const stack = [] diff --git a/leetcode-docs/hard/23-merge-k-lists.mdx b/leetcode-docs/hard/23-merge-k-lists.mdx index 818c6e91..8a1894dc 100644 --- a/leetcode-docs/hard/23-merge-k-lists.mdx +++ b/leetcode-docs/hard/23-merge-k-lists.mdx @@ -125,6 +125,7 @@ var mergeKLists = function (lists) { * @param {ListNode} l2 * @return {ListNode} */ +// 这个就是 21 题原题 var mergeTwoLists = function (l1, l2) { const dummy = new ListNode(-1) let curr = dummy diff --git a/leetcode-docs/hard/76-min-window.mdx b/leetcode-docs/hard/76-min-window.mdx index 9eecb731..0a4b1ba5 100644 --- a/leetcode-docs/hard/76-min-window.mdx +++ b/leetcode-docs/hard/76-min-window.mdx @@ -65,59 +65,42 @@ import TabItem from '@theme/TabItem' * @return {string} */ var minWindow = function (s, t) { - let res = '' + let minStr = '' const needMap = new Map() - for (const letter of t) { - if (needMap.has(letter)) { - needMap.set(letter, needMap.get(letter) + 1) - } else { - needMap.set(letter, 1) - } + for (const ch of t) { + needMap.set(ch, needMap.has(ch) ? needMap.get(ch) + 1 : 1) } + const windowMap = new Map() let start = 0 let end = 0 let count = 0 - while (end < sLen) { - const endLetter = s[end] - end++ - - if (needMap.has(endLetter)) { - if (windowMap.has(endLetter)) { - windowMap.set(endLetter, windowMap.get(endLetter) + 1) - } else { - windowMap.set(endLetter, 1) - } + while (end < s.length) { + const endCh = s[end++] - if (needMap.get(endLetter) === windowMap.get(endLetter)) { - count++ - } + if (needMap.has(endCh)) { + windowMap.set(endCh, windowMap.has(endCh) ? windowMap.get(endCh) + 1 : 1) + if (needMap.get(endCh) === windowMap.get(endCh)) count++ } // 当 count === needMap.size, 说明找到了一个覆盖子串 // 这个时候就可以收缩窗口了 while (count === needMap.size) { - const window = s.slice(start, end) - if (res.length === 0 || window.length < res.length) { - res = window - } - - const startLetter = s[start] - start++ + const subStr = s.slice(start, end) + if (minStr.length === 0 || subStr.length < minStr.length) minStr = subStr - if (needMap.has(startLetter)) { - if (needMap.get(startLetter) === windowMap.get(startLetter)) { - count-- - } + const startCh = s[start++] - windowMap.set(startLetter, windowMap.get(startLetter) - 1) + if (needMap.has(startCh)) { + if (needMap.get(startCh) === windowMap.get(startCh)) count-- + windowMap.set(startCh, windowMap.get(startCh) - 1) } } } - return res + return minStr } ``` @@ -128,7 +111,7 @@ var minWindow = function (s, t) { use std::collections::HashMap; pub fn min_window(s: String, t: String) -> String { - let mut res = ""; + let mut min_str = ""; let mut need_map = HashMap::new(); for letter in t.as_bytes() { @@ -152,9 +135,9 @@ pub fn min_window(s: String, t: String) -> String { } while count == need_map.len() { - let window = &s[start..end]; - if res.len() == 0 || window.len() < res.len() { - res = window; + let sub_str = &s[start..end]; + if min_str.len() == 0 || sub_str.len() < min_str.len() { + min_str = sub_str; } let start_letter = s.as_bytes()[start]; @@ -170,7 +153,7 @@ pub fn min_window(s: String, t: String) -> String { } } - res.to_string() + min_str.to_string() } ``` diff --git a/leetcode-docs/medium/12-int-to-roman.mdx b/leetcode-docs/medium/12-int-to-roman.mdx index 47f6bcd5..b532841e 100644 --- a/leetcode-docs/medium/12-int-to-roman.mdx +++ b/leetcode-docs/medium/12-int-to-roman.mdx @@ -54,13 +54,13 @@ sidebar_label: 12. 整数转罗马数字 ```ts 输入: num = 58 输出: 'LVIII' -解释: L = 50, V = 5, III = 3. +解释: (L = 50), (V = 5), (III = 3) ``` ```ts 输入: num = 1994 输出: 'MCMXCIV' -解释: M = 1000, CM = 900, XC = 90, IV = 4. +解释: (M = 1000), (CM = 900), (XC = 90), (IV = 4) ``` ::: @@ -81,7 +81,6 @@ import TabItem from '@theme/TabItem' * @return {string} */ var intToRoman = function (num) { - let roman = '' const map = [ [1000, 'M'], [900, 'CM'], @@ -98,6 +97,7 @@ var intToRoman = function (num) { [1, 'I'], ] + let roman = '' for (const [value, symbol] of map) { while (num >= value) { num -= value diff --git a/leetcode-docs/medium/3-length-of-longest-substring.mdx b/leetcode-docs/medium/3-length-of-longest-substring.mdx index 6803d950..bae6cb0d 100644 --- a/leetcode-docs/medium/3-length-of-longest-substring.mdx +++ b/leetcode-docs/medium/3-length-of-longest-substring.mdx @@ -88,17 +88,12 @@ var lengthOfLongestSubstring = function (s) { let max = 0 while (end < n) { - const endChar = s[end++] + const endCh = s[end++] + map.set(endCh, map.has(endCh) ? map.get(endCh) + 1 : 1) - if (map.has(endChar)) { - map.set(endChar, map.get(endChar) + 1) - } else { - map.set(endChar, 1) - } - - while (map.get(endChar) > 1) { - const startChar = s[start++] - map.set(startChar, map.get(startChar) - 1) + while (map.get(endCh) > 1) { + const startCh = s[start++] + map.set(startCh, map.get(startCh) - 1) } max = Math.max(max, end - start) @@ -123,14 +118,14 @@ pub fn length_of_longest_substring(s: String) -> i32 { let mut end = 0; while end < s.len() { - let end_char = s_bytes[end]; + let end_ch = s_bytes[end]; end += 1; - map.entry(end_char).and_modify(|e| *e += 1).or_insert(1); + map.entry(end_ch).and_modify(|e| *e += 1).or_insert(1); - while map[&end_char] > 1 { - let start_char = s_bytes[start]; + while map[&end_ch] > 1 { + let start_ch = s_bytes[start]; start += 1; - map.entry(start_char).and_modify(|e| *e -= 1); + map.entry(start_ch).and_modify(|e| *e -= 1); } max = cmp::max(end - start, max); diff --git a/leetcode-docs/medium/6-convert.mdx b/leetcode-docs/medium/6-convert.mdx index 800394a9..737c2c0a 100644 --- a/leetcode-docs/medium/6-convert.mdx +++ b/leetcode-docs/medium/6-convert.mdx @@ -74,11 +74,11 @@ import TabItem from '@theme/TabItem' * @return {string} */ var convert = function (s, numRows) { - const n = s.length - // 如果 numRows 比 2 小, 是没法变成 Z 型的, 直接返回 s 即可 if (numRows < 2) return s + const n = s.length + // 初始化一个数组, 数组长度为 numRows const res = Array(numRows).fill('') @@ -90,7 +90,7 @@ var convert = function (s, numRows) { const carry = i % cycle // 计算索引, 如果余数小于 numRows, 那就用这个余数即可, 否则用循环数减去该余数 - const idx = carry < numRows ? carry : cycle - carry; + const idx = carry < numRows ? carry : cycle - carry res[idx] += s[i] } diff --git a/leetcode-docs/medium/7-reverse.mdx b/leetcode-docs/medium/7-reverse.mdx index 944a0ba1..e53dbc3d 100644 --- a/leetcode-docs/medium/7-reverse.mdx +++ b/leetcode-docs/medium/7-reverse.mdx @@ -75,6 +75,7 @@ pub fn reverse(x: i32) -> i32 { if n > i32::INT_MAX / 10 || n < i32::INT_MIN / 10 { return 0; } + n = n * 10 + (x % 10); x /= 10; } diff --git a/src/leetcode/rust/src/easy/question_14.rs b/src/leetcode/rust/src/easy/question_14.rs index fdc7d2a1..3b60a27a 100644 --- a/src/leetcode/rust/src/easy/question_14.rs +++ b/src/leetcode/rust/src/easy/question_14.rs @@ -1,5 +1,7 @@ #[allow(unused)] pub fn longest_common_prefix(strs: Vec) -> String { + let mut strs = strs; + strs.sort(); let mut prefix: &str = &strs[0]; let mut has_found = false; diff --git a/src/leetcode/rust/src/main.rs b/src/leetcode/rust/src/main.rs index 73c7fa8f..146c66a4 100644 --- a/src/leetcode/rust/src/main.rs +++ b/src/leetcode/rust/src/main.rs @@ -3,7 +3,11 @@ mod hard; mod medium; fn main() { - let res = medium::question_40::combination_sum2(vec![10, 1, 2, 7, 6, 1, 5], 8); + let res = easy::question_14::longest_common_prefix(vec![ + "flow".to_string(), + "flower".to_string(), + "flight".to_string(), + ]); println!("{:?}", res); }