京东笔试:100%,27%

第一题合唱团身高分组,第二个用例给的很有迷惑性,我看了很久才发现69079936 236011312 77957850 653604087 443890802 277126428 755625552 768751840 993860213 882053548
这个用例,69079936 236011312 77957850排序后是 69079936 77957850  236011312 ,236011312 是9位数。。。。。
然后就可以AC了:
import java.util.*;

/**
 * @author :week
 * @date :Created in 2019-08-24 18:48
 * @description:
 * @modified By:
 * @version: 1.0.0
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] H=new int[N];
        int[] tmp=new int[N];
        for(int i=0;i<N;i++){
            H[i]=sc.nextInt();
            tmp[i]=H[i];
        }
        Arrays.sort(tmp);
        Map<Integer,Integer> map =new HashMap<Integer,Integer>();
        int count=0;
        for(int i=0;i<N;i++){
            int a=H[i];
            int b=tmp[i];
            if(map.containsKey(a)){
                map.put(a,map.get(a)+1);
            }else{
                map.put(a,1);
            }
            if(map.containsKey(b)){
                map.put(b,map.get(b)-1);
            }else{
                map.put(b,-1);
            }
            if(map.containsKey(a)&&map.get(a)==0){
                map.remove(a);
            }
            if(map.containsKey(b)&&map.get(b)==0){
                map.remove(b);
            }
            if(map.size()==0){
                count++;
            }
        }
        System.out.println(count);


    }
}
第二题:不知道字典序怎么贪。。来回倒腾,永远27%。。
import java.util.*;

/**
 * @author :week
 * @date :Created in 2019-08-24 18:49
 * @description:
 * @modified By:
 * @version: 1.0.0
 */
public class Main {
    private static boolean[][] grah;
    private static int[] boycount;
    private static int[] grilcount;
    private static int n;
    private static int m;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        m=sc.nextInt();
        grah=new boolean[n][n];
        boycount =new int[n];
        grilcount=new int[n];
        for(int i=0;i<m;i++){
            int x=sc.nextInt();
            int y=sc.nextInt();
            int min=Math.min(x,y);
            int max=Math.max(x,y);
            x=min-1;
            y=max-n-1;
            boycount[x]++;
            grilcount[y]++;
            grah[x][y]=grah[y][x]=true;
        }
        int[] L=findMax(boycount);
        int[] R=findMax(grilcount);
        int count=0;
        List<Integer> res=new ArrayList<Integer>();
        while(true){
            if(L[0]==-1&&R[0]==-1){
                break;
            }
            count++;
            if(L[1]>=R[1]){
                deleteNode(L[0],true);
                res.add(L[0]+1);
            }else{
                deleteNode(R[0],false);
                res.add(R[0]+1+n);
            }
            L=findMax(boycount);
            R=findMax(grilcount);
        }
        System.out.println(count);
        Collections.sort(res, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                int t1=o1;
                int t2=o2;
                while(t1>10){
                    t1=t1/10;
                }
                while(t2>10){
                    t2=t2/10;
                }
                return t1-t2;
            }
        });
        for(int i=0;i<res.size();i++){
            System.out.println(res.get(i));
        }
    }
    public static void deleteNode(int index,boolean tag){
        if(tag){
            boycount[index]=0;
            for(int i=0;i<n;i++){
                if(grah[index][i]){
                    grah[index][i]=false;
                    grilcount[i]--;
                }
            }
        }else{
            grilcount[index]=0;
            for(int i=0;i<n;i++){
                if(grah[i][index]){
                    grah[i][index]=false;
                    boycount[i]--;
                }
            }
        }
    }
    public static int[] findMax(int[] num){
        int max=0;
        int index=-1;
        for(int i=0;i<num.length;i++){
            if(num[i]>max){
                max=num[i];
                index=i;
            }
        }
        return new int[]{index,max};
    }
}


#京东##笔试题目##Java工程师#
全部评论
点赞 回复 分享
发布于 2019-08-24 21:46
大佬说一下第一题思路吗?
点赞 回复 分享
发布于 2019-08-24 22:01
import java.util.*; public class Main {     public static void main(String[] args) {         Scanner input = new Scanner(System.in);         int len = Integer.valueOf(input.nextLine());         String s = input.nextLine();         boolean isBig = false;         int count = 0;         for (int i=0; i < s.length(); i++ ){             if (isBig) {                 if (Character.isUpperCase(s.charAt(i)))                     continue;                 else {                     count++;                     if (i + 1 < s.length() && Character.isLowerCase(s.charAt(i + 1)))                         isBig = false;                 }             }             else {                 if (Character.isLowerCase(s.charAt(i)))                     continue;                 else {                     count++;                     if (i+1<s.length()&&Character.isUpperCase(s.charAt(i+1)))                         isBig=true;                 }             }         }         count += len;         System.out.println(count);     } } AC
点赞 回复 分享
发布于 2019-08-30 16:34
tql。。。我不配和东哥做兄弟。。。我连题都没读懂。。
点赞 回复 分享
发布于 2019-08-24 21:47
这是什么岗位的题目
点赞 回复 分享
发布于 2019-08-24 22:11
同求第一题思路
点赞 回复 分享
发布于 2019-08-24 22:40
楼主你好,看你第一题代码,代码清晰,思路也很好理解,但是有点不解,你算的H与temp(排过序的)的连续子数组是同一集合的个数,但是这个为什么是最后的答案呢? 比如测试用例H=[2 1 4 5 3 6 8 7],temp=[1 2 3 4 5 6 7 8], 连续子数组[2 1]、[4 5 3]、[6]、[8、7]满足要求,答案是4,最后算的是最多划分的个数,连续子数组是同一集合的个数和这感觉联系不上来呀,比如我可以仅在1和4之间切一刀,我也可以在1和4之间以及3和6之间切,共切两刀,如果这样组合就有不止4中情况,求指点,感谢
点赞 回复 分享
发布于 2019-08-25 10:02
哥们,你收到面试约了,咱们俩编程题A的差不多
点赞 回复 分享
发布于 2019-08-30 21:21

相关推荐

评论
4
36
分享

创作者周榜

更多
牛客网
牛客企业服务