Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x)
Implement int sqrt(int x).
Compute and return the square root of x.
注:这里的输入输出都是整数说明不会出现 sqrt(7)这种情况,思路一就是应用二分法进行查找。每次给出中间值,然后比对cur的平方与目标值的大小。需要先设定两个变量用来存放左右游标。
这里要考虑一下整型溢出的问题,另外,即使不能开出整数的也要近似给出整数部分,不能忽略。
代码如下:
int Solution::mySqrt(int x) {
//输入一个整数,输出它的平方根
if (x == 1)
return x;
long long int s;
s = x / 2;
long long int leftflag = 0, rightflag = x;
while (s*s != x && leftflag+1<rightflag)
{
if (s*s < x)
{//在右侧区间内寻找
leftflag = s;
}
else
{
rightflag = s;
}
s = (leftflag + rightflag) / 2;
cout << "left " << leftflag << endl;
cout << "right" << rightflag << endl;
cout << "s" << s << endl;
}
return s;
/*来自博客的一个参考,更简练一些*/
//long long left = 0, right = (x / 2) + 1;
//while (left <= right) {
// long long mid = (left + right) / 2;
// long long sq = mid * mid;
// if (sq == x) return mid;
// else if (sq < x) left = mid + 1;
// else right = mid - 1;
//}
//return right;
}
Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
注:给出一个矩阵(有序的)和目标值,找到目标值所在位置(如果有),或者应该插入的位置(如果没有)。
思路:从左向右进行遍历,发现第一个不小于目标值的元素时,对位置进行记录。
代码如下:
int Solution::searchInsert(vector<int>& nums, int target) {
int position=0;
vector<int>::iterator iter = nums.begin();
while (iter != nums.end() && target > *iter)// && target != *iter)
{
iter++; position++;
}
return position;
}
Leetcode 题目整理 Sqrt && Search Insert Position的更多相关文章
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- LeetCode Arrary Easy 35. Search Insert Position 题解
Description Given a sorted array and a target value, return the index if the target is found. If not ...
- LeetCode(35) Search Insert Position
题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode记录之35——Search Insert Position
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
随机推荐
- JMM&Thread
1.概述 高效并发通过JAVA线程之间提高并发协调实现,在实现过程中需考虑硬件的效率和一致性,但在运算的过程中需要考虑处理器与内存的交互,所以基于高速缓存的存储交互解决的处理器与内存的方案,在对多处理 ...
- Mongdb的基本操作及java中用法
Mongdb中所有数据以Bson(类似JSON)的格式存在,可以存储集合,map,二进制文件等多种数据类型. 数据库的常用操作 use [数据库名称];//有就选中,没有就添加并选中show dbs; ...
- The fifth day of Crawler learning
使用mongoDB 下载地址:https://www.mongodb.com/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl ...
- centos6.x将python2.6升级到2.7
一,安装开发工具和Python2.7(1)查看当前python版本 python -V Python 2.6.6 (2)下载Python-2.7.3 wget http://python.org/ft ...
- alpha week 1/2 Scrum立会报告+燃尽图 06
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/9916 小组名称:“组长”组 组长:杨天宇 组员:魏新,罗杨美慧,王歆瑶, ...
- 高阶函数HOF和高阶组件HOC(Higher Order Func/Comp)
一.什么是高阶函数(组件),作用是什么? 子类使用父类的方法可以通过继承的方式实现,那无关联组件通信(redux).父类使用子类方法(反向继承)呢 为了解决类(函数)功能交叉/功能复用等问题,通过传入 ...
- 1027 打印沙漏 (20 分)C语言
题目描述 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ***** 所谓"沙漏形状" ...
- 机器学习实战笔记(一)- 使用SciKit-Learn做回归分析
一.简介 这次学习的书籍主要是Hands-on Machine Learning with Scikit-Learn and TensorFlow(豆瓣:https://book.douban.com ...
- ACWing 248. 窗内的星星|扫描线+懒惰标记
传送门 题目描述 在一个天空中有很多星星(看作平面直角坐标系),已知每颗星星的坐标和亮度(都是整数). 求用宽为W.高为H的矩形窗户(W,H为正整数)能圈住的星星的亮度总和最大是多少.(矩形边界上的星 ...
- 解决Maven项目中的无故报错的方法
解决Eclipse+maven中的无故报错 错误: One or more constraints have not been satisfied. Deployment Assembly跟java版 ...