当前位置: 首页 > news >正文

leetcode热题100道——字母异位词分组

给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

字母异位词 是由重新排列源单词的所有字母得到的一个新单词。

示例 1:

输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]

示例 2:

输入: strs = [""]
输出: [[""]]

示例 3:

输入: strs = ["a"]
输出: [["a"]]

Java解法:暴力统计

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<Character,Integer>[] maps = new Map[strs.length];
        Set<Map<Character,Integer>> set = new HashSet<>();
        for(int i = 0;i < strs.length;i++){
            maps[i] = statistics(strs[i]);
        }
        int index = 0;
        List<List<String>> res = new ArrayList<>();
        while (index < strs.length){
            if(set.contains(maps[index])){
                index++;
                continue;
            }
            List<String> temp = new ArrayList<>();
            set.add(maps[index]);
            temp.add(strs[index]);
            for(int i = index + 1;i < strs.length;i++){
                if(maps[index].equals(maps[i])){
                    temp.add(strs[i]);
                }
            }
            res.add(temp);
        }
        return res;
    }

    public Map<Character,Integer> statistics(String str){
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0;i < str.length();i++){
            if(!map.containsKey(str.charAt(i))){
                map.put(str.charAt(i),1);
            }else {
                int temp = map.get(str.charAt(i));
                map.put(str.charAt(i),temp+1);
            }
        }
        return map;
    }
}

结果:

js解法:

方法一:暴力统计

var groupAnagrams = function(strs) {
    //解题思路:统计法
    //用map对每个字符串进行统计
    let map = new Map()
    for (let i = 0;i < strs.length;i++) {
        let str = strs[i]
        const currMap = new Map()
        for (let j = 0;j < str.length;j++) {
            let char = str[j]
            if (currMap.has(char)) {
                currMap.set(char,currMap.get(char) + 1)
            } else {
                currMap.set(char,1)
            }
        }
        //序列化map
        let key = [...currMap].sort().join('')
        if (map.has(key)) {
            map.get(key).push(str)
        } else {
            map.set(key,[str])
        }
    }
    return [...map.values()]
};

方法二:排序比较法

var groupAnagrams = function(strs) {
    //解题思路:排队比较法
    let map = new Map();
    for (let i = 0;i < strs.length;i++) {
        let str = strs[i];
        let key = str.split('').sort().join('');
        if (map.has(key)) {
            map.get(key).push(str)
        } else {
            map.set(key, [str])
        }
    }
    return [...map.values()];
};

相关文章:

  • lmbench测试方法
  • Java 分布式高并发重试方案及实现
  • Modbus通信协议基础知识总结及应用
  • 网络原理之传输层
  • 【Linux】Reactor模式
  • 01分数规划,二分法,题目练习
  • rust学习笔记18-迭代器
  • 网络安全威胁与防护措施(上)
  • “锈化”Python:用Rust重塑Python生态的六大工具深度解析
  • 床头灯3000词:《傲慢与偏见》(Pride and Prejudice)阅读(英语学习)记录
  • 用爬虫解锁 Shopee 店铺商品数据,开启电商新洞察
  • springboot项目,mapper.xml里面,jdbcType报错 已解决
  • day 5 寄存器 时钟 堆栈
  • 蓝桥杯 之 暴力回溯
  • 分开6年,移居美国的吴秀波和被送进监狱的小三陈昱霖,如今都怎么样了?
  • springboot项目引用外部jar包,linux部署后启动失败,找不到jar包
  • Jvm运行时数据区有哪些
  • SpringCache @Cacheable 在同一个类中调用方法,导致缓存不生效的问题及解决办法
  • 2025年渗透测试面试题总结- PingCAP安全工程师(题目+回答)
  • 蓝桥杯十天冲刺-day1(日期问题)
  • 安徽省委常委、合肥市委书记费高云卸任副省长职务
  • 广东省中医院脾胃病科大科主任张北平病逝,年仅52岁
  • 美凯龙:董事兼总经理车建兴被立案调查并留置
  • 著名连环画家庞邦本逝世
  • “75万买299元路由器”事件进展:重庆市纪委等三部门联合介入调查
  • 乌方:泽连斯基只接受与普京会谈,拒见其他俄代表