zoukankan      html  css  js  c++  java
  • 每日一练leetcode

    搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

    请必须使用时间复杂度为 O(log n) 的算法。

    class Solution {
        public int searchInsert(int[] nums, int target) {
            if(target > Arrays.stream(nums).max().getAsInt()){
                return nums.length;
            }

            for(int i = 0;i < nums.length;i++){
                if(nums[i] >= target){
                    return i;
                }else{
                    continue;
                }
            }
            return -1;

        }
    }
     
    此题要求的算法复杂度意思是只能有一个for循环
    Arrays.stream(nums).max().getAsInt()此函数为求数组变最大值,当然你也可以使用nums[nums.length-1]
     
  • 相关阅读:
    Clam and fish
    费马小定理求逆元模板题
    1
    DP 习题
    106. 从中序与后序遍历序列构造二叉树
    计算几何(判断四边形形状)
    中国剩余定理
    BFS模板
    DFS模板
    线段树
  • 原文地址:https://www.cnblogs.com/nenu/p/15229685.html
Copyright © 2011-2022 走看看