码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Group Shifted Strings

时间:2015-11-29 12:07:06      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public List<List<String>> groupStrings(String[] strings) {
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        for (String str :strings) {
            String tmp = helper(str);
            List<String> list = map.containsKey(tmp) ? map.get(tmp) : new ArrayList<String>();
            list.add(str);
            map.put(tmp, list);
        }
        Iterator<String> it = map.keySet().iterator();
        List<List<String>> result = new ArrayList<List<String>>();
        while (it.hasNext()) {
            List<String> ll = map.get(it.next());
            Collections.sort(ll);
            result.add(ll);
        }
        return result;
    }
    private String helper(String str) {
        char[] chars = str.toCharArray();
        int tmp = (int)chars[0] - (int)‘0‘;
        String result = String.valueOf(0) + ",";
        for (int i = 1; i < chars.length; i++) {
            int num = (int)chars[i] - (int)‘0‘;
            num = num - tmp;
            num += num > 0 ? 0 : 26;
            result = result + String.valueOf(num) + ",";
        }
        return result;
    }
}

 

[LeetCode]Group Shifted Strings

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5004463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!