LeetCode 48.Rotate Image & 49.Group Anagrams

Problem 48 Rotate Image

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;
            }
        }
    }
}


Problem 49 Group Anagrams

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());
        
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值