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 ...
随机推荐
- 远程管理服务器--批量管理服务器,vps
一般大型的企事业单位都有自己的服务器,但是服务器一般都放在机房,辐射较大,噪音大,如何能有效的避免这一情况呢?哈哈,那就来个远程桌面,远程操作服务器吧. 一.使用 iis7远程连接管理工具工具下载官网 ...
- 用Django加PIL做一个证件照模板生成器网页
最近在整理自己的简历,发现简历上面的ID照有些太老了,所以就准备重新准备一些证件照,刚好最近在弄自己的博客网站,想着直接做一个网页工具出来,直接生成证件照模板,这样还可以省去PS的麻烦.而且照片涉及到 ...
- 07Shell数组
Shell 数组变量 普通数组:只能使用整数作为数组索引 关联数组:可以使用字符串作为数组索引 普通数组 定义数组 方法1: 一次赋一个值 数组名[索引]=变量值 示例 # array1[0]=pea ...
- springboot-实现文件下载
一 前言 本文实现的文件下载是使用Apache 的 commons-fileupload 实现:在之前的springboot系列文件中已经讲述过如何实现多文件上传:这篇文件实现的文件下载功能主要是能在 ...
- docker概述和基本命令
命名空间 Docker使用一种称为namespaces提供隔离工作空间的技术来称为容器.当您运行容器时,Docker会为该容器创建一组 名称空间. 这些命名空间提供了一层隔离.容器的每个方面都在一个单 ...
- SCU 4438 Censor|KMP变形题
传送门 Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text P. He ...
- leetcode腾讯精选练习之两数相加
两数相加 题目: 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们 ...
- C#调用Fortran生成的DLL的方法报内存不足
最近在研究一个程序,公司给的,程序是VB写的,程序里面还有一个计算的模型,用Fortran语言写的. 在调试到这个模型里面的方法时报错,说是内存不足,于是就在网上查找方法,看了两篇博客之后问题解决了. ...
- 修饰符new与override
new:在作为修饰符时,可以隐藏从父类的继承的成员. override:修改父类的方法.属性. 上代码比较清楚: using System; using System.Collections.Gene ...
- 详细解析Redis中的布隆过滤器及其应用
欢迎关注微信公众号:万猫学社,每周一分享Java技术干货. 什么是布隆过滤器 布隆过滤器(Bloom Filter)是由Howard Bloom在1970年提出的一种比较巧妙的概率型数据结构,它可以告 ...