Skip to content

Commit ee0a0f0

Browse files
committed
feat: update solutions to lcof problems: 46,48
- 面试题46. 把数字翻译成字符串 - 面试题48. 最长不含重复字符的子字符串
1 parent faa6fc2 commit ee0a0f0

File tree

4 files changed

+112
-24
lines changed

4 files changed

+112
-24
lines changed

lcof/面试题46. 把数字翻译成字符串/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ function translateNum(num: number): number {
148148
}
149149
```
150150

151+
```ts
152+
function translateNum(num: number): number {
153+
const s = num + '';
154+
const n = s.length;
155+
let res = 1;
156+
const dfs = (i: number) => {
157+
if (i >= n) {
158+
return;
159+
}
160+
const val = Number(s[i - 1] + s[i]);
161+
if (val >= 10 && val <= 25) {
162+
res++;
163+
dfs(i + 2);
164+
}
165+
dfs(i + 1);
166+
};
167+
dfs(1);
168+
return res;
169+
}
170+
```
171+
151172
### **Rust**
152173

153174
```rust
@@ -169,6 +190,29 @@ impl Solution {
169190
}
170191
```
171192

193+
```rust
194+
impl Solution {
195+
fn dfs(s: &String, i: usize, res: &mut i32) {
196+
if i >= s.len() {
197+
return;
198+
}
199+
let val = s[i - 1..=i].parse::<i32>().unwrap();
200+
if val >= 10 && val <= 25 {
201+
*res += 1;
202+
Self::dfs(s, i + 2, res);
203+
}
204+
Self::dfs(s, i + 1, res);
205+
}
206+
207+
pub fn translate_num(num: i32) -> i32 {
208+
let s = num.to_string();
209+
let mut res = 1;
210+
Self::dfs(&s, 1, &mut res);
211+
res
212+
}
213+
}
214+
```
215+
172216
### **...**
173217

174218
```

lcof/面试题48. 最长不含重复字符的子字符串/README.md

+54-12
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,14 @@ var lengthOfLongestSubstring = function (s) {
160160

161161
```ts
162162
function lengthOfLongestSubstring(s: string): number {
163+
const n = s.length;
164+
const set = new Set<string>();
163165
let res = 0;
164-
let l = 0;
165-
let set = new Set<string>();
166-
for (const c of s) {
166+
let i = 0;
167+
for (let j = 0; j < n; j++) {
168+
const c = s[j];
167169
while (set.has(c)) {
168-
set.delete(s[l++]);
170+
set.delete(s[i++]);
169171
}
170172
set.add(c);
171173
res = Math.max(res, set.size);
@@ -174,30 +176,70 @@ function lengthOfLongestSubstring(s: string): number {
174176
}
175177
```
176178

179+
```ts
180+
function lengthOfLongestSubstring(s: string): number {
181+
const map = new Map<string, number>();
182+
const n = s.length;
183+
let res = 0;
184+
let i = -1;
185+
for (let j = 0; j < n; j++) {
186+
if (map.has(s[j])) {
187+
i = Math.max(i, map.get(s[j]));
188+
}
189+
map.set(s[j], j);
190+
res = Math.max(res, j - i);
191+
}
192+
return res;
193+
}
194+
```
195+
177196
### **Rust**
178197

179198
```rust
180199
use std::collections::HashSet;
181-
182200
impl Solution {
183201
pub fn length_of_longest_substring(s: String) -> i32 {
202+
let s = s.as_bytes();
203+
let n = s.len();
204+
let mut set = HashSet::new();
184205
let mut res = 0;
185206
let mut i = 0;
186-
let mut set = HashSet::new();
187-
let chars = s.chars().collect::<Vec<char>>();
188-
for c in chars.iter() {
189-
while set.contains(c) {
190-
set.remove(&chars[i]);
207+
for j in 0..n {
208+
while set.contains(&s[j]) {
209+
set.remove(&s[i]);
191210
i += 1;
192211
}
193-
set.insert(c);
194-
res = res.max(set.len())
212+
set.insert(s[j]);
213+
res = res.max(set.len());
195214
}
196215
res as i32
197216
}
198217
}
199218
```
200219

220+
```rust
221+
use std::collections::HashMap;
222+
impl Solution {
223+
pub fn length_of_longest_substring(s: String) -> i32 {
224+
let s = s.as_bytes();
225+
let n = s.len();
226+
let mut map = HashMap::new();
227+
let mut res = 0;
228+
let mut i = -1;
229+
for j in 0..n {
230+
let c = s[j];
231+
let j = j as i32;
232+
if map.contains_key(&c) {
233+
i = i.max(*map.get(&c).unwrap());
234+
}
235+
map.insert(c, j);
236+
res = res.max(j - i);
237+
}
238+
res
239+
}
240+
}
241+
```
242+
201243
### **...**
202244

203245
```

lcof/面试题48. 最长不含重复字符的子字符串/Solution.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use std::collections::HashSet;
2-
32
impl Solution {
43
pub fn length_of_longest_substring(s: String) -> i32 {
4+
let s = s.as_bytes();
5+
let n = s.len();
6+
let mut set = HashSet::new();
57
let mut res = 0;
68
let mut i = 0;
7-
let mut set = HashSet::new();
8-
let chars = s.chars().collect::<Vec<char>>();
9-
for c in chars.iter() {
10-
while set.contains(c) {
11-
set.remove(&chars[i]);
9+
for j in 0..n {
10+
while set.contains(&s[j]) {
11+
set.remove(&s[i]);
1212
i += 1;
1313
}
14-
set.insert(c);
15-
res = res.max(set.len())
14+
set.insert(s[j]);
15+
res = res.max(set.len());
1616
}
1717
res as i32
1818
}

lcof/面试题48. 最长不含重复字符的子字符串/Solution.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
function lengthOfLongestSubstring(s: string): number {
2+
const n = s.length;
3+
const set = new Set<string>();
24
let res = 0;
3-
let l = 0;
4-
let set = new Set<string>();
5-
for (const c of s) {
5+
let i = 0;
6+
for (let j = 0; j < n; j++) {
7+
const c = s[j];
68
while (set.has(c)) {
7-
set.delete(s[l++]);
9+
set.delete(s[i++]);
810
}
911
set.add(c);
1012
res = Math.max(res, set.size);

0 commit comments

Comments
 (0)