159. Find Minimum in Rotate Sorted Array (本质:从一个排过序的数组中查找一个元素)
public class Solution { /** * @param nums: a rotated sorted array * @return: the minimum number in the array */ public int findMin(int[] nums) { // write your code here if(nums == null || nums.length == 0) return -1; int left = 0; int right = nums.length - 1; while(left + 1 < right) { int mid = left + (right - left) / 2; if(nums[mid] < nums[right]) { right = mid; } else { left = mid; } } return Math.min(nums[left], nums[right]); } }
140. Fast Power (本质:将指数二分)
https://www.lintcode.com/problem/fast-power/description?_from=ladder&&fromId=1
使用二分法的原因:降低时间复杂度,思路和 pow(x, n) 很像,将指数 n 二分
错误思路:我最初的思路 先 pow,后 %,return (int)(pow(a, n) % b),但是 long pow(a, n) 也会越界。
解决方案:一边 %, 一边 pow,两者同时进行
方法一:递归 ,为了解决越界,递归时将数据类型转换成 long
public class Solution { /** * @param a: A 32bit integer * @param b: A 32bit integer * @param n: A 32bit integer * @return: An integer */ public int fastPower(int a, int b, int n) { // write your code here if(n == 1) { return a % b; } if(n == 0) { return 1 % b; } long product = fastPower(a, b, n / 2); product = (product * product) % b; if(n % 2 != 0) { product = (product * a) % b; } return (int) product; } }
方法二:非递归版本,思路是转换成二进制
public class Solution { /** * @param a: A 32bit integer * @param b: A 32bit integer * @param n: A 32bit integer * @return: An integer */ public int fastPower(int a, int b, int n) { // write your code here if(n == 0) return 1 % b; if(n == 1) return a % b; long ans = 1, temp = a; while(n != 0) { if(n % 2 != 0) { ans = (ans * temp) % b; } temp = (temp * temp) % b; n /= 2; } return (int)ans; } }
75. Find Peak Element ( 本质:从一个部分有序的数组里查找一个元素 )
https://www.lintcode.com/problem/find-peak-element/description?_from=ladder&&fromId=1
public class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { // write your code here if(A == null || A.length == 0) return 0; int left = 0, right = A.length - 1; while(left + 1 < right) { int mid = left + (right - left) / 2; if(A[mid] > A[mid - 1] && A[mid] > A[mid + 1]) { return mid; } else if(A[mid] > A[mid - 1] && A[mid] < A[mid + 1]) { left = mid; } else { right = mid; } } return A[left] > A[right] ? left : right; } }
74. First Bad Version (本质:从一个有规律的数组中查找一个元素)
https://www.lintcode.com/problem/first-bad-version/description?_from=ladder&&fromId=1
/** * public class SVNRepo { * public static boolean isBadVersion(int k); * } * you can use SVNRepo.isBadVersion(k) to judge whether * the kth code version is bad or not. */ public class Solution { /** * @param n: An integer * @return: An integer which is the first bad version. */ public int findFirstBadVersion(int n) { // write your code here int left = 1, right = n; while(left + 1 < right) { int mid = left + (right - left) / 2; if(SVNRepo.isBadVersion(mid)) { right = mid; } else { left = mid; } } if(SVNRepo.isBadVersion(left) == true) { return left; } return right; } }
62. Search in Rotated Sorted Array https://www.lintcode.com/problem/search-in-rotated-sorted-array/description?_from=ladder&&fromId=1
public class Solution { /** * @param A: an integer rotated sorted array * @param target: an integer to be searched * @return: an integer */ public int search(int[] A, int target) { // write your code here if(A == null || A.length == 0) return -1; int left = 0, right = A.length - 1; while(left + 1 < right) { int mid = left + (right - left) / 2; if(A[mid] > A[right]) { if(A[mid] == target) { return mid; } else if(A[mid] < target && target > A[left] || target < A[mid] && target <= A[right]) { left = mid; } else { right = mid; } } else { if(A[mid] == target) { return mid; } else if(A[mid] < target && A[right] >= target) { left = mid; } else { right = mid; } } } if(A[left] == target) return left; if(A[right] == target) return right; return -1; } }