Leecode刷题之旅-C语言/python-69x的平方根
/*
* @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的平方根的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
- Leecode刷题之旅-C语言/python-349两个数组的交集
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
随机推荐
- Matlab函数——awgn(高斯噪声)
Matlab函数--awgn awgn 将白色高斯噪声添加到信号中 语法 y = awgn(x,snr) y = awgn(x,snr,sigpower) y = awgn(x,snr,'mea ...
- windows下使用VNC进行远程连接
在 windows 电脑上安装 VNC,包含 VNC server 和 VNC viewer,如果仅需要被操控或操控他人,选择型下载安装 VNC server 或 VNC viewer 即可. 在需要 ...
- vue项目运行报错:Module bulid failed: Error: Node Sass does not yet support your current environment
出错起因: 从GitLab clone项目 --> 用 npm install 命令下载依赖包 --> #npm run dev,报错 错误截图: 解决方法: 思路:单独 i ...
- 移动端调试工具weinre
前段时间在大豹公众号上看到一个关于移动端调试的工具,了解了一下,确实不错. npm install -g weinre 先全局安装,然后使用的时候通过如下命令启动 weinre --boundHost ...
- 笨办法学Python(三十九)
习题 39: 列表的操作 你已经学过了列表.在你学习“while 循环”的时候,你对列表进行过“追加(append)”操作,而且将列表的内容打印了出来.另外你应该还在加分习题里研究过 Python 文 ...
- 推荐一个很好用的脚本session snapper
源网址http://tech.e2sn.com/oracle-scripts-and-tools/session-snapper 内容: If you want to just download Sn ...
- IOS KVO的实现原理
#import "HMViewController.h" #import "HMPerson.h" @interface HMViewController () ...
- A potentially dangerous Request.Form value was detected from the client的解决办法
网上找了这么多,这条最靠谱,记录下来,以备后用 <httpRuntime requestValidationMode="2.0"/> <pages validat ...
- Catalan数列
引入 今天听学长讲了卡特兰数列后对其有了更深的认识,在此完善了一下之前的博客加以总结. 首先用一个经典的例子来描述一下Catalan数列,我们有一个1~n的数列和一个大小为n的栈,我们有如下两种操作: ...
- 郑州Day6
今天考了毕姥爷的一套题,差点保龄 题目 挺良心的一套题,至少我不用再搬一遍题面了 T1.B君的第一题 我为什么当时去写了一个树形\(dp\)还妄图\(A\)掉啊 这题保龄感觉舒爽 首先如果我们要求的是 ...