前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#版 - Leetcode49 - 字母异位词分组 - 题解

C#版 - Leetcode49 - 字母异位词分组 - 题解

作者头像
Enjoy233
发布2019-03-05 15:30:15
6560
发布2019-03-05 15:30:15
举报

C#版 - Leetcode49 - 字母异位词分组 - 题解

Leetcode49.Group Anagrams


在线提交: https://leetcode.com/problems/group-anagrams/

题目描述

给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

示例:

代码语言:javascript
复制
输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
输出:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

说明:

  • 所有输入均为小写字母。
  • 不考虑答案输出的顺序。
    • Difficulty: Medium
    • Total Accepted: 238 K
    • Total Submissions: 577.1K
    • Contributor: LeetCode

Related Topics: Hash Table String

Similar Questions: Valid Anagram Group Shifted Strings

思路:

方法1 已AC代码:

代码语言:javascript
复制
public class Solution
{
    public IList<IList<string>> GroupAnagrams(string[] strs)
    {
        IList<IList<string>> res = new List<IList<string>>();
        if (strs == null || strs.Length == 0)
            return res;

        Dictionary<string, int> dict = new Dictionary<string,int>();
        foreach (var str in strs)
        {
            char[] ch = str.ToCharArray();
            Array.Sort(ch);
            string s = new string(ch);
            if (dict.ContainsKey(s))
            {
                IList<string> list = res[dict[s]];
                list.Add(str);
            }
            else
            {
                IList<string> list = new List<string>();
                list.Add(str);
                dict.Add(s, res.Count);
                res.Add(list);
            }
        }

        return res;
    }
}

Rank:

You are here! Your runtime beats 30.94% of csharp submissions.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年09月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C#版 - Leetcode49 - 字母异位词分组 - 题解
  • 题目描述
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档