Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 51d0217

Browse files
committed
commit solution 14 69
1 parent 7bb37c1 commit 51d0217

File tree

9 files changed

+175
-12
lines changed

9 files changed

+175
-12
lines changed

‎index-tags.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
3434
| --- | --- | --- | --- | --- |
3535
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
36+
| [14](https://leetcode-cn.com/problems/longest-common-prefix) | [最长公共前缀](/solution/1-99/0014.longest-common-prefix/) | `字符串` | <font color=green>简单</font> ||
3637
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
3738
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
3839
| [49](https://leetcode-cn.com/problems/group-anagrams) | [字母异位词分组](/solution/1-99/0049.group-anagrams/) | `哈希表`,`字符串` | <font color=blue>中等</font> ||
@@ -51,6 +52,7 @@
5152
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
5253
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
5354
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
55+
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> ||
5456
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
5557
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
5658
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
@@ -113,6 +115,7 @@
113115
| --- | --- | --- | --- | --- |
114116
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
115117
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
118+
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> ||
116119
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
117120
| [209](https://leetcode-cn.com/problems/minimum-size-subarray-sum) | [长度最小的子数组](/solution/200-299/0209.minimum-size-subarray-sum/) | `数组`,`双指针`,`二分查找` | <font color=blue>中等</font> ||
118121

‎index-type.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
| [7](https://leetcode-cn.com/problems/reverse-integer) | [整数反转](/solution/1-99/0007.reverse-integer/) | `数学` | <font color=green>简单</font> ||
1212
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
1313
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
14-
| [14](https://leetcode-cn.com/problems/longest-common-prefix) | [最长公共前缀](/solution/1-99/0014.longest-common-prefix/) | `字符串` | <font color=green>简单</font> |
14+
| [14](https://leetcode-cn.com/problems/longest-common-prefix) | [最长公共前缀](/solution/1-99/0014.longest-common-prefix/) | `字符串` | <font color=green>简单</font> ||
1515
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
1616
| [21](https://leetcode-cn.com/problems/merge-two-sorted-lists) | [合并两个有序链表](/solution/1-99/0021.merge-two-sorted-lists/) | `链表` | <font color=green>简单</font> ||
1717
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
@@ -23,7 +23,7 @@
2323
| [58](https://leetcode-cn.com/problems/length-of-last-word) | [最后一个单词的长度](/solution/1-99/0058.length-of-last-word/) | `字符串` | <font color=green>简单</font> ||
2424
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
2525
| [67](https://leetcode-cn.com/problems/add-binary) | [二进制求和](/solution/1-99/0067.add-binary/) | `数学`,`字符串` | <font color=green>简单</font> |
26-
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> |
26+
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> ||
2727
| [70](https://leetcode-cn.com/problems/climbing-stairs) | [爬楼梯](/solution/1-99/0070.climbing-stairs/) | `动态规划` | <font color=green>简单</font> |
2828
| [83](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list) | [删除排序链表中的重复元素](/solution/1-99/0083.remove-duplicates-from-sorted-list/) | `链表` | <font color=green>简单</font> ||
2929
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||

‎solution/1-99/0014.longest-common-prefix/README.md‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [14. 最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>编写一个函数来查找字符串数组中的最长公共前缀。</p>
66

77
<p>如果不存在公共前缀,返回空字符串&nbsp;<code>&quot;&quot;</code>。</p>
@@ -27,15 +27,36 @@
2727

2828
### 解题思路
2929

30+
![](http://lc-photo.xwlin.com/14.png)
31+
32+
1. 纵向扫描。纵向扫描时,从前往后遍历所有字符串的每一列,比较相同列上的字符是否相同,如果相同则继续对下一列进行比较,如果不相同则当前列不再属于公共前缀,当前列之前的部分为最长公共前缀。
3033

3134
### 具体解法
3235

33-
<!-- tabs:start -->
3436

3537
#### **Golang**
3638
```go
37-
39+
func longestCommonPrefix(strs []string) string {
40+
if len(strs) == 0 {
41+
return ""
42+
}
43+
var res string
44+
sort.Strings(strs)
45+
for k := range strs[0] {
46+
flag := true
47+
for i := 0; i < len(strs)-1; i++ {
48+
if strs[i][k] != strs[i+1][k] {
49+
flag = false
50+
break
51+
}
52+
}
53+
if flag {
54+
res += strs[0][k : k+1]
55+
} else {
56+
break
57+
}
58+
}
59+
return res
60+
}
3861
```
3962

40-
<!-- tabs:end -->
41-
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package leetcode
2+
3+
import (
4+
"sort"
5+
)
6+
7+
/*
8+
* @lc app=leetcode.cn id=14 lang=golang
9+
*
10+
* [14] 最长公共前缀
11+
*/
12+
13+
// @lc code=start
14+
func longestCommonPrefix(strs []string) string {
15+
if len(strs) == 0 {
16+
return ""
17+
}
18+
var res string
19+
sort.Strings(strs)
20+
for k := range strs[0] {
21+
flag := true
22+
for i := 0; i < len(strs)-1; i++ {
23+
if strs[i][k] != strs[i+1][k] {
24+
flag = false
25+
break
26+
}
27+
}
28+
if flag {
29+
res += strs[0][k : k+1]
30+
} else {
31+
break
32+
}
33+
}
34+
return res
35+
}
36+
37+
// @lc code=end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLongestCommonPrefix(t *testing.T) {
8+
var s []string
9+
var ret string
10+
s = []string{"flower", "flow", "flight"}
11+
ret = "fl"
12+
if ret != longestCommonPrefix(s) {
13+
t.Fatalf("case fails %v\n", ret)
14+
}
15+
16+
s = []string{}
17+
ret = ""
18+
if ret != longestCommonPrefix(s) {
19+
t.Fatalf("case fails %v\n", ret)
20+
}
21+
}

‎solution/1-99/0069.sqrtx/README.md‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [69. x 的平方根](https://leetcode-cn.com/problems/sqrtx)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>实现&nbsp;<code>int sqrt(int x)</code>&nbsp;函数。</p>
66

77
<p>计算并返回&nbsp;<em>x</em>&nbsp;的平方根,其中&nbsp;<em>x </em>是非负整数。</p>
@@ -26,15 +26,36 @@
2626

2727
### 解题思路
2828

29+
1. 二分查找
2930

3031
### 具体解法
3132

32-
<!-- tabs:start -->
3333

3434
#### **Golang**
3535
```go
36+
func mySqrt(x int) int {
37+
if x == 1 {
38+
return 1
39+
}
40+
l, r := 0, x
41+
for {
42+
mid := (l + r) / 2
43+
if mid == l {
44+
return mid
45+
}
46+
sqr := mid * mid
47+
if sqr > x {
48+
r = mid
49+
}
50+
if sqr < x {
51+
l = mid
52+
}
53+
if sqr == x {
54+
return mid
55+
}
56+
}
57+
}
3658

3759
```
3860

39-
<!-- tabs:end -->
4061

‎solution/1-99/0069.sqrtx/solution.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=69 lang=golang
5+
*
6+
* [69] x 的平方根
7+
*/
8+
9+
// @lc code=start
10+
func mySqrt(x int) int {
11+
if x == 1 {
12+
return 1
13+
}
14+
l, r := 0, x
15+
for {
16+
mid := (l + r) / 2
17+
if mid == l {
18+
return mid
19+
}
20+
sqr := mid * mid
21+
if sqr > x {
22+
r = mid
23+
}
24+
if sqr < x {
25+
l = mid
26+
}
27+
if sqr == x {
28+
return mid
29+
}
30+
}
31+
}
32+
33+
// @lc code=end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMySqrt(t *testing.T) {
8+
var x int
9+
var ret int
10+
x = 8
11+
ret = 2
12+
if ret != mySqrt(x) {
13+
t.Fatalf("case fails %v\n", ret)
14+
}
15+
16+
x = 1
17+
ret = 1
18+
if ret != mySqrt(x) {
19+
t.Fatalf("case fails %v\n", ret)
20+
}
21+
22+
x = 101
23+
ret = 10
24+
if ret != mySqrt(x) {
25+
t.Fatalf("case fails %v\n", ret)
26+
}
27+
}

‎solution/1-99/_sidebar.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- [11. 盛最多水的容器 ✅](solution/1-99/0011.container-with-most-water/)
2020
- [12. 整数转罗马数字](solution/1-99/0012.integer-to-roman/)
2121
- [13. 罗马数字转整数 ✅](solution/1-99/0013.roman-to-integer/)
22-
- [14. 最长公共前缀](solution/1-99/0014.longest-common-prefix/)
22+
- [14. 最长公共前缀](solution/1-99/0014.longest-common-prefix/)
2323
- [15. 三数之和 ✅](solution/1-99/0015.3sum/)
2424
- [16. 最接近的三数之和](solution/1-99/0016.3sum-closest/)
2525
- [17. 电话号码的字母组合](solution/1-99/0017.letter-combinations-of-a-phone-number/)
@@ -74,7 +74,7 @@
7474
- [66. 加一 ✅](solution/1-99/0066.plus-one/)
7575
- [67. 二进制求和](solution/1-99/0067.add-binary/)
7676
- [68. 文本左右对齐](solution/1-99/0068.text-justification/)
77-
- [69. x 的平方根](solution/1-99/0069.sqrtx/)
77+
- [69. x 的平方根](solution/1-99/0069.sqrtx/)
7878
- [70. 爬楼梯](solution/1-99/0070.climbing-stairs/)
7979
- [71. 简化路径](solution/1-99/0071.simplify-path/)
8080
- [72. 编辑距离](solution/1-99/0072.edit-distance/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /