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 ...
随机推荐
- 【题解】P4755 Beautiful Pair(启发式合并的思路+分治=启发式分治)
[题解]P4755 Beautiful Pair upd: 之前一个first second烦了,现在AC了 由于之前是直接抄std写的,所以没有什么心得体会,今天自己写写发现 不知道为啥\(90\) ...
- 开源项目SMSS开发指南
SMSS是一个由我个人发起的开源项目,目的是建立一套轻量化,高可用,高安全和方便扩展的业务支撑框架.SMSS面向TCP/IP层开发,适合扩展上层业务接口.数据结构传输序列化通过Protobuf实现.传 ...
- Spring命令行参数
一般我们通过java -jar xxx.jar的方式启动应用,其实除了启动应用我们还能在命令中指定应用的参数,比如java -jar xxx.jar --server.port=1234,直接以命令行 ...
- maven中scope标签各个值的意义
在使用maven配置时,有时候会见到scope这个标签,但是总是记不住他们所对应的含义,现在整理一下,以后忘记了再来查看. 版权声明:本文为CSDN博主「MrZhangBaby」的原创文章,遵循 CC ...
- 变量的取用与设定:echo,变量设定规则,unset
1.变量的取用echo echo $variable echo ${variable} 2.变量的设定规则 3.让我设定的name=VBird应用在下个应用程序 4.进入到核心的模块目录 5.取消设定 ...
- 自定义实现的ArrayList以及自定义实现的Iterator迭代器
ArrayList的底层是长度可动态变化的数组,其适用于查找多,修改少的情况,原因是数组的元素的增加删除元素会涉及大量元素的移动,效率比较低,ArrayList中的元素可以重复,与插入时的顺序相同,可 ...
- Ubuntu16安装NVIDIA驱动后重复登录 简单粗暴
第一步 卸载所有NVIDIA的东西 第二步 开机,应该能进入默认驱动的桌面了,在设置里关闭开机密码,开机自动登录 第三步 安装英伟达驱动
- 大白话建造者模式(Builder Pattern)
前言 起初打算按照之前的日产系列写建造者模式.但参考了网上的很多文章,让我对建造者模式更加的困惑,也害怕自己无法已易懂的方式进行解释.最后通过Google发现了一篇英文文章Builder,使我茅塞顿开 ...
- dp-LCS(递归输出最短合串)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- IDEA 公司推出新字体,极度舒适~
这几天炒得沸沸扬扬的 Intellij IDEA 公司 JetBrains 推出了一种新字体:JetBrains Mono,据说它是专为开发人员设计的,下面栈长带大家一起来吃个瓜. JetBrains ...