首页 > 其他 > 详细

Search Insert Position

时间:2014-11-05 12:19:53      阅读:231      评论:0      收藏:0      [点我收藏+]

一个简单的问题:

c++

#include<iostream>
using namespace std;

int searchInsert(int a[],int n,int target){
    int i ,count;
    if (target< a[0]){
        count = 0;
        return count;
    }
    if (target>a[n-1]){
        count= n;
        return count;
    }
    for (i=0;i<n-1;++i){
        if (target > a[i] && target < a[i+1]){
            count = i+1;
        }
    }
    for (i=0; i<n ; ++i){
        if (target==a[i]){
            count=i;
        }
    }
    return count;
}

 

还有更简洁的:

python

class Solution:
    # @param A, a list of integers
    # @param target, an integer to be inserted
    # @return integer
    def searchInsert(self, A, target):
        A=A+[target]
        A.sort()
        return A.index(target)

 

Search Insert Position

原文:http://www.cnblogs.com/iois/p/4075681.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!