拼多多笔试,第二题编程题75%

拼多多笔试,第二题编程题,求问,为什么我的是75%,我能想到的用例都能测试通过啊!!!!

代码实现如下:


import java.util.*;

/**
 * Created by lalala 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();
        LinkedHashMap<Character,Integer> map = new LinkedHashMap<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';

        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);
    }
}

#拼多多#
全部评论
xxa
点赞 回复 分享
发布于 2019-03-10 20:03
经过一楼提示,终于找到问题,是我自己事例验证时,没有发现结果不对,,,做蒙圈了 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); } }
点赞 回复 分享
发布于 2019-03-10 20:21

相关推荐

牛客410815733号:这是什么电影查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务