You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
解题思路:
方法1:
1. 如果不是直接在矩阵内进行操作的话,可以借助一个临时矩阵,将旋转后的结果保存其中,然后再替换掉原来的即可
代码如下:
public class Solution {
public void rotate(int[][] matrix) {
int size = matrix.length;
int[][] temp = new int[size][size];
for(int i = 0;i < size;i++){
for(int j = 0;j < size;j++){
temp[j][size-i-1] = matrix[i][j];
}
}
for(int i = 0;i < size;i++){
for(int j = 0;j < size;j++){
matrix[i][j] = temp[i][j];
}
}
}
}
方法二(网上看到的更符合要求的方法):
1. 观察顺时针旋转九十度的特点,可以发现只要首先将矩阵转置,然后只需要交换一下各列即可
1 2 3
4 5 6
7 8 9
首先转置矩阵 swap(matrix[i][j], matrix[j][i])
1 4 7
2 5 8
3 6 9
然后按照规律交换各列 (swap(matrix[i][j], matrix[i][matrix.length-1-j])
7 4 1
8 5 2
9 6 3
代码如下:
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;
}
}
}
}
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Return:
[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]
Note: All inputs will be in lower-case.
解题思路:
方法一:
1. 由于要找的字符串拥有相同的长度以及字符,所以我首先想到利用异或运算,如果两个字符串组成的字符是一样的,那么把这两个字符串的各个字符依次异或,最后得到的结果一定是0。
2. 但是也有特殊情况,两个字符串组成不一样但是异或起来也是0,所以需要加一些约束,比如用set记录字符,看两个字符串是否由相同的字符组成,以及计算两个字符串各个元素相加起来得到的值看是否相等。
代码如下:
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> result = new ArrayList<>();
for(String str : strs){
boolean handle = false;
for(int i = 0;i < result.size();i++){
List<String> list = result.get(i);
String s = list.get(0);
if(s.length()!= str.length()){
continue;
}
else{
int sum = 0;
for(int j = 0; j < s.length();j++){
sum ^= s.charAt(j) ^ str.charAt(j);
}
if(sum == 0){
Set<Character> set1 = new HashSet<>();
Set<Character> set2 = new HashSet<>();
int sum1 = 0;
int sum2 = 0;
for(int j = 0; j < s.length();j++){
set1.add(s.charAt(j));
set2.add(str.charAt(j));
sum1 += s.charAt(j);
sum2 += str.charAt(j);
}
for(int j = 0; j < s.length();j++){
if(set1.contains(str.charAt(j))){
continue;
}
else{
sum = 1;
break;
}
}
if(set1.size() == set2.size() && sum == 0 && sum1 ==sum2){
list.add(str);
handle = true;
}
}
}
}
if(!handle){
result.add(new ArrayList<String>());
result.get(result.size()-1).add(str);
}
}
return result;
}
}
虽然通过了所有测试用例,但是超时了,在网上看到了一个更快更简单的方法,贴过来给大家分享一下:
解题思路:
1. 将字符串的元素进行排序,组成一个新的字符串key,这样所有组成相同的字符串都有相同的key,用一个HashMap<String,List<String>>来保存结果。
2.对于每个字符串,如果他的key已经存在,则直接把他加进对应的list中,如果不存在,则创建一个新的list,然后put进HashMap中。
代码如下:
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String,List<String>> result = new HashMap<>();
for(String str: strs){
char[] temp = str.toCharArray();
Arrays.sort(temp);
String key = String.valueOf(temp);
if(result.get(key) == null){
result.put(key,new ArrayList());
}
result.get(key).add(str);
}
return new ArrayList<List<String>>(result.values());
}
}