请教一个大家今天上午刚面试的一个题目,谢谢!

qq_29448025 2017-07-06 01:09:58
...全文
1255 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
加权不平权 2017-07-08
  • 打赏
  • 举报
回复
   public static void main(String[] args) {
        String input = "\"hello world\"";
        int[] result = new int[128];
        boolean firstTime = true;
        for (char c : input.toCharArray()) {
            result[c]++;
            if (firstTime && result[c] > 1) {
                System.out.printf("first repeat character:%c\n", c);
                firstTime = false;
            }
        }
        for (int i = 0; i < result.length; i++) {
            if (result[i] > 1) {
                System.out.printf("repeat character:%c,tims:%d.\n", (char) i, result[i]);
            }
        }
    }
Alfred8899 2017-07-08
  • 打赏
  • 举报
回复
进来学习学习
ljheee 2017-07-08
  • 打赏
  • 举报
回复
这个 面试题目,之前我也碰到了
_明月 2017-07-07
  • 打赏
  • 举报
回复
请问一下,楼主去面试时,这个楼主是面试的几年工作经验? 3年么?
75闪光雷 2017-07-07
  • 打赏
  • 举报
回复
看到这题我第一个想法是用hashmap
_明月 2017-07-07
  • 打赏
  • 举报
回复
引用 3 楼 soton_dolphin 的回复:

		char[] chars =  "hello world".toCharArray();
		Map<Character, Integer> mappingCount = new HashMap<>();
		for(char c : chars){
			mappingCount.computeIfPresent(Character.valueOf(c),	(k,v) -> v  +1);
			mappingCount.putIfAbsent(Character.valueOf(c), 1);
		}
		System.out.println(mappingCount);
好厉害,写的好简洁。
「已注销」 2017-07-07
  • 打赏
  • 举报
回复
public class Info2Action { public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); int cishu = 0; String heeloWord = "oheelo word"; char[] s = heeloWord.trim().toCharArray(); for (int i = 0; i < s.length; i++) { for (int j = 0; j < s.length; j++) { if (i != j) { if (s[i] == s[j]) { cishu++; map.put(String.valueOf(s[i]), cishu); } } } cishu = 0; } Info2Action.test(map); } public static void test(Map<String, Integer> parameters) { Set set = parameters.entrySet(); Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]); for (int i = 0; i < entries.length; i++) { System.out.println("重复字母:" + entries[i].getKey().toString()); System.out.println("重复:" + entries[i].getValue().toString()+"次"); } System.out.println("第一个重复的是"+entries[0].getKey().toString()); } }
「已注销」 2017-07-07
  • 打赏
  • 举报
回复
上面有一个错了,,现在更正一下,i=j
「已注销」 2017-07-07
  • 打赏
  • 举报
回复
public class Info2Action { public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); int cishu = 0; String heeloWord = "heeelo word"; char[] s = heeloWord.trim().toCharArray(); for (int i = 0; i < s.length; i++) { for (int j = 0; j < s.length; j++) { if (i != j) { if (s[i] == s[j]) { i = j; cishu++; map.put(String.valueOf(s[i]), cishu); } } } cishu = 0; } Info2Action.test(map); } public static void test(Map<String, Integer> parameters) { Set set = parameters.entrySet(); Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]); for (int i = 0; i < entries.length; i++) { System.out.println("重复字母:" + entries[i].getKey().toString()); System.out.println("重复:" + entries[i].getValue().toString()+"次"); } System.out.println("第一个重复的是"+entries[0].getKey().toString()); } }
yannsPeng 2017-07-07
  • 打赏
  • 举报
回复
引用 12 楼 dear_Alice_moon 的回复:
[quote=引用 3 楼 soton_dolphin 的回复:]

		char[] chars =  "hello world".toCharArray();
		Map<Character, Integer> mappingCount = new HashMap<>();
		for(char c : chars){
			mappingCount.computeIfPresent(Character.valueOf(c),	(k,v) -> v  +1);
			mappingCount.putIfAbsent(Character.valueOf(c), 1);
		}
		System.out.println(mappingCount);
好厉害,写的好简洁。 [/quote] java8 666.
  • 打赏
  • 举报
回复
引用 4 楼 soton_dolphin 的回复:

		char[] chars =  "hello world".toCharArray();
		Map<Character, Integer> mappingCount = new HashMap<>();
		for(char c : chars){
			mappingCount.merge(Character.valueOf(c), 1, (oldV, newV) -> oldV + 1);
		}
		System.out.println(mappingCount);
好牛,大神啊
追赶阳光 2017-07-06
  • 打赏
  • 举报
回复
str.replaceAll(" ", ""); 去掉所有空格,包括首尾、中间 。
soton_dolphin 2017-07-06
  • 打赏
  • 举报
回复
没看到还要报告第一个重复的字母

char[] chars =  "hello world".toCharArray();
		Map<Character, Integer> mappingCount = new HashMap<>();
		LinkedList<Character> repeatedChars = new LinkedList<>();
		for(char c : chars){
			mappingCount.computeIfPresent(Character.valueOf(c), (k, v) -> {repeatedChars.offer(c); return v + 1;});
			mappingCount.putIfAbsent(Character.valueOf(c), 1);
		}
		System.out.println(mappingCount);
		System.out.println("first repeated character: " + repeatedChars.poll());
自由自在_Yu 2017-07-06
  • 打赏
  • 举报
回复
去掉空格是str = str.replace(" ",""); trim()只能去掉首尾空格
soton_dolphin 2017-07-06
  • 打赏
  • 举报
回复

		char[] chars =  "hello world".toCharArray();
		Map<Character, Integer> mappingCount = new HashMap<>();
		for(char c : chars){
			mappingCount.merge(Character.valueOf(c), 1, (oldV, newV) -> oldV + 1);
		}
		System.out.println(mappingCount);
soton_dolphin 2017-07-06
  • 打赏
  • 举报
回复

		char[] chars =  "hello world".toCharArray();
		Map<Character, Integer> mappingCount = new HashMap<>();
		for(char c : chars){
			mappingCount.computeIfPresent(Character.valueOf(c),	(k,v) -> v  +1);
			mappingCount.putIfAbsent(Character.valueOf(c), 1);
		}
		System.out.println(mappingCount);
minemine0418 2017-07-06
  • 打赏
  • 举报
回复
希望能帮到你
鱿鱼ing 2017-07-06
  • 打赏
  • 举报
回复
根据自己思路写了一个,仅供参考,欢迎补充。
public static void main(String[] args) {
		Map<Character, Integer> map = new HashMap<>();//创建一个map<字母,出现次数>
		Character first=null;//创建一个char对象代表第一个出现重复的
		String a= "hello world";
		char[] charArray = a.trim().toCharArray();//获取字符串里所有字符
		for (char c : charArray) { //循环字符数组
			Integer num = map.get(c);//从map里获取次数,如为null,带表第一次出现
			if (num==null) {
				num=1;
			}else{
				if (first==null) {//如果不为空且上面声明的first对象为空,则是第一个出现重复的字母,将次数+1
					first=c;
				}
				num++;
			}
			map.put(c, num);
		}
		for (char c : map.keySet()) {
			Integer num = map.get(c);
			if (num>1) {    //次数大于1代表重复,打印出来
				System.out.println("字母"+c+"重复出闲"+map.get(c)+"次!");
			}
		}
		System.out.println("第一个重复的字母是:"+first);
	}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧