0
点赞
收藏
分享

微信扫一扫

【LeeCode】

生活记录馆 2023-03-06 阅读 88

【题目描述】

给你一个非负整数 ​​x​​​ ,计算并返回 ​​x​​ 的 算术平方根 。

由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。

注意:不允许使用任何内置指数函数和算符,例如 ​​pow(x, 0.5)​​ 或者 ​​x ** 0.5​​ 。

 ​​https://leetcode.cn/problems/sqrtx/​​


【示例】

【LeeCode】_java

【代码】admin

package com.company;
// 2023-03-06
import java.util.*;

class Solution {
public int mySqrt(int x) {
double sqrt = Math.sqrt(x);
int res = (int)Math.floor(sqrt);
return res;
}
}

public class Test {
public static void main(String[] args) {
new Solution().mySqrt(4); // 输出: 2
new Solution().mySqrt(8); // 输出: 2
}
}

举报

相关推荐

0 条评论