第10题:寻找插入位置

第10题:寻找插入位置

给定一个已经升序排好序的数组,以及一个数target,如果target在数组中,返回它在数组中的位置。

否则,返回target插入数组后它应该在的位置。

假设数组中没有重复的数。以下是简单的示例:

[1,3,5,6], 5 → 2

[1,3,5,6], 2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6], 0 → 0

提示:输入一个整数n,以及其对应的数组A[n],最后输入target

searchInsert(int A[], int n, int target)

样例输入

3
1 3 5
2

样例输出

1

答案:

#include <stdio.h>

int searchInsert(int A[], int n, int target);

int main()

{
	int n,A[10000],target,i;
	scanf("%d",&n);
	for(i = 0;i < n;i++)
	{
		scanf("%d",&A[i]);
	}
	scanf("%d",&target);
	i = searchInsert(A,n,target);
	printf("%d\n",i);
	return 0;
}

int searchInsert(int A[], int n, int target)
{
	int i = 0;
	for(i = 0;i < n;i++)
	{
		if(A[i] == target)
			return i+1;
		else if(A[i] > target)
			return i;
	}
	if(i == n)
		return n;
}

如果看不懂欢迎留言提问或者留下邮箱!!!o(∩_∩)o

时间: 2024-08-29 17:26:50

第10题:寻找插入位置的相关文章

[经典算法题]寻找数组中第K大的数的方法总结

[经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26 字体:[大 中 小] 打印复制链接我要评论 今天看算法分析是,看到一个这样的问题,就是在一堆数据中查找到第k个大的值. 名称是:设计一组N个数,确定其中第k个最大值,这是一个选择问题,当然,解决这个问题的方法很多,本人在网上搜索了一番,查找到以下的方式,决定很好,推荐给大家. 所谓"第(前)k大数问题"指的是在长度为n(n>=k)的乱序数组中S找出从大到小顺序的第(前)k个数的问题.

Leetcode第1题至第10题 思路分析及C++实现

笔者按照目录刷题,对于每一道题,力争使用效率最高(时间复杂度最低)的算法,并全部通过C++代码实现AC.(文中计算的复杂度都是最坏情况复杂度) 因为考虑到大部分读者已经在Leetcode浏览过题目了,所以每道题都按照 解题思路 -> 实现代码 -> 问题描述 的顺序进行讲解. (笔者目前已刷 40 题,已更新解法 10 题,最近一段时间会频繁更新)可以点击下方链接,直达gitbook: https://codernie.gitbooks.io/leetcode-solutions/conten

第10题:翻转句子中单词的顺序

欢迎转载,转载请务必注明出处:http://blog.csdn.net/alading2009/article/details/44906243 第10题:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理.例如输入"I am a student.",则输出"student. a am I". 此题就是对序列求逆,正如矩阵求逆 (AB)?1=B?1A?1,AB可以是子字符串,再单独对其进行求

HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

Problem DescriptionYJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.One day, he is going to travel from city A to southeastern city B. Let us assume th

16.05.25-16.06.10 题集

继2016.05.24续: codeforces 651B. Beautiful Paintings-简单 http://codeforces.com/problemset/problem/651/B 大意:给出一个序列,求解其任意排列中满足ai?+?1?>?ai 的元素个数最大和. 分析:理想情况下,无重复元素的0从小到大的排列,满足条件的元素个数最多,是n-1. 非理想情况下还有重复元素,只要不断提取出重复的这些元素归到另一集合再这样讨论即可. #include <iostream>

LeetCode第三十四题-寻找数组中对应目标值的首尾索引

Find First and Last Position of Element in Sorted Array 问题简介:给定按升序排序的整数数组,找到给定目标值的起始位置和结束位置. 注: 1.算法的运行时复杂度必须为O(log n) 2.如果在数组中找不到目标,则返回[-1,-1] 举例: 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: [3,4] 2: 输入: nums = [5,7,7,8,8,10], target = 6 输出: [-1,-1

OCP-1Z0-051-题目解析-第10题

10. View the Exhibit and examine the structure of the PROMOTIONS table. Each promotion has a duration of at least seven days . Your manager has asked you to generate a report, which provides the weekly cost for each promotion done to l date. Which qu

海量数据处理10题

1.海量日志数据,提取出某日访问百度次数最多的那个IP. 首先是这一天,并且是访问百度的日志中的IP取出来,逐个写入到一个大文件中.注意到IP是32位的,最多有个2^32个IP.同样可以采用映射的方法,比如模1000,把整个大文件映射为1000个小文件,再找出每个小文中出现频率最大的IP(可以采用hash_map进行频率统计,然后再找出频率最大的几个)及相应的频率.然后再在这1000个最大的IP中,找出那个频率最大的IP,即为所求. 或者如下阐述: 算法思想:分而治之+Hash IP地址最多有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 →