【每日算法】哈希表 | day7

文章讲述了两个编程问题的解决方案:一是模拟行走机器人在遇到障碍物时的最大欧式距离计算;二是通过哈希映射对字母异位词进行分组和判断的有效性。在机器人的问题中,使用了哈希集存储障碍物并动态更新坐标。在字母异位词的问题中,关键在于统计每个字符串中字母的出现次数来创建唯一的键。
摘要由CSDN通过智能技术生成

874.模拟行走机器人

代码逻辑:

        使用哈希setp存储障碍物

        变量:结果 --- > 过程中最大的欧式和,进行不断地迭代

                   方向

                   坐标值 x,y  方向值 direction

        指令的遍历:三种情况:

                方向的负数 -1  -2

                        方向值的一个变化

                正数:在某个方向的前进多少位

                        没有遇到障碍物,一直往前走 com 个单位长度

                        没有遇到障碍物:三个条件

                                指令-- 为正数  --- while循环

                                障碍物的key不存在当前值

class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        int result = 0;
        int x = 0, y = 0;
        int direction = 0;
        int[][] direxy = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
        HashSet<String> obstaclesSet = new HashSet<>();

        for (int[] obs : obstacles) {

            // 以坐标的形式存储
            obstaclesSet.add(obs[0] + "," + obs[1]);

        }

        for(int com : commands){
            if(com == -1){
                direction = (direction == 3) ? 0 : direction + 1;
            } else if(com == -2){
                direction = (direction == 0) ? 3 : direction - 1;
            } else {
                while(com-- > 0 && !obstaclesSet.contains((x + direxy[direction][0]) + "," + (y + direxy[direction][1]))){
                        x += direxy[direction][0];
                        y += direxy[direction][1];
                    }

                result = Math.max(result,x * x + y * y);
            }
        }

        return result;
    }
}

49.字母异位词分组

代码逻辑:

        变量:哈希map --- key为字母和其个数,value是他们的同位词的列表  --- 最后 map.values()

                        就可以获取输出

        过程: 遍历所有的字符串数组  ----  将其变为key  

                将其变为key  : 统计字母的个数  使用StringBuffer

                将其存进map

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<>();

        for(String str : strs){
            int[] counts = new int[26];

            for(char c : str.toCharArray()){
                counts[c - 'a']++;
            }
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 26; i++) {
                if(counts[i] != 0){
                    sb.append((char) ('a' + i));
                    sb.append(counts[i]);
                }
            }
            String key = sb.toString();
            List<String> list = map.getOrDefault(key, new ArrayList<String>());

            list.add(str);
            map.put(key,list);

        }
        return new ArrayList<List<String>>(map.values());
    }
}

242.(举一反三)有效的字母异位词

class Solution {

    public boolean isAnagram(String s, String t) {
        if(s.length()  != t.length() ) return false;

        String key1 = getKey(s);
        String key2 = getKey(t);

        if(!key1.equals(key2)) return false;


        return true;
    }

    String getKey(String str){
        int[] counts = new int[26];
        for(char c : str.toCharArray()){
            counts[c - 'a']++;
        }

        StringBuffer sb = new StringBuffer();
        for(int i = 0 ; i < 26 ; i++){
            if (counts[i] != 0){
                sb.append((char) ('a' + i));
                sb.append(counts[i]);
            }
        }

        return sb.toString();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值