赞
踩
本人解答:
class Solution { public int[] searchRange(int[] nums, int target) { int j=-1; int m=-1; for(int i=0;i<nums.length;i++){ if(nums[i]==target && j==-1){ j=i; m=i; }else if(nums[i]==target && j!=-1){ m=i; } } int[] l1={j,m}; return l1; } }
官方解答:
class Solution { public int[] searchRange(int[] nums, int target) { int leftIdx = binarySearch(nums, target, true); int rightIdx = binarySearch(nums, target, false) - 1; if (leftIdx <= rightIdx && rightIdx < nums.length && nums[leftIdx] == target && nums[rightIdx] == target) { return new int[]{leftIdx, rightIdx}; } return new int[]{-1, -1}; } public int binarySearch(int[] nums, int target, boolean lower) { int left = 0, right = nums.length - 1, ans = nums.length; while (left <= right) { int mid = (left + right) / 2; if (nums[mid] > target || (lower && nums[mid] >= target)) { right = mid - 1; ans = mid; } else { left = mid + 1; } } return ans; } }
他人优解:
class Solution { //先找>=target的第一个 //再找>target的第一个 //我真是这辈子都不想看见这题 public int[] searchRange(int[] nums, int target) { int l=search(nums,target); int r=search(nums,target+1); if(l==nums.length||nums[l]!=target) return new int[]{-1,-1}; return new int[]{l,r-1}; } //找>=target的第一个 public int search(int[] nums,int target){ int l=0,r=nums.length; while(l<r){ int mid=(r+l)>>1; if(nums[mid]>=target) r=mid; else l=mid+1; } return l; } }
总结:该题未注意需二分查找,本人用暴力解法得解,并未掌握技巧。
2. 搜索旋转排序数组(leecode34题)
题目:整数数组 nums 按升序排列,数组中的值 互不相同 。在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 target ,则返回它的下标,否则返回 -1 。
class Solution { public int search(int[] nums, int target) { int n = nums.length; if (n == 0) { return -1; } if (n == 1) { return nums[0] == target ? 0 : -1; } int l = 0, r = n - 1; while (l <= r) { int mid = (l + r) / 2; if (nums[mid] == target) { return mid; } if (nums[0] <= nums[mid]) { if (nums[0] <= target && target < nums[mid]) { r = mid - 1; } else { l = mid + 1; } } else { if (nums[mid] < target && target <= nums[n - 1]) { l = mid + 1; } else { r = mid - 1; } } } return -1; } }
class Solution { public boolean searchMatrix(int[][] matrix, int target) { int rowIndex = binarySearchFirstColumn(matrix, target); if (rowIndex < 0) { return false; } return binarySearchRow(matrix[rowIndex], target); } public int binarySearchFirstColumn(int[][] matrix, int target) { int low = -1, high = matrix.length - 1; while (low < high) { int mid = (high - low + 1) / 2 + low; if (matrix[mid][0] <= target) { low = mid; } else { high = mid - 1; } } return low; } public boolean binarySearchRow(int[] row, int target) { int low = 0, high = row.length - 1; while (low <= high) { int mid = (high - low) / 2 + low; if (row[mid] == target) { return true; } else if (row[mid] > target) { high = mid - 1; } else { low = mid + 1; } } return false; } }
巧妙解法:
class Solution { public boolean searchMatrix(int[][] matrix, int target) { int rows = matrix.length - 1, columns = 0; while (rows >= 0 && columns < matrix[0].length) { int num = matrix[rows][columns]; if (num == target) { return true; } else if (num > target) { rows--; } else { columns++; } } return false; } }
今日总结:很久不刷题,感觉思路和逻辑都有一点混乱,也不是很清晰,需要不断巩固和持续不断的做题解题来进行提升,明天再见。peace!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。