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 ...
随机推荐
- k8s集群———etcd-ssl自签名证书
etcd集群master节点安装 ,自签名SSL证书 ##安装工具cfssl $ cat cfssl.sh curl -L https://pkg.cfssl.org/R1.2/cfssl_linux ...
- 洛谷$P4318$ 完全平方数 容斥+二分
正解:容斥/杜教筛+二分 解题报告: 传送门$QwQ$ 首先一看这数据范围显然是考虑二分这个数然后$check$就计算小于等于它的不是讨厌数的个数嘛. 于是考虑怎么算讨厌数的个数? 看到这个讨厌数说, ...
- [MySQL实践] 实践记录
[MySQL实践] 实践记录 版权2019.5.17更新 MySQL MySQL各版本区别 一.选择的版本 1. MySQL Community Server 社区版本,开源免费,但不提供官方技术支持 ...
- CF1272D. Remove One Element 题解 动态规划
题目链接:http://codeforces.com/contest/1272/problem/D 题目大意: 给你一个长度为 \(n\) 的数组,你最多删除一个元素(也可以不删),求此条件限制下的最 ...
- 「洛谷P1306」斐波那契公约数 解题报告
P1306 斐波那契公约数 题目描述 对于Fibonacci数列:1,1,2,3,5,8,13......大家应该很熟悉吧~~~但是现在有一个很"简单"问题:第n项和第m项的最大公 ...
- Web漏洞总结: OWASP Top 10
本文原创,更多内容可以参考: Java 全栈知识体系.如需转载请说明原处. 开发安全 - OWASP Top 10 在学习安全需要总体了解安全趋势和常见的Web漏洞,首推了解OWASP,因为它代表着业 ...
- MinIO 搭建使用
MinIO简介¶ MinIO 是一款基于Go语言的高性能对象存储服务,在Github上已有19K+Star.它采用了Apache License v2.0开源协议,非常适合于存储大容量非结构化的数据, ...
- Java虚拟机OOM问题和四大引用问题简述
一.请你谈谈实际的项目中在Java虚拟机会抛出哪些异常,每个异常都是怎么产生的? 1.java.lang.StackOverflowError 栈空间满了 public static void sta ...
- input值
input里面的值为字符串(string)类型,所以用作数的计算的时候需要用Number(mInput.value)进行转换成数值Numbei()类型才可以计算 例如: mInput1.value + ...
- 详解定时任务中的 cron 表达式
1.前言 我们经常使用 cron 表达式来定义定时任务的执行策略,今天我们就总结一下 cron 表达式的一些相关知识. 2. cron 表达式的定义 cron 表达式是一个字符串,该字符串由 6 个空 ...