File tree 4 files changed +81
-21
lines changed
src/main/java/com/diguage/algo/leetcode
4 files changed +81
-21
lines changed Original file line number Diff line number Diff line change 1
1
[#0069-sqrtx]
2
- = 69. Sqrt(x)
2
+ = 69. x 的平方根
3
3
4
- { leetcode} /problems/sqrtx/[LeetCode - Sqrt(x) ^]
4
+ https:// leetcode.cn /problems/sqrtx/[LeetCode - 69. x 的平方根 ^]
5
5
6
- 明知使用的是二分查找,竟然没有写出来!!
6
+ 给你一个非负整数 `x` ,计算并返回 `x` 的 *算术平方根* 。
7
7
8
- Implement `int sqrt(int x)` .
8
+ 由于返回类型是整数,结果只保留 *整数部分* ,小数部分将被 *舍去* 。
9
9
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^` 。
11
11
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:*
13
13
14
- *Example 1:*
14
+ ....
15
+ 输入:x = 4
16
+ 输出:2
17
+ ....
15
18
16
- [subs="verbatim,quotes,macros"]
17
- ----
18
- *Input:* 4
19
- *Output:* 2
20
- ----
19
+ *示例 2:*
21
20
22
- *Example 2:*
21
+ ....
22
+ 输入:x = 8
23
+ 输出:2
24
+ 解释:8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
25
+ ....
23
26
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
+ == 思路分析
31
33
34
+ 明知使用的是二分查找,竟然没有写出来!!
35
+
36
+ 使用 `result` 字段用于存储最后一个小于等于的情况,这样就能直接获取答案,省去很多判断。
32
37
33
38
[[src-0069]]
39
+ [tabs]
40
+ ====
41
+ 一刷::
42
+ +
43
+ --
34
44
[{java_src_attr}]
35
45
----
36
46
include::{sourcedir}/_0069_SqrtX.java[tag=answer]
37
47
----
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
+ == 参考资料
38
62
63
+ . https://leetcode.cn/problems/sqrtx/solutions/238553/x-de-ping-fang-gen-by-leetcode-solution/[69. x 的平方根 - 官方题解^]
Original file line number Diff line number Diff line change @@ -508,11 +508,16 @@ endif::[]
508
508
|{doc_base_url} /0049-group-anagrams.adoc[题解]
509
509
|✅ 使用 `Map` 存字符和数量即可。
510
510
511
- |{counter:codes }
511
+ |{counter:codes2503 }
512
512
|{leetcode_base_url} /subarray-sum-equals-k/[560. 和为 K 的子数组^]
513
513
|{doc_base_url} /0560-subarray-sum-equals-k.adoc[题解]
514
514
|✅ 前缀和。需要记录每一个和的出现次数。
515
515
516
+ |{counter:codes2503}
517
+ |{leetcode_base_url} /sqrtx/[69. x 的平方根^]
518
+ |{doc_base_url} /0069-sqrtx.adoc[题解]
519
+ |✅ 二分查找。使用一个单独变量来保存最后一个小于等于的中间值,那么即可直接获取答案。
520
+
516
521
|===
517
522
518
523
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line number Diff line number Diff line change @@ -38,6 +38,9 @@ public class _0069_SqrtX {
38
38
* Memory Usage: 34.1 MB, less than 5.00% of Java online submissions for Sqrt(x).
39
39
*
40
40
* 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
41
44
*/
42
45
public int mySqrt (int x ) {
43
46
if (x == 0 ) {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments