搜索插入位置

private static int test(int target) {
        int[] nums = new int[]{1, 3, 5, 6};
        int index = 0;
        int length = nums.length;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                index = i;
                break;
            } else {
                if ((target - nums[length - 1] > 0)) {
                    index = length;
                } else {
                    if ((target - nums[i] < 0)) {
                        index = i;
                        break;
                    }
                }

            }
        }
        return index;
    }

你可能感兴趣的:(java,算法,数据结构,c++)