LeetCode_69. Sqrt(x)

LeetCode_69. Sqrt(x),第1张

概述  69. Sqrt(x) Easy Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are t

 

69. Sqrt(x)

Easy

Implement int sqrt(int x).

Compute and return the square root of x,where x is guaranteed to be a non-negative integer.

Since the return type is an integer,the decimal digits are truncated and only the integer part of the result is returned.

Example 1:

input: 4Output: 2

Example 2:

input: 8Output: 2Explanation: The square root of 8 is 2.82842...,and since              the decimal part is truncated,2 is returned.

 

package leetcode.easy;public class SqrtX {	@org.junit.Test	public voID test() {		long x1 = 4;		long x2 = 8;		System.out.println(mySqrt(x1));		System.out.println(mySqrt(x2));	}	public int mySqrt(long x) {		for (long i = 0; i <= x; i++) {			if (i * i == x) {				return (int) i;			} else if (i * i < x && (i + 1) * (i + 1) > x) {				return (int) i;			} else {				continue;			}		}		return 0;	}}
总结

以上是内存溢出为你收集整理的LeetCode_69. Sqrt(x)全部内容,希望文章能够帮你解决LeetCode_69. Sqrt(x)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/1023100.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存