215. 數組中的第K個最大元素
45. 跳躍遊戲
46. 全排列
47. 全排列 II
48. 旋轉圖像
--------------------------------分界線-----------------------------------
215. 數組中的第K個最大元素
Difficulty: 中等
在未排序的數組中找到第 k 個最大的元素。請注意,你需要找的是數組排序後的第 k 個最大的元素,而不是第 k 個不同的元素。
示例 1:
輸入: [3,2,1,5,6,4] 和 k = 2
輸出: 5
示例 2:
輸入: [3,2,3,1,2,4,5,5,6] 和 k = 4
輸出: 4
說明:
你可以假設 k 總是有效的,且 1 ≤ k ≤ 數組的長度。
Solution:隨機快速排序
import java.util.Arrays;
import java.util.Random;
class Solution {
public int findKthLargest(int[] nums, int k) {
int res = quickSort(nums, 0, nums.length-1, nums.length-k);
return nums[res];
}
Random random = new Random();
//找到第nums.length - k 大的元素的位置
public int quickSort(int[] nums, int l, int r, int k){
if(l > r) return l;
int flag = random.nextInt(r-l+1)+l; //生成(l-r)之間的隨機數
swap(nums, l, flag);
int i = l, j = r, partition = nums[l];
while(i < j){
while(i<j && nums[j]>=partition){
j--;
}
if(i<j){
swap(nums, i, j);
}
while(i<j && nums[i]<=partition){
i++;
}
if(i<j){
swap(nums, i, j);
}
}
if(i == k) return i;
else return i<k? quickSort(nums, i+1, r, k): quickSort(nums, l, i-1, k);
}
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
45. 跳躍遊戲 II
Difficulty: 困難
給定一個非負整數數組,你最初位於數組的第一個位置。
數組中的每個元素代表你在該位置可以跳躍的最大長度。
你的目標是使用最少的跳躍次數到達數組的最後一個位置。
示例:
輸入: [2,3,1,1,4]
輸出: 2
解釋: 跳到最後一個位置的最小跳躍數是 2。
從下標爲 0 跳到下標爲 1 的位置,跳 1 步,然後跳 3 步到達數組的最後一個位置。
說明:
假設你總是可以到達數組的最後一個位置。
Solution:貪心算法
//貪心算法,一直找最遠可以跳到我這個點的位置
class Solution {
public int jump(int[] nums) {
int len = nums.length;
int index = len-1, res = 0;
while(index > 0){
for(int i=0; i<index; i++){
if(index-i <= nums[i]){
res++;
index = i;
break;
}
}
}
return res;
}
}
46. 全排列
Difficulty: 中等
給定一個 沒有重複 數字的序列,返回其所有可能的全排列。
示例:
輸入: [1,2,3]
輸出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
Solution:回溯算法
import java.util.ArrayList;
import java.util.List;
//回溯
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
int[] used = new int[nums.length];
backtrack(nums, res, temp, used);
return res;
}
public void backtrack(int[] nums, List<List<Integer>> res, List<Integer> temp, int[] used){
if(temp.size() == nums.length){
res.add(new ArrayList<>(temp));
return;
}
for(int i=0; i<nums.length; i++){
if(used[i] == 1) continue;
used[i] = 1;
temp.add(nums[i]);
backtrack(nums, res, temp, used);
used[i] = 0;
temp.remove(temp.size()-1);
}
}
}
47. 全排列 II
Difficulty: 中等
給定一個可包含重複數字的序列,返回所有不重複的全排列。
示例:
輸入: [1,1,2]
輸出:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
Solution:上一題的回溯算法,去重
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//回溯
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
int[] used = new int[nums.length];
backtrack(nums, res, temp, used);
return res;
}
public void backtrack(int[] nums, List<List<Integer>> res, List<Integer> temp, int[] used){
if(temp.size() == nums.length){
res.add(new ArrayList<>(temp));
return;
}
for(int i=0; i<nums.length; i++){
if(i!=0 && nums[i]==nums[i-1] && used[i-1]==1) continue; //去重
if(used[i] == 1) continue;
used[i] = 1;
temp.add(nums[i]);
backtrack(nums, res, temp, used);
used[i] = 0;
temp.remove(temp.size()-1);
}
}
}
48. 旋轉圖像
Difficulty: 中等
給定一個 _n _× n 的二維矩陣表示一個圖像。
將圖像順時針旋轉 90 度。
說明:
你必須在旋轉圖像,這意味着你需要直接修改輸入的二維矩陣。請不要使用另一個矩陣來旋轉圖像。
示例 1:
給定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
原地旋轉輸入矩陣,使其變爲:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
示例 2:
給定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
原地旋轉輸入矩陣,使其變爲:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
Solution:一次交換四個位置
/*
固定四個角
(low, low) (low, high)
(high, low) (high, high)
一圈爲一個循環
*/
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length, count;
int low = 0, high = n-1;
for(int i=0; i<n/2; i++){ //圈數
low = i;
high = n-i-1;
for(int j=0; j<high-low; j++){ //遍歷第一行,每次交換四個元素
int temp = matrix[low][low+j];
matrix[low][low+j] = matrix[high-j][low];
matrix[high-j][low] = matrix[high][high-j];
matrix[high][high-j] = matrix[low+j][high];
matrix[low+j][high] = temp;
}
}
}
}