1. Permutations

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]

思路;直接使用递归来遍历即可,因为数字是不重复的所以,所以这里的每层递归都从索引0开始遍历整个数组,将不在集合中的数字添加到集合中,到集合元素个数达到要求时返回到上层递归即可。

public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
backtrack(list, new ArrayList<>(), nums);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
} else{
for(int i = 0; i < nums.length; i++){
if(tempList.contains(nums[i])) continue; // element already exists, skip
tempList.add(nums[i]);
backtrack(list, tempList, nums);
tempList.remove(tempList.size() - 1); //注意这里remove方法是按照索引来删的,因为参数是int类型
}
}
}

2. Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations

For example,
[1,1,2] have the following unique permutations:

[
[1,1,2],
[1,2,1],
[2,1,1]
]

思路:和上一题不同在于数组中可能含有重复数字。解法和上题类似,但是递归遍历数组时要考虑到的是因为数组中有重复数字,所以遇到相同数时要做判断,一是如果是用过的数字则不能再用,利用一个布尔数组来记录,还有一个避免对连着的重复数字循环遍历,以 1,1,2和1,1,1来举例,找所有的组合仍然是一层层从索引0到2循环遍历,总共有3层,所以对于 1,1来说第一层遍历到第二个1,第二层遍历到第一个1和第一层遍历到第一个1,第二层遍历到第二个1这两种情况是完全相同的,因此需要避免这种重复的情况,因此对元数组排序一遍后,对连着的重复数字只需要递归遍历一遍即可,如果当前数字和前面数字相同并且前面数字还没用过的情形则直接跳过。

public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, boolean [] used){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
} else{
for(int i = 0; i < nums.length; i++){
      // 当nums[i]用过了或者nums[i]和前一个数字相等且前一个数字没用过的情形则直接跳过
if(used[i] || (i > 0 && nums[i] == nums[i-1] && !used[i - 1])) continue;
used[i] = true;
tempList.add(nums[i]);
backtrack(list, tempList, nums, used);
used[i] = false; //用完后记得置回为false
tempList.remove(tempList.size() - 1);
}
}

3. Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]

Example 2:

Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

思路:本来想根据索引替换规律来解,但是以此遍历整个数组时会出现重复替换的问题。因为顺时针旋转矩阵其实就是将行变成列,再变动列号。所以比较简单的做法是先对矩阵进行转置,然后按列交换矩阵:

The idea was firstly transpose the matrix and then flip it symmetrically. For instance,

1  2  3
4 5 6
7 8 9

after transpose, it will be swap(matrix[i][j], matrix[j][i])

1  4  7
2 5 8
3 6 9

Then flip the matrix horizontally. (swap(matrix[i][j], matrix[i][matrix.length-1-j])

7  4  1
8 5 2
9 6 3

写代码时一个比较麻烦的地方是怎么输入一个矩阵

import java.util.*;

public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
int[][] matrix=new int[n][n];
for(int i=0;i<n;i++){
String str=sc.nextLine();
String[] strs=str.split(" |,");
for(int j=0;j<n;j++){
matrix[i][j]=Integer.parseInt(strs[j]);
}
}
rotate(matrix);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
} static void rotate(int[][] matrix) {
for(int i = 0; i<matrix.length; i++){
for(int j = i; j<matrix[0].length; j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i =0 ; i<matrix.length; i++){
for(int j = 0; j<matrix.length/2; j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix.length-1-j];
matrix[i][matrix.length-1-j] = temp;
}
}
}
}

LeetCode解题报告—— Permutations & Permutations II & Rotate Image的更多相关文章

  1. leetcode 解题报告 Word Ladder II

    题目不多说了.见https://oj.leetcode.com/problems/word-ladder-ii/ 这一题我反复修改了两天半.尝试过各种思路,总是报TLE.终于知道这一题为什么是leet ...

  2. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  3. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  6. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  7. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  8. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  9. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

    1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...

随机推荐

  1. AT1984 Wide Swap

    AT1984 Wide Swap 题意翻译 给出一个元素集合为\(\{1,2,\dots,N\}(1\leq N\leq 500,000)\)的排列\(P\),当有\(i,j(1\leq i<j ...

  2. Semphore信号量的使用

    前言:在多线程环境的同步中,我们为了让每个线程具有同步的作用,经常采用synchronize.reetrantlock等同步手段进行上锁,以便在同一时间只能有一个线程具有访问变量和读写变量的权力.然而 ...

  3. 【题解】Weird journey Codeforces 788B 欧拉路

    传送门:http://codeforces.com/contest/788/problem/B 好题!好题! 首先图不连通的时候肯定答案是0,我们下面讨论图联通的情况 首先考虑,如果我们每条边都经过两 ...

  4. UVA 10214 Trees in a Wood

    https://vjudge.net/problem/UVA-10214 题意:你站在原点,每个坐标位置有一棵高度相同的树,问能看到多少棵树 ans=Σ gcd(x,y)=1 欧拉函数搞搞 #incl ...

  5. java与C#的基础语法区别--持续更新

    1.判断字符串是否相等 java : equals()比较的是对象的内容(区分字母的大小写格式),但是如果使用“==”比较两个对象时,比较的是两个对象的内存地址,所以不相等.即使它们内容相等,但是不同 ...

  6. Windows/Linux javac/java编译运行引入所需的jar包

    > Windows 假设要引用的jar放在D:/test目录下,名字为t1.jar, java源文件放在D:/test/src目录下,名字为t2.java. 编译: javac  -cp  d: ...

  7. bzoj3716/4251 [PA2014]Muzeum

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3716 http://www.lydsy.com/JudgeOnline/problem.ph ...

  8. 牛客网刷题(纯java题型 1~30题)

    牛客网刷题(纯java题型 1~30题) 应该是先extend,然后implement class test extends A implements B { public static void m ...

  9. python初步学习-pycharm使用 (二)

    pycharm调试模式 假设我们的程序在运行过程中命中了一个错误,那我们如何定位错误发生的位置?这就需要进行调试. 在Pycharm中我们可以在其中直接对程序进行调试,唯一需要做的准备工作就是在程序必 ...

  10. base--AuditResult

    //参考base-4.0.2.jar public class AuditResult implements TimeReferable, Serializable //参考api-1.0.0.jar ...