1、全排列
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
package leetcode;
import java.util.ArrayList;
import java.util.List;
public class fullArray_46 {
public static void main(String[] args) {
fullArray_46 res = new fullArray_46();
int[] nums = { 1, 2, 3 };
res.permute(nums);
}
public List<List<Integer>> permute(int[] nums) {
int len = nums.length ;
List<List<Integer>> result = combination(nums,len);
// System.out.println(result.toString());
return result;
}
List<Integer> list2 = new ArrayList<>();
List<List<Integer>> list1 = new ArrayList<>();
public List<List<Integer>> combination(int[] nums,int len) {
for (int i = 0; i < nums.length; i++) {
list2.add(nums[i]);
int[] temp = new int[nums.length - 1];
int m = 0;
for (int j = 0; j < nums.length; j++) {
if (i != j) {
temp[m] = nums[j];
m++;
}
}
if (list2.size() == len) {
list1.add(new ArrayList<>(list2));
}
combination(temp,len);
list2.remove(list2.size() - 1);
}
return list1;
}
}
2、全排列2
给定一个可包含重复数字的序列,返回所有不重复的全排列。
示例:
输入: [1,1,2]
输出:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class fullArray_46 {
public static void main(String[] args) {
fullArray_46 res = new fullArray_46();
int[] nums = { 1, 1, 2 };
res.permute(nums);
}
public List<List<Integer>> permute(int[] nums) {
Set<List<Integer>> res = combination(nums,nums.length);
for(List<Integer> s: res){
list1.add(s);
}
System.out.println(list1.toString());
return list1;
}
List<List<Integer>> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
Set<List<Integer>> set = new HashSet<>();
public Set<List<Integer>> combination(int[] nums,int len){
for(int i = 0; i < nums.length; i++){
list2.add(nums[i]);
int[] temp = new int[nums.length - 1];
int m = 0;
for (int j = 0; j < nums.length; j++) {
if (i != j) {
temp[m] = nums[j];
m++;
}
}
if(list2.size() == len){
set.add(new ArrayList<>(list2));
}
combination(temp,len);
list2.remove(list2.size() - 1);
}
return set;
}
}
3、旋转图像
给定一个 n × n 的二维矩阵表示一个图像。
将图像顺时针旋转 90 度。
说明:
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
示例 1:
给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
原地旋转输入矩阵,使其变为:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
先每行翻转,再对角线(\)交换
package leetcode;
public class rotate_48 {
public static void main(String[] args) {
rotate_48 res = new rotate_48();
int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
res.rotate(matrix);
}
public int[][] rotate(int[][] matrix) {
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length/2; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix[0].length - 1 - j];
matrix[i][j] = temp;
}
}
for(int i = 0; i < matrix.length-1; i++){
for(int j = 0; j < matrix[0].length-1-i; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[matrix[0].length-1-j][matrix[0].length-1-i];
matrix[matrix[0].length-1-j][matrix[0].length-1-i] = temp;
}
}
return matrix;
}
}
4、字母异位词分组
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
示例:
输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
输出:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
说明:
所有输入均为小写字母。
不考虑答案输出的顺序
维护一个映射 map: {String -> List},其中每个键 K 是一个排序字符串,每个值是初始输入的字符串列表,排序后等于 K。
时间复杂度:O(NKlogK),其中 NNN 是 strs 的长度,而 KKK 是 strs 中字符串的最大长度。当我们遍历每个字符串时,外部循环具有的复杂度为 O(N)。然后,我们在O(KlogK) 的时间内对每个字符串排序。
空间复杂度:O(NK),排序存储在 ans 中的全部信息内容
package leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class groupAnagrams_49 {
public static void main(String[] args) {
String[] stars = {"eat", "tea", "tan", "ate", "nat", "bat"};
groupAnagrams(stars);
}
public static List<List<String>> groupAnagrams(String[] strs) {
Map<String,List<String>> map = new HashMap<>();
for(String s : strs){
char[] ca = s.toCharArray();
Arrays.sort(ca);
String key = String.valueOf(ca);
if(!map.containsKey(key)) {
map.put(key,new ArrayList<>());
}
map.get(key).add(s);
}
return new ArrayList<>(map.values());
}
}