Skip to content

Commit 22d2bbb

Browse files
committed
二刷69
1 parent 08a6b7e commit 22d2bbb

File tree

4 files changed

+81
-21
lines changed

4 files changed

+81
-21
lines changed

docs/0069-sqrtx.adoc

+45-20
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
11
[#0069-sqrtx]
2-
= 69. Sqrt(x)
2+
= 69. x 的平方根
33

4-
{leetcode}/problems/sqrtx/[LeetCode - Sqrt(x)^]
4+
https://leetcode.cn/problems/sqrtx/[LeetCode - 69. x 的平方根 ^]
55

6-
明知使用的是二分查找,竟然没有写出来!!
6+
给你一个非负整数 `x` ,计算并返回 `x`*算术平方根*
77

8-
Implement `int sqrt(int x)`.
8+
由于返回类型是整数,结果只保留 *整数部分*,小数部分将被 *舍去*
99

10-
Compute and return the square root of _x_, where _x_ is guaranteed to be a non-negative integer.
10+
**注意:**不允许使用任何内置指数函数和算符,例如 `pow(x, 0.5)` 或者 `x^0.5^`
1111

12-
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
12+
*示例 1:*
1313

14-
*Example 1:*
14+
....
15+
输入:x = 4
16+
输出:2
17+
....
1518

16-
[subs="verbatim,quotes,macros"]
17-
----
18-
*Input:* 4
19-
*Output:* 2
20-
----
19+
*示例 2:*
2120

22-
*Example 2:*
21+
....
22+
输入:x = 8
23+
输出:2
24+
解释:8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
25+
....
2326

24-
[subs="verbatim,quotes,macros"]
25-
----
26-
*Input:* 8
27-
*Output:* 2
28-
*Explanation:* The square root of 8 is 2.82842..., and since
29-
the decimal part is truncated, 2 is returned.
30-
----
27+
*提示:*
28+
29+
* `0 \<= x \<= 2^31^ - 1`
30+
31+
32+
== 思路分析
3133

34+
明知使用的是二分查找,竟然没有写出来!!
35+
36+
使用 `result` 字段用于存储最后一个小于等于的情况,这样就能直接获取答案,省去很多判断。
3237

3338
[[src-0069]]
39+
[tabs]
40+
====
41+
一刷::
42+
+
43+
--
3444
[{java_src_attr}]
3545
----
3646
include::{sourcedir}/_0069_SqrtX.java[tag=answer]
3747
----
48+
--
49+
50+
二刷::
51+
+
52+
--
53+
[{java_src_attr}]
54+
----
55+
include::{sourcedir}/_0069_SqrtX_2.java[tag=answer]
56+
----
57+
--
58+
====
59+
60+
61+
== 参考资料
3862

63+
. https://leetcode.cn/problems/sqrtx/solutions/238553/x-de-ping-fang-gen-by-leetcode-solution/[69. x 的平方根 - 官方题解^]

logbook/202503.adoc

+6-1
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,16 @@ endif::[]
508508
|{doc_base_url}/0049-group-anagrams.adoc[题解]
509509
|✅ 使用 `Map` 存字符和数量即可。
510510

511-
|{counter:codes}
511+
|{counter:codes2503}
512512
|{leetcode_base_url}/subarray-sum-equals-k/[560. 和为 K 的子数组^]
513513
|{doc_base_url}/0560-subarray-sum-equals-k.adoc[题解]
514514
|✅ 前缀和。需要记录每一个和的出现次数。
515515

516+
|{counter:codes2503}
517+
|{leetcode_base_url}/sqrtx/[69. x 的平方根^]
518+
|{doc_base_url}/0069-sqrtx.adoc[题解]
519+
|✅ 二分查找。使用一个单独变量来保存最后一个小于等于的中间值,那么即可直接获取答案。
520+
516521
|===
517522

518523
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0069_SqrtX.java

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class _0069_SqrtX {
3838
* Memory Usage: 34.1 MB, less than 5.00% of Java online submissions for Sqrt(x).
3939
*
4040
* Copy from: https://leetcode.com/problems/sqrtx/discuss/25047/A-Binary-Search-Solution[A Binary Search Solution - LeetCode Discuss]
41+
*
42+
* @author D瓜哥 · https://www.diguage.com
43+
* @since 2020-01-13 22:30
4144
*/
4245
public int mySqrt(int x) {
4346
if (x == 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0069_SqrtX_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-04-27 00:05:40
9+
*/
10+
public int mySqrt(int x) {
11+
int left = 0, right = x, result = 0;
12+
while (left <= right) {
13+
int mid = left + (right - left) / 2;
14+
if ((long) mid * mid <= x) {
15+
result = mid;
16+
left = mid + 1;
17+
} else {
18+
right = mid - 1;
19+
}
20+
}
21+
return result;
22+
}
23+
// end::answer[]
24+
public static void main(String[] args) {
25+
new _0069_SqrtX_2().mySqrt(2147395599);
26+
}
27+
}

0 commit comments

Comments
 (0)