Skip to content

Commit

Permalink
style: code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Oct 31, 2023
1 parent db20b75 commit cfe1771
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 68 deletions.
1 change: 0 additions & 1 deletion leetcode-docs/easy/13-roman-to-int.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ pub fn roman_to_int(s: String) -> i32 {
})
.0
}

```

</TabItem>
Expand Down
14 changes: 8 additions & 6 deletions leetcode-docs/easy/14-longest-common-prefix.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
}
}
Expand All @@ -60,6 +60,8 @@ var longestCommonPrefix = function (strs) {

```rust
pub fn longest_common_prefix(strs: Vec<String>) -> String {
let mut strs = strs;
strs.sort();
let mut prefix: &str = &strs[0];
let mut has_found = false;

Expand Down
2 changes: 1 addition & 1 deletion leetcode-docs/easy/20-is-valid.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
1 change: 1 addition & 0 deletions leetcode-docs/hard/23-merge-k-lists.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 21 additions & 38 deletions leetcode-docs/hard/76-min-window.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand All @@ -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() {
Expand All @@ -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];
Expand All @@ -170,7 +153,7 @@ pub fn min_window(s: String, t: String) -> String {
}
}

res.to_string()
min_str.to_string()
}
```

Expand Down
6 changes: 3 additions & 3 deletions leetcode-docs/medium/12-int-to-roman.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

:::
Expand All @@ -81,7 +81,6 @@ import TabItem from '@theme/TabItem'
* @return {string}
*/
var intToRoman = function (num) {
let roman = ''
const map = [
[1000, 'M'],
[900, 'CM'],
Expand All @@ -98,6 +97,7 @@ var intToRoman = function (num) {
[1, 'I'],
]

let roman = ''
for (const [value, symbol] of map) {
while (num >= value) {
num -= value
Expand Down
25 changes: 10 additions & 15 deletions leetcode-docs/medium/3-length-of-longest-substring.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions leetcode-docs/medium/6-convert.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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('')

Expand All @@ -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]
}
Expand Down
1 change: 1 addition & 0 deletions leetcode-docs/medium/7-reverse.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/leetcode/rust/src/easy/question_14.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[allow(unused)]
pub fn longest_common_prefix(strs: Vec<String>) -> String {
let mut strs = strs;
strs.sort();
let mut prefix: &str = &strs[0];
let mut has_found = false;

Expand Down
6 changes: 5 additions & 1 deletion src/leetcode/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit cfe1771

Please sign in to comment.