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 ...
随机推荐
- 关于Qt中窗口的坐标
主要是给自己以后参考,所以不会太仔细的讲解. #include "mainwindow.h" #include <QApplication> #include<Q ...
- 0018 CSS注释(简单)
CSS注释规则: /* 需要注释的内容 */ 进行注释的,即在需要注释的内容前使用 "/*" 标记开始注释,在内容的结尾使用 "*/"结束. 例如: p { / ...
- Asp.net导入Excel并读取数据
protected void Button1_Click(object sender, EventArgs e) { if (station.HasFile == false)//HasFile用来检 ...
- 不仅仅是双11大屏—Flink应用场景介绍
双11大屏 每年天猫双十一购物节,都会有一块巨大的实时作战大屏,展现当前的销售情况. 这种炫酷的页面背后,其实有着非常强大的技术支撑,而这种场景其实就是实时报表分析. 实时报表分析是近年来很多公司采用 ...
- $NOIp$普及组做题记录
\([NOIp2014]\) 螺旋矩阵 \(Sol\) 直接模拟,一次走一整行或者一整列.复杂度\(O(n)\). \(Code\) #include<bits/stdc++.h> #de ...
- solr学习(一)安装与部署
经过测试,同步MongoDB数据到Solr的时候,Solr版本为8.4.0会出现连接不上的错误,8.3.0未经测试不知,博主测试好用的一版为8.2.0,但是官网已经下不到了,所以我会把下载链接放在文末 ...
- git submodule 管理子项目
使用场景 拆分项目,当项目越来越大之后,我们希望 子模块 可以单独管理,并由 专门 的人去维护,这个时候只可以使用 git submodule 去完成. 常用命令 git clone <repo ...
- iOS开发常见问题
1. 在 ViewController 中添加子视图时,导航栏遮挡添加的子视图 let bpView = BpView.init(frame: CGRect.init(x: , y: , width: ...
- ipynb 文件转 py
前言 好多机器学习的文件都是ipynb的格式,如果想用其他编辑器的需要转为py的格式 命令: jupyter nbconvert --to script *.ipynb jupyter: pip3 i ...
- 如何使用F4的IRAM2内存
在使用KEIL做F4的项目的时候发现RAM区有片上IRAM2选项,查了datesheet后发现这块是CCM内存区 CCM内存是在地址0x1000000映射的64KB块,只提供CPU通过数据D总线进行访 ...