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. Promise用法总结

    1. Promise的状态   Promise对象有三个状态: 1. 进行中(pending) 2. 成功(resolved) 3. 失败(rejected)   2. 生成一个Promise对象   ...

  2. 两年Java的面试经验

    前言:从过年前就萌生出要跳槽的想法,到过年来公司从3月初提出离职到23号正式离职,上班的时间也出去面试过几家公司,后来总觉的在职找工作总是得请假,便决心离职后找工作.到4月10号找到了一家互联网公司成 ...

  3. MySQL、Oracle、DB2等数据库常规排序、自定义排序和按中文拼音字母排序

    MySQL常规排序.自定义排序和按中文拼音字母排序,在实际的SQL编写时,我们有时候需要对条件集合进行排序. 下面给出3中比较常用的排序方式,mark一下 1.常规排序ASC DESC ASC 正序 ...

  4. ACM2112迪克斯特算法

    HDU Today Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区50 ...

  5. async/await 里的并行和串行

    我们在使用 async/await 语法时,有时会这样用: function getName () { return new Promise((resolve, reject)=>{ setTi ...

  6. HTML或者JSP页面--执行完某事件后刷新页面,重置表单,清空数据

    在提交表单或者执行某个事件之后,如果需要重置表单(即清空表单里的数据) 可以执行下面代码来完成 方式一: self.location.href="userController.do?goAd ...

  7. mysql 多列唯一索引在事务中select for update是不是行锁?

    在表中有这么一索引 UNIQUE KEY `customer_id` (`customer_id`,`item_id`,`ref_id`) 问1. 这种多列唯一索引在事务中select for upd ...

  8. Billboard HDU 2795 (线段树)

    题目链接 Problem Description At the entrance to the university, there is a huge rectangular billboard of ...

  9. POJ 3061 Subsequence ( 尺取法)

    题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...

  10. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...