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 ...
随机推荐
- WCF 的优势和特点
版权声明:本文为博主原创文章,未经博主允许不得转载. 一.理解面向服务(Service-Oriented-Architecture) 是指为了解决在Internet环境下业务集成的需要,通过连接 ...
- 开源监控zabbix的搭建
yum-nginx-php-mysql 1.依赖安装 1.依赖 yum -y install pcre* openssl* 2.php rpm -Uvh https://mirror.webtatic ...
- 服务器常用命令之 启用/禁用PING状态
启用 echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all (PING的通) 禁用 echo 1 > /proc/sys/net/ipv4/i ...
- Flutter仿照airbnb创建app
github地址:https://github.com/GainLoss/flutter-app 一.基础 flutter是谷歌公司开发的开源免费的UI框架,用dart语言实现的,可以实现跨平台,一套 ...
- python入门13 集合set
set集合与数学中的集合同一个概念,是无序不重复元素组成的. #coding:utf-8 #/usr/bin/python """ 2018-11-10 dinghanh ...
- OC 方法声明使用
Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; } - (void) ...
- nginx安装和基础代理配置
mac上执行 npm install nginx 安装好后运行nginx sudo nginx 一般mac下nginx会安装在 /usr/local/etc/nginx 下 里面的nginx.conf ...
- IntelliJ IDEA 快速搭建 Spring MVC环境
IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.Ant.JUnit.CVS整合.代码审查. 创新的GUI设计等方面的功能可以说是超常的. ...
- Win7系统中哪些服务可以关闭?
Win7系统中很多服务和进程其实大家在平时都用不上,这些服务和进程不仅占用了很大部分的资源,而且还会影响系统的运行速度和安全.我们可以关闭那些不常用的服务和进程来加快系统运行速度,但对于一般的用户来说 ...
- mybatis学习记录四——输入、输出映射
6 输入映射 通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型. 6.1.1 需求 完成用户信息的综合查询,需要传入查询条件很 ...