249. Group Shifted Strings把迁移后相同的字符串集合起来
[抄题]:
Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence:
"abc" -> "bcd" -> ... -> "xyz"
Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.
Example:
Input:["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"],
Output:
[
["abc","bcd","xyz"],
["az","ba"],
["acef"],
["a","z"]
]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
为了保证ba和az分为同一类,加上循环总数26

[思维问题]:
不知道怎么处理相对顺序:就多设置一个变量,来记录总共的相对偏移量就行了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 数据类型:key要求是string,但是计算出来的是数字,所以还是要加“”转一下
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
不知道怎么处理相对顺序:就多设置一个变量,来记录总共的相对偏移量就行了
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<List<String>> groupStrings(String[] strings) {
//initialization: result, map
List<List<String>> result = new ArrayList<List<String>>();
HashMap<String, List<String>> map = new HashMap<>();
//add (key, list) to map
for (String string : strings) {
String key = "";
for (int i = 1; i < string.length(); i++) {
int diff = string.charAt(i) - string.charAt(i - 1);
key += diff > 0 ? diff : 26 + diff;
}
if (!map.containsKey(key)) {
map.put(key, new ArrayList<String>());
}
map.get(key).add(string);
}
//sort and output the results
for (List<String> ss : map.values()) {
Collections.sort(ss);
result.add(ss);
}
//return
return result;
}
/*
az 25 - 0 = 25
ba -1 + 26 = 25
*/
}
249. Group Shifted Strings把迁移后相同的字符串集合起来的更多相关文章
- 249. Group Shifted Strings
题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...
- [LeetCode#249] Group Shifted Strings
Problem: Given a string, we can "shift" each of its letter to its successive letter, for e ...
- [LeetCode] 249. Group Shifted Strings 分组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- LeetCode 249. Group Shifted Strings (群组移位字符串)$
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [Locked] Group Shifted Strings
Group Shifted Strings Given a string, we can "shift" each of its letter to its successive ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [Swift]LeetCode249.群组偏移字符串 $ Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- LeetCode – Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
随机推荐
- Anaconda安装python(idea兼容)
官方网站www.anaconda.com 1 Windos安装 一路下一步,注意添加环境变量 成功过后,测试一下,打开cmd命令行 2 Linux安装 下载完成后上传到linux bash Anaco ...
- 我发起并创立了一个 .Net 平台上的 Web 业务系统 基础库 开源项目 WebEasy
我 强调一点, 程序员 应该对 程序 有 控制感 . 过多的 控制反转 使 程序员 丧失了 对 程序 的 控制感 . 过多的 依赖注入 束缚了 程序员 的 创造力 . 过度复杂的 架构设计 束缚了 程 ...
- OpenLDAP主从
yum -y install compat-openldap必须得安装这个 1:在主上 备份 cp /etc/openldap/slapd.conf /etc/open ...
- confluence6.3.1升级最新版本(6.15.1)
参考自官方文档:https://www.cwiki.us/display/CONFLUENCEWIKI/Upgrading+Confluence 1,confluence6.3.1安装部署 https ...
- Linux之cp、rm、mv
cp.rm.mv 命令功能: 复制文件或目录 命令格式: cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [ ...
- MySQL 批量添加
自己封装的一个批量添加. $data 是一个二维数组.key对应是数据表的字段名: /** * 批量创建 * @param array $data * @return int $res 影响行 * @ ...
- react-native 获取组件的宽度和高度
react-native 获取组件的尺寸有两种方式,第一种方式使用元素自身的onLayout属性去获取,但是这种方式有一个局限性,就是只有在初次渲染的时候才会触发这个函数,而且此种方法获取的是组件相对 ...
- centos7下安全访问远程服务器
1. 添加普通账号 众所周知,linux下的root拥有最高权限,可以执行任何命令.在使用root身份操作时,有时的一个不注意就可能将非常重要的删除(最可怕的是 rm -rf /).而linux不像w ...
- [UE4]Scroll Box带滚动条的容器
一.黑边,当可以往下滚动的时候,下边会出现黑边.当可以往上滚动的时候,上边也会出现黑边. Scroll Box.Style.Style:也可以自定义上下左右黑边的样式: 二.Scroll Box. ...
- REST framwork之解析器
一 我们首先要知道解析器是什么以及他的功能: REST framework 包含许多内置的解析器类,允许接受各种媒体类型(media types)的请求.还支持自定义解析器,这使你可以灵活地设计 AP ...