首页 > 其他 > 详细

8.Leetcode69 Sqrt(x) 笔记

时间:2018-02-03 22:36:40      阅读:247      评论:0      收藏:0      [点我收藏+]

1.题目描述

mplement int sqrt(int x).

Compute and return the square root of x.

x is guaranteed to be a non-negative integer.

求开方,输出为整数型

2.题目分析

python自带sqrt函数,但还是选择了牛顿迭代法,算顺便眷顾一下微积分

3.解题过程

class Solution(object):
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x==0:    #首先排除0
return 0 x=float(x) #强制转换浮点型 x1=float(x/2) x2=float((x1+x/x1)/2) while abs((x2-x1))>0.1: # 不能忘了绝对值 x1=x2 x2=float((x1+x/x1)/2) sqrt_x=int(x2) #强制转换整数型 return sqrt_x

 

8.Leetcode69 Sqrt(x) 笔记

原文:https://www.cnblogs.com/19991201xiao/p/8410837.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!