/*
* @lc app=leetcode.cn id=69 lang=c
*
* [69] x 的平方根
*
* https://leetcode-cn.com/problems/sqrtx/description/
*
* algorithms
* Easy (34.81%)
* Total Accepted: 25.4K
* Total Submissions: 73.1K
* Testcase Example: '4'
*
* 实现 int sqrt(int x) 函数。
*
* 计算并返回 x 的平方根,其中 x 是非负整数。
*
* 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
*
* 示例 1:
*
* 输入: 4
* 输出: 2
*
*
* 示例 2:
*
* 输入: 8
* 输出: 2
* 说明: 8 的平方根是 2.82842...,
* 由于返回类型是整数,小数部分将被舍去。
*
*
*/
int mySqrt(int x) {
long i = ;
if(x==){
return ;
}
for(i=;i<=x/;i++){
if(i*i==x){
return i;
}
else if(i*i<x&&(i+)*(i+)>x){
return i;
}
}
return ;
}

这里要注意取值范围,用long比较合适。思路也挺简单的,循环到目标值的一半即可,然后两个判断,要么相等,要么当前值平方小于目标值,当前值加1的平方大于目标值。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=69 lang=python3
#
# [69] x 的平方根
#
# https://leetcode-cn.com/problems/sqrtx/description/
#
# algorithms
# Easy (34.81%)
# Total Accepted: 25.4K
# Total Submissions: 73.1K
# Testcase Example: '4'
#
# 实现 int sqrt(int x) 函数。
#
# 计算并返回 x 的平方根,其中 x 是非负整数。
#
# 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
#
# 示例 1:
#
# 输入: 4
# 输出: 2
#
#
# 示例 2:
#
# 输入: 8
# 输出: 2
# 说明: 8 的平方根是 2.82842...,
# 由于返回类型是整数,小数部分将被舍去。
#
#
#
class Solution:
def mySqrt(self, x: int) -> int:
low = 0
mid = x // 2 # //代表整数除法
high = x
while low <= high: #注意判断条件
if mid * mid > x:
high = mid - 1
elif mid * mid < x:
low = mid + 1
else:
return mid
mid = (low + high) // 2
return mid #向下取整

python用的是二分法。

Leecode刷题之旅-C语言/python-69x的平方根的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  10. Leecode刷题之旅-C语言/python-349两个数组的交集

    /* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...

随机推荐

  1. supervisor运行virtualenv环境下的nagios-api

    supervisord-example.conf [unix_http_server] file=/tmp/supervisor.sock ; path to your socket file [su ...

  2. C#设计模式之代理模式(四)

    15.7 代理模式效果与适用场景 代理模式是常用的结构型设计模式之一,它为对象的间接访问提供了一个解决方案,可以对对象的访问进行控制.代理模式类型较多,其中远程代理.虚拟代理.保护代理等在软件开发中应 ...

  3. 猿创|有赞的zan framework安装与使用

    1.准备工作 1.1 一台腾讯云服务器2核CPU+2G内存的Linux CentOS 7.2(谢谢小杨同学@erchoc) 1.2 预装lnmp一键安装包环境,官方地址:lnmp一键安装包 (如不会使 ...

  4. 如何在ubuntu上安装virtualbox的driver module vboxdrv

    干净的ubuntu安装完毕之后是没有vboxdrv这个driver module的. 新建一个folder jerry_virtualbox: 使用wget下载virtualbox安装包:https: ...

  5. python接口测试-项目实践(三)数据的处理示例

    三 数据处理 枚举值的转换.如接口返回1-5,需转成对应的中文. typecap = findinfo_from_api(result, 'TypeCap') dictcap = {': '微盘'} ...

  6. python入门13 集合set

    set集合与数学中的集合同一个概念,是无序不重复元素组成的. #coding:utf-8 #/usr/bin/python """ 2018-11-10 dinghanh ...

  7. github desktop项目版本控制

    [git版本控制-笔记]by lijun   0.推荐学习网址:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b ...

  8. ms17_010利用复现(32位)

    准备阶段: 1,原版windows7:cn_windows_7_enterprise_x86_dvd_x15-70737.iso 2,kali系统,  虚拟机 3,用于32位机的攻击模块:Eterna ...

  9. prior_box层

    https://www.jianshu.com/p/5195165bbd06 1.step_w.step_h其实就相当于faster中的feat_stride,也就是把这些点从feature map映 ...

  10. Hibernate的属性配置

    Hibernate配置属性 hibernate.dialect Hibernate方言(Dialect)的类名 - 可以让Hibernate使用某些特定的数据库平台的特性 取值. full.class ...