zoukankan      html  css  js  c++  java
  • 字母异位词分组

    题目:

    题目描述:
    给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
    示例:
    输入:["eat","tea","tan","ate","nat","bat"],
    输出:[
                   ["ate","eat","tea"],
                   ["nat","tan"],
                   ["bat"]
               ]
    说明:
    所有输入均为小写字母。
     不考虑答案输出的顺序。
     
    代码:
    class Solution(object):
    
        def groupAnagrams(self, strs):
    
            """
    
            :type strs: List[str]
    
            :rtype: List[List[str]]
    
            """
    
            #思路:处理过程用map进行保存;key和value值分别对应 字母的从小到大排列值 和 strs中的真实值
    
            map_ = {}
            for i in range(len(strs)):
                tmp = ''.join(sorted(strs[i]))
                if tmp in map_:
                    map_[tmp].append(strs[i])
                else:
                    map_[tmp] = [strs[i]]
            return [v for v in map_.values()]
  • 相关阅读:
    ElasticSearch-03-远行、停止
    ElasticSearch-02-elasticsearch.yaml
    Go-31-杂序
    Go-30-main包
    SpringBoot 初入门
    Spring 事务管理
    JDBC Template
    Spring 基于 AspectJ 的 AOP 开发
    Spring AOP 代理
    Spring 的 AOP 概述和底层实现
  • 原文地址:https://www.cnblogs.com/tsdblogs/p/12348712.html
Copyright © 2011-2022 走看看