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;
}
时间: 2024-11-03 01:17:47

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, 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 th

LeetCode(35) 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]

leetcode第34题--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 →

LeetCode 035 Search Insert Position

题目要求: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

[LeetCode] Search Insert Position [21]

题目 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]

[LeetCode] 035. Search Insert Position (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search Insert Position (Medium) 链接: 题目:https://leetcode.com/problems/search-insert-position/ 代码(github):https://github.com/illuz/leetcode 题意: 要把一个数有序插入到一

LeetCode: Search Insert Position [034]

[题目] 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,

Search Insert Position leetcode java

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      

LeetCode——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