经过一楼提示,终于找到问题,是我自己事例验证时,没有发现结果不对,,,做蒙圈了 package com.companyExamination.pingduoduo; import java.util.*; /** * Created by 凌 on 2019/3/10. * 注释:字符去重后,找到首个字母最小值 */ public class Second { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); if (str == null || str.trim().length() == 0){ System.out.println(""); return; } str = str.toLowerCase(); Map<Character,Integer> map = new HashMap<Character,Integer>(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (map.containsKey(ch)){ map.put(ch, (Integer) map.get(ch)+1); }else { map.put(ch, 1); } } char firstMin='z'; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (firstMin > ch){ firstMin =ch; } int value = map.get(ch); if (value == 1){ break; }else{ map.put(ch,--value); } } /*Iterator iterator=map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry entry=(Map.Entry )iterator.next(); char ch = (char)entry.getKey(); if (firstMin > ch){ firstMin =ch; } int value = (int)entry.getValue(); if (value == 1){ break; }else{ map.put(ch,--value); } }*/ System.out.println(firstMin); } }
点赞 2
牛客网
牛客企业服务