牛客网---2016---华为字符集合
来源:互联网 发布:回归分析 数据标准化 编辑:程序博客网 时间:2024/06/03 16:21
兔子感冒了,难受,想哭。。。
题目:
输入一个字符串,求出该字符串包含的字符集合
输入:
每组数据输入一个字符串,字符串最大长度为100,且只包含字母,不可能为空串,区分大小写。
abcqweracb
输出:
每组数据一行,按字符串原有的字符顺序,输出字符集合,即重复出现并靠后的字母不输出。
abcqwer
解析:
1 . 讲道理,就是按照原来的顺序输出结果,重复的不进行输出,第一个反映就是Hash。http://blog.csdn.net/weixin_39427526/article/details/75498245,这里有整理的Hash。。。
2 . HashSet可以保证不重复,但不能保证顺序.我们可以利用set中的add方法是否成功来判断是否重复。
3 . char转String不能toString(),因为char是基本类型不能用方法 s.charAt(i)+”“
解题所需函数:
1 . StringBuilder的append函数,HashSet的add函数
// HashSet的多种声明方式不同,方法调用也会有所不同Set<String> set = new HashSet<String>();HashSet<Character> set = new HashSet<Character>();
代码:
import java.util.*;public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ // 获取字符串信息 String string1=scan.nextLine(); // 创建结果字符串 StringBuilder string2=new StringBuilder(); // 创建HashSet String Set<String> set = new HashSet<String>(); // 遍历s.length(),查看string1内数据,并添加到string2中 for(int i=0;i<string1.length();i++){ if(set.add(string1.charAt(i)+"")){//如set添加成功说明不重复 string2.append(string1.charAt(i)+""); } } // 最后转化成字符串,显示出来 System.out.println(string2.toString()); } scan.close(); } }
import java.util.*;public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ // 获取字符串信息 String string1=scan.nextLine(); // 转化成字符串数组 char[] charArray = string1.toCharArray(); // 创建结果字符串 StringBuilder string2=new StringBuilder(); // 创建HashSet Character HashSet<Character> set = new HashSet<Character>(); // 遍历s.length(),查看string1内数据,并添加到string2中 for(int i=0;i<charArray.length;i++){ if(set.add(charArray[i])){//如set添加成功说明不重复 string2.append(string1.charAt(i)+""); } } // 最后转化成字符串,显示出来 System.out.println(string2.toString()); } scan.close(); } }
阅读全文
0 0
- 牛客网---2016---华为字符集合
- 华为 字符集合
- 华为机试---字符集合
- 字符集合---- 华为2016研发工程师编程题
- 字符集合-- 华为2016研发工程师编程题
- 华为2016研发工程师编程题之字符集合
- 华为2016研发工程师编程题2字符集合
- 华为笔试——字符集合
- 华为校招(字符集合)python
- 华为机考笔试题-字符集合
- 面试题-华为(16年)-字符集合去重-5
- 牛客网华为在线训练---计算字符个数
- 牛客网华为在线训练---字符个数统计
- 字符集合
- 字符集合
- 字符集合
- 华为面试:字符逆序
- 华为 字符逆序
- lvs群集DR模式搭建
- 在 android 4.4 下预置app
- 数据库ACID特性
- eclipse配置pydev
- 禅城区南庄镇附近(大学生)外出什么地方可以娱乐!
- 牛客网---2016---华为字符集合
- 2017cocoaPods安装 以及遇到的坑
- 在被调用的方法里获取调用方法的方法名
- 数据库索引的深入浅出
- 数据库的基本SQL查询
- Linux下进行Redis安装以及一些redis配置
- Linux&&shell 总结 一
- PL/SQLDeveloper导入导出Oracle数据库方法
- android 6.0 logcat机制(二)logcat从logd中获取log保存到文件中