Skip to content

Commit 0bea9d6

Browse files
author
binbin.hou
committed
[Feature] add for new
1 parent b6cc3e5 commit 0bea9d6

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: LC58. 最后一个单词的长度 length-of-last-word
3+
date: 2025-10-21
4+
categories: [TopInterview150]
5+
tags: [leetcode, topInterview150, array, sort]
6+
published: true
7+
---
8+
9+
# LC58. 最后一个单词的长度 length-of-last-word
10+
11+
给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。
12+
13+
返回字符串中 最后一个 单词的长度。
14+
15+
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
16+
17+
示例 1:
18+
19+
输入:s = "Hello World"
20+
输出:5
21+
解释:最后一个单词是“World”,长度为 5。
22+
23+
示例 2:
24+
25+
输入:s = " fly me to the moon "
26+
输出:4
27+
解释:最后一个单词是“moon”,长度为 4。
28+
29+
30+
示例 3:
31+
32+
输入:s = "luffy is still joyboy"
33+
输出:6
34+
解释:最后一个单词是长度为 6 的“joyboy”。
35+
36+
37+
提示:
38+
39+
1 <= s.length <= 10^4
40+
41+
s 仅有英文字母和空格 ' ' 组成
42+
43+
s 中至少存在一个单词
44+
45+
46+
# v1-优化
47+
48+
## 思路
49+
50+
可以从后往前对比,这样可以更快一些。
51+
52+
## 实现
53+
54+
```java
55+
class Solution {
56+
public int lengthOfLastWord(String s) {
57+
int len = 0;
58+
int n = s.length();
59+
for(int i = n-1; i >= 0; i--) {
60+
char c = s.charAt(i);
61+
62+
// 忽略后边的空格
63+
if(c == ' ' && len == 0) {
64+
continue;
65+
} else if(c != ' ') {
66+
len++;
67+
} else if(c == ' ') {
68+
// 再次遇到空格
69+
break;
70+
}
71+
}
72+
73+
return len;
74+
}
75+
}
76+
```
77+
78+
## 效果
79+
80+
0ms 击败 100.00%
81+
82+
## 反思
83+
84+
要学会逆向思考。
85+
86+
87+
88+
# 开源地址
89+
90+
为了便于大家学习,所有实现均已开源。欢迎 fork + star~
91+
92+
> 笔记 [https://github.com/houbb/leetcode-notes](https://github.com/houbb/leetcode-notes)
93+
94+
> 源码 [https://github.com/houbb/leetcode](https://github.com/houbb/leetcode)
95+
96+
97+
# 参考资料
98+
99+
https://leetcode.cn/problems/jump-game-ix/solutions/3762167/jie-lun-ti-pythonjavacgo-by-endlesscheng-x2qu/
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: LC14. 最长公共前缀 longest-common-prefix
3+
date: 2025-10-21
4+
categories: [TopInterview150]
5+
tags: [leetcode, topInterview150, array, sort]
6+
published: true
7+
---
8+
9+
# LC14. 最长公共前缀 longest-common-prefix
10+
11+
编写一个函数来查找字符串数组中的最长公共前缀。
12+
13+
如果不存在公共前缀,返回空字符串 ""。
14+
15+
16+
17+
示例 1:
18+
19+
输入:strs = ["flower","flow","flight"]
20+
输出:"fl"
21+
示例 2:
22+
23+
输入:strs = ["dog","racecar","car"]
24+
输出:""
25+
解释:输入不存在公共前缀。
26+
27+
28+
提示:
29+
30+
1 <= strs.length <= 200
31+
32+
0 <= strs[i].length <= 200
33+
34+
strs[i] 如果非空,则仅由小写英文字母组成
35+
36+
# v1-暴力
37+
38+
## 思路
39+
40+
直接暴力循环匹配
41+
42+
## 实现
43+
44+
```java
45+
class Solution {
46+
47+
public String longestCommonPrefix(String[] strs) {
48+
int n = strs.length;
49+
50+
StringBuilder buffer = new StringBuilder();
51+
String first = strs[0];
52+
53+
for(int i = 0; i < first.length(); i++) {
54+
// 每一个位置?
55+
if(!isAllFit(strs, i)) {
56+
break;
57+
}
58+
59+
buffer.append(first.charAt(i));
60+
}
61+
62+
return buffer.toString();
63+
}
64+
65+
private boolean isAllFit(String[] strs, int i) {
66+
String first = strs[0];
67+
int n = first.length();
68+
if(i > n - 1) {
69+
return false;
70+
}
71+
72+
char fc = first.charAt(i);
73+
for(int j = 1; j < strs.length; j++) {
74+
String str = strs[j];
75+
76+
if(i >= str.length() || str.charAt(i) != fc) {
77+
return false;
78+
}
79+
}
80+
81+
return true;
82+
}
83+
84+
}
85+
```
86+
87+
## 效果
88+
89+
1ms 击败 75.03%
90+
91+
## 反思
92+
93+
还能更快吗?
94+
95+
# 其他解法思路
96+
97+
| 方法 | 平均速度 | 适用场景 |
98+
| ----- | ------------- | --------- |
99+
| 逐字符比对 | ✅ 快且简单 | 一般情况 |
100+
| 二分查找 | 🚀 更快(长字符串场景) | 字符串极长,前缀短 |
101+
| 分治法 | 🧩 理论优雅 | 并行或递归应用 |
102+
| Trie | 🔍 扩展性强 | 频繁前缀查询 |
103+
104+
# 开源地址
105+
106+
为了便于大家学习,所有实现均已开源。欢迎 fork + star~
107+
108+
> 笔记 [https://github.com/houbb/leetcode-notes](https://github.com/houbb/leetcode-notes)
109+
110+
> 源码 [https://github.com/houbb/leetcode](https://github.com/houbb/leetcode)
111+
112+
113+
# 参考资料
114+
115+
https://leetcode.cn/problems/jump-game-ix/solutions/3762167/jie-lun-ti-pythonjavacgo-by-endlesscheng-x2qu/

0 commit comments

Comments
 (0)