11.2---字符串数组排序,删除变位词(CC150)
这道题主义的就是,要利用数组自带的sort函数。
此外,注意,利用hash来判断是否出现了。
public static ArrayList<String> sortStrings(String[] str, int n) {
// write code here
ArrayList<String> res = new ArrayList();
//sort
Arrays.sort(str);
HashSet<String> hash = new HashSet();
for(int i = 0; i <n; i++){
char[] c = str[i].toCharArray();
Arrays.sort(c);
String tmp = new String(c);
if(!hash.contains(tmp)){
hash.add(tmp);
res.add(str[i]);
}
}
return res;
}
11.2---字符串数组排序,删除变位词(CC150)的更多相关文章
- 005推断两个字符串是否是变位词 (keep it up)
写一个函数推断两个字符串是否是变位词. 变位词(anagrams)指的是组成两个单词的字符同样,但位置不同的单词.比方说, abbcd和abcdb就是一对变位词 这也是简单的题. 我们能够排序然后对照 ...
- [Swust 549]--变位词(vector水过)
Time limit(ms): 1000 Memory limit(kb): 65535 Description 输入N和一个要查找的字符串,以下有N个字符串,我们需要找出其中的所有待查找字符串的 ...
- [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序
11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...
- lintcode-158-两个字符串是变位词
158-两个字符串是变位词 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 说明 What is Anagram? Two strings are ...
- LeetCode 438. Find All Anagrams in a String (在字符串中找到所有的变位词)
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- 36.在字符串中删除特定的字符[Delete source from dest]
[题目] 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符.例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”. ...
- Leetcode 242. Valid Anagram(有效的变位词)
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- 变位词(0029)-swustoj
变位词(0029)水题 变位词如果两个单词的组成字母完全相同,只是字母的排列顺序不一样,则它们就是变位词,两个单词相同也被认为是变位词.如tea 与eat , nic 与cin, ddc与dcd, a ...
- 南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和
问题 A: 变位词 时间限制: 2 Sec 内存限制: 10 MB提交: 322 解决: 59提交 状态 算法问答 题目描述 请大家在做oj题之前,仔细阅读关于抄袭的说明http://www.bi ...
随机推荐
- ast模块
有这么一个需求,你想从文件中读取字典,方法有很多,这里用的是ast模块 import ast with open("account","r",encoding= ...
- Game Programming Pattern
http://gameprogrammingpatterns.com/contents.html
- php生成excle
方法一: 新建index.php,代码如下 <?php header("Content-type:application/vnd.ms-excel"); header(&qu ...
- TCP/IP协议详解 卷1—读书笔记(1)
0. 前言 本系列简要记录该书的关键点,用以梳理知识点. 1. 简介 简述链路层下的一些相关协议,如以太网IP数据报,802标准,SLIP,CSLIP,PPP. 链路层主要为上层(IP)和本层(ARP ...
- Mac常用命令
~ 当前所在目录# 超级用户提示符$ 普通用户提示符 Alfred2 //呼出 option + space rm -rf //删除文件夹pwd //打印当前目录 print working dire ...
- CSS3导航效果
来自codepen http://codepen.io/mouradhamoud/pen/RRvVZp <!DOCTYPE html> <html> <head> ...
- sqlserver2008 R2 创建作业(定时任务)
如题: 第一步: 第二步: 第三步: 第四步: 第五步: 第六步: 第七步: 完成!!! 记得把服务打开.设置为自动启动,别重启服务器后没用了.
- step 3 socket
socket 网络通讯三要素 IP地址(主机名) 网络中设备的标示 不易记忆,可以用主机名 本地回环地址:127.0.0.1 主机名:localhost 每台计算机都有一个 127.0.0.1 如果 ...
- [js/jquery]移动端手势拖动,放大,缩小预览图片
摘要 有这样的需求需要在手机端预览图片的时候,实现图片的手势拖动,放大缩小功能.最终通过touch.js这个插件实现了效果. touch.js Touch.js是移动设备上的手势识别与事件库, 由百度 ...
- eclipse将引用了第三方jar包的java项目打成jar包
今天用java开发了一个项目,想要打成jar包,并在linux环境下运行.但是运行时引用的第三方jar包却显示classNotFind错误. 于是查了一下解决办法,在此贴出来,方便以后查阅. 用Ecl ...