笔试题集
来源:互联网 发布:易语言编程系统全书 编辑:程序博客网 时间:2024/05/18 02:05
最近是校招的高峰期,我呢也参加了很多宣讲会。当然计算机系技术类的应届生,宣讲后做笔试理所当然。现在将遇到过的编程题都记录下来,题目如下:两个字符串如果其中一个交换任意次数,能成为相同的字符串,则将其归为同一类型,现将N个字符串分类(编程实现)。解: 分析:题目意思是两个字符串每个字母出现的次数相同则为一类,那我按正常思路来写就是将每个字符串中每个字母出现次数记录下来,然后做比较。那么用什么存好呢?当然是map嘛,k-v(字母-次数)无重复。
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Scanner;class Base_003 { private static int num ;//需要分类的串数量 private static List<Map<Character, Integer>> list;//容器 存储每个字符串<字母,每个字符串出现次数>的Hashmap public static void main(String[] args){ input();//输入方法 Classification();//分类方法 } public static void input(){ list = new ArrayList<Map<Character,Integer>>(); Scanner in = new Scanner(System.in); System.out.print("请输入要分类的字符串个数:"); num = Integer.valueOf(in.nextLine()); String[] strs = new String[num]; for(int i=1;i<=num;i++){ System.out.print("请输入要分类的第"+i+"个字符串:"); strs[i-1] = in.nextLine(); list.add(saveAsMap(strs[i-1])); } } public static void Classification(){//分类方法 int typeNum = num;//类型数 List<Map<Character, Integer>> relist=new ArrayList<Map<Character,Integer>>(); for(int i=0;i<list.size()-1;i++){ relist.add(list.get(i)); if(list.get(i).equals(list.get(i+1)) ) typeNum--;//每次找到相同类型 总数减一 } list.removeAll(relist);//删除比过的map System.out.println("可以将输入的"+num+"个字符串分为"+typeNum+"类"); } public static Map<Character, Integer> saveAsMap(String str){ Map<Character, Integer> map = new HashMap<Character, Integer>(); int length = str.length(); for(int i=0;i<length;i++){ char c = str.charAt(i); //System.out.println(c+" "+map.get(c)); if(null != map.get(c)) map.put(c, map.get(c)+1); else map.put(c,1); } System.out.println(map.toString()); return map; }}
阅读全文
0 0
- 笔试题集
- C++笔试题集
- 华为笔试题集
- 笔试题集
- 并查集笔试题
- java笔试题集1
- 硬件工程师笔试题集
- Java开发笔试题集
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- git pull 和 git fecth 的区别
- java继承与多态之面向对象
- 人工智能+互联网后,医疗行业竟成最大受益者
- ansible小试
- 云计算
- 笔试题集
- ubuntu常用命令
- springboot FeignClient 请求接收json
- 模拟、SDI、AHD、网络摄像头的视频延时测量
- Linux部署Tomcat 并访问默认项目
- Quartz Spring 报错!自动注解! Couldn't retrieve trigger: ORA-00942: 表或视图不存在
- 04-面向对象(final关键字)1 04-面向对象(final关键字)2 05-面向对象(抽象类-概述).
- Java Web 程序员如何转型大数据
- 宏的使用