秦皇島建設(shè)網(wǎng)站品牌宣傳方式
1.全排列
給定一個(gè)不含重復(fù)數(shù)字的數(shù)組 nums ,返回其 所有可能的全排列 。你可以 按任意順序 返回答案。
示例 1:
輸入:nums = [1,2,3]
輸出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:
輸入:nums = [0,1]
輸出:[[0,1],[1,0]]
示例 3:
輸入:nums = [1]
輸出:[[1]]
我的解法(回溯)
class Solution {public List<List<Integer>> permute(int[] nums) {Arrays.sort(nums);List<List<Integer>> res = new ArrayList<>();// 記錄已經(jīng)被選過的元素boolean[] used = new boolean[nums.length];backtracing(nums, used, res, new ArrayDeque<Integer>());return res;}public void backtracing(int[] nums, boolean[] used, List<List<Integer>> res, Deque<Integer> path){if(path.size() == nums.length){res.add(new ArrayList<>(path));return;}for(int i = 0; i < nums.length; ++i){if(used[i] == true) continue;used[i] = true;path.add(nums[i]);backtracing(nums, used, res, path);path.removeLast();used[i] = false;}}
}
2.子集
給你一個(gè)整數(shù)數(shù)組 nums ,數(shù)組中的元素 互不相同 。返回該數(shù)組所有可能的子集(冪集)。
解集 不能 包含重復(fù)的子集。你可以按 任意順序 返回解集。
示例 1:
輸入:nums = [1,2,3]
輸出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
示例 2:
輸入:nums = [0]
輸出:[[],[0]]
我的解法(回溯)
class Solution {public List<List<Integer>> subsets(int[] nums) {List<List<Integer>> res = new ArrayList<>();backtracking(nums, 0, res, new ArrayDeque<Integer>());return res;}public void backtracking(int[] nums, int startIndex, List<List<Integer>> res, Deque<Integer> path){res.add(new ArrayList<>(path));for(int i = startIndex; i < nums.length; ++i){path.add(nums[i]);backtracking(nums, i + 1, res, path);path.removeLast();}}
}
3.根據(jù)身高重建隊(duì)列
假設(shè)有打亂順序的一群人站成一個(gè)隊(duì)列,數(shù)組 people 表示隊(duì)列中一些人的屬性(不一定按順序)。每個(gè) people[i] = [hi, ki] 表示第 i 個(gè)人的身高為 hi ,前面 正好 有 ki 個(gè)身高大于或等于 hi 的人。
請(qǐng)你重新構(gòu)造并返回輸入數(shù)組 people 所表示的隊(duì)列。返回的隊(duì)列應(yīng)該格式化為數(shù)組 queue ,其中 queue[j] = [hj, kj] 是隊(duì)列中第 j 個(gè)人的屬性(queue[0] 是排在隊(duì)列前面的人)。
示例 1:
輸入:people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
輸出:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]解釋:
編號(hào)為 0 的人身高為 5 ,沒有身高更高或者相同的人排在他前面。
編號(hào)為 1 的人身高為 7 ,沒有身高更高或者相同的人排在他前面。
編號(hào)為 2 的人身高為 5 ,有 2 個(gè)身高更高或者相同的人排在他前面,即編號(hào)為 0 和 1 的人。
編號(hào)為 3 的人身高為 6 ,有 1 個(gè)身高更高或者相同的人排在他前面,即編號(hào)為 1 的人。
編號(hào)為 4 的人身高為 4 ,有 4 個(gè)身高更高或者相同的人排在他前面,即編號(hào)為 0、1、2、3 的人。
編號(hào)為 5 的人身高為 7 ,有 1 個(gè)身高更高或者相同的人排在他前面,即編號(hào)為 1 的人。
因此 [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] 是重新構(gòu)造后的隊(duì)列。
示例 2:
輸入:people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
輸出:[[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
我的解法(貪心)
class Solution {public int[][] reconstructQueue(int[][] people) {Arrays.sort(people, (a, b) -> {if(a[0] == b[0]) return a[1] - b[1];return b[0] - a[0];});LinkedList<int[]> res = new LinkedList<>();for(int[] p : people){res.add(p[1], p);}return res.toArray(new int[people.length][]);}
}
4.盛最多水的容器
給定一個(gè)長度為 n 的整數(shù)數(shù)組 height 。有 n 條垂線,第 i 條線的兩個(gè)端點(diǎn)是 (i, 0) 和 (i, height[i]) 。
找出其中的兩條線,使得它們與 x 軸共同構(gòu)成的容器可以容納最多的水。
返回容器可以儲(chǔ)存的最大水量。
說明:你不能傾斜容器。
示例 1:
輸入:[1,8,6,2,5,4,8,3,7]
輸出:49
解釋:圖中垂直線代表輸入數(shù)組 [1,8,6,2,5,4,8,3,7]。在此情況下,容器能夠容納水(表示為藍(lán)色部分)的最大值為 49。
示例 2:
輸入:height = [1,1]
輸出:1
我的解法(暴力破解)
class Solution {public int maxArea(int[] height) {int max = 0;for(int i = 0; i < height.length; ++i){for(int j = height.length - 1; j >= 0; --j){max = Math.max(max, (j - i) * Math.min(height[i], height[j]));}}return max;}
}
一做算法題腦子就跟漿糊一樣,兩眼一瞪,啥也不會(huì),暴力破解還超時(shí),沒有天理啊。
官方解法(雙指針)
class Solution {public int maxArea(int[] height) {int res = 0, i = 0, j = height.length - 1;while(i < j){res = height[i] < height[j] ? Math.max(res, (j - i) * height[i++]):Math.max(res, (j - i) * height[j--]);}return res;}
}
沒有思路時(shí),一定要仔細(xì)分析,如果實(shí)在想不出來就動(dòng)筆畫畫圖。