zoukankan      html  css  js  c++  java
  • Search Insert Position

    一个简单的问题:

    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)
  • 相关阅读:
    无缝轮播图
    瀑布流之ajax
    进阶版轮播图
    桌面特效
    3D模型文字动画
    Razor 常用方法
    easyui常用
    C#
    Redis设置记录
    日志三剑客ELK
  • 原文地址:https://www.cnblogs.com/iois/p/4075681.html
Copyright © 2011-2022 走看看