Movatterモバイル変換


[0]ホーム

URL:


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

Commit7b96e63

Browse files
author
applewjg
committed
Sqrt
Change-Id: I1fcca7720537bd8777b820629991c01ee03d4f98
1 parenta3b9763 commit7b96e63

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎Sqrt(x).java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Apr 18, 2013
4+
Problem: Sqrt(x)
5+
Difficulty: Easy
6+
Source: http://leetcode.com/onlinejudge#question_69
7+
Notes:
8+
Implement int sqrt(int x).
9+
Compute and return the square root of x.
10+
11+
Solution: 1. Binary search in range [0, x / 2 + 1].
12+
2. Newton iteration method. x(i+1) = (x(i) + n/x(i)) / 2.
13+
See my blog (http://www.cnblogs.com/AnnieKim/archive/2013/04/18/3028607.html) for more explanation (in Chinese).
14+
*/
15+
publicclassSolution {
16+
publicintsqrt(intx) {
17+
intleft =1,right =x/2;
18+
if(x<2)returnx;
19+
while (left <=right) {
20+
intmid = (left +right)/2;
21+
if (x/mid ==mid)returnmid;
22+
if (x/mid >mid)left =mid +1;
23+
elseright =mid -1;
24+
}
25+
returnright;
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp