Solution by Java with ascArray or descArray


  • 0
    S

    public int searchInsert(int[] nums, int target) {

    • if (nums == null || nums.length < 1) {
      return 0;
      }

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

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

    • return nums.length;
      }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.