# LeetCode算法代码笔记（46-50）

`给自己的目标：[LeetCode](https://leetcode.com/ "Online Judge Platform") 上每日一题`

## 46. 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]
]
``````

``````public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
for (int i : nums) {
List<List<Integer>> temp = new ArrayList<>(result);
result.clear();
if (temp.size() == 0) {
List<Integer> list = new ArrayList<>();
continue;
}
for (List<Integer> list : temp) {
for (int k = 0; k <= list.size(); k++) {
if (k == list.size()) {
break;
}
List<Integer> ll = new ArrayList<>(list);
}
}
}
return result;
}
}
``````

## 47. Permutations II

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

``````public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) return res;
boolean[] used = new boolean[nums.length];
List<Integer> list = new ArrayList<>();
Arrays.sort(nums);
unique(nums, used, list, res);
return res;
}

public void unique(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> res) {
if (list.size() == nums.length) {
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i]) continue;
if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) continue;
used[i] = true;
unique(nums, used, list, res);
used[i] = false;
list.remove(list.size() - 1);
}
}
}
``````

## 48. Rotate Image

``````旋转后——matrix[i][j]

``````
``````public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
if (n <= 1) return;
int[][] temp = new int[n][n];
for (int i = 0; i < n; i++) {
System.arraycopy(matrix[i], 0, temp[i], 0, n);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = temp[n - 1 - j][i];
}
}
}
}
``````

``````旋转前——matrix[i][j]

``````
``````public class Solution {
public 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;
}
}
}
}
``````

## 49. Group Anagrams

``````For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:

[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
``````

``````public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> ll = new ArrayList<>();
if (strs.length <= 0) return ll;
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
String sstr = sortStr(str);
if (map.containsKey(sstr)) {
} else {
List<String> l = new ArrayList<>();
map.put(sstr, l);
}
}
return new ArrayList<>(map.values());
}

public String sortStr(String str) {
char[] cc = str.toCharArray();
Arrays.sort(cc);
return String.valueOf(cc);
}
}
``````

## 50. Pow(x, n)

``````public class Solution {
public double myPow(double x, int n) {
double temp = x;
if (n == 0) return 1;
temp = myPow(x, n / 2);
if (n % 2 == 0) {
return temp * temp;
} else {
if (n > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
//return Math.pow(x, n);
}
}
``````