推荐:数据科学课程和书籍清单以及培训讲座


一亩三分地论坛

 找回密码
 获取更多干活,快来注册

一亩三分地官方iOS手机应用下载
查看: 804|回复: 3
收起左侧

Indeed OA 15

[复制链接] |试试Instant~ |关注本帖
helloc93 发表于 2016-10-2 16:17:47 | 显示全部楼层 |阅读模式

2016(7-9月) 码农类 硕士 全职@Indeed - 内推 - 在线笔试 |Otherfresh grad应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干活,快来注册

x
刚做的Indeed OA 15,挂了两个test case 4跟6,估计是挂了。附上写的代码,求大神帮忙debug。hackerrank不能看到test case是什么,真的很坑 085435d3g5fnijc5taicbr.png
[size=100%][size=100%]
[size=100%]1


[size=100%]import java.io.*;
[size=100%][size=100%]
[size=100%]2


[size=100%]import java.util.*;
[size=100%][size=100%]
[size=100%]3


[size=100%]
[size=100%][size=100%]
[size=100%]4

[size=100%][size=100%][size=1.25em]▾

. From 1point 3acres bbs

[size=100%]public class Solution {
[size=100%][size=100%]
[size=100%]5

[size=100%][size=100%][size=1.25em]▾


. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%]    public static void main(String args[] ) throws Exception {. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%][size=100%]
[size=100%]6


[size=100%]        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
[size=100%][size=100%]
[size=100%]7


[size=100%]        Scanner sc = new Scanner(System.in);
[size=100%][size=100%]
[size=100%]8
鏉ユ簮涓浜.涓夊垎鍦拌鍧.

[size=100%]        int Q = sc.nextInt();
[size=100%][size=100%]
[size=100%]9

. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%]        int M = sc.nextInt();
[size=100%][size=100%]
[size=100%]10
鏉ユ簮涓浜.涓夊垎鍦拌鍧.

[size=100%]        Pair[] pairs = new Pair[M];. visit 1point3acres.com for more.
[size=100%][size=100%]
[size=100%]11

[size=100%][size=100%][size=1.25em]▾



[size=100%]        for (int i = 0; i < M; i++) {
[size=100%][size=100%]
[size=100%]12


[size=100%]            pairs = new Pair(sc.nextInt(), sc.nextInt());
[size=100%][size=100%]
[size=100%]13
.1point3acres缃
. 鍥磋鎴戜滑@1point 3 acres
[size=100%]        }
[size=100%][size=100%]
[size=100%]14


[size=100%]        int[] res = calculate(pairs, Q);
[size=100%][size=100%]
[size=100%]15


[size=100%]        for (int n: res) System.out.println(n);. 1point 3acres 璁哄潧
[size=100%][size=100%]
[size=100%]16


[size=100%]    }. 1point3acres.com/bbs
[size=100%][size=100%]
[size=100%]17


[size=100%]   
[size=100%][size=100%]
[size=100%]18

[size=100%][size=100%][size=1.25em]▾



[size=100%]    public static int[] calculate(Pair[] pairs, int Q) {.鏈枃鍘熷垱鑷1point3acres璁哄潧
[size=100%][size=100%]
[size=100%]19


[size=100%]        if (pairs.length == 0 || Q < 2) return new int[0];
[size=100%][size=100%]
[size=100%]20

. From 1point 3acres bbs
[size=100%]        
[size=100%][size=100%]
[size=100%]21

[size=100%][size=100%][size=1.25em]▾
. from: 1point3acres.com/bbs


[size=100%]        Arrays.sort(pairs, new Comparator<Pair>(){
[size=100%][size=100%]
[size=100%]22

[size=100%][size=100%][size=1.25em]▾


. more info on 1point3acres.com
[size=100%]            public int compare(Pair a, Pair b) {
[size=100%][size=100%]
[size=100%]23

. From 1point 3acres bbs
[size=100%]                return a.val-b.val;
[size=100%][size=100%]
[size=100%]24


[size=100%]            }. more info on 1point3acres.com
[size=100%]
[size=100%]
[size=100%]25


[size=100%]        });
[size=100%][size=100%]
[size=100%]26

. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
[size=100%]        
[size=100%][size=100%]
[size=100%]27


[size=100%]        int[] indexs = new int[pairs.length];. From 1point 3acres bbs
[size=100%][size=100%]
[size=100%]28


[size=100%]        int sum = 1;
[size=100%][size=100%]
[size=100%]29

[size=100%][size=100%][size=1.25em]▾
. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
. 鍥磋鎴戜滑@1point 3 acres

[size=100%]        for (int i = 0; i < indexs.length; i++) {
[size=100%][size=100%]
[size=100%]30

.鏈枃鍘熷垱鑷1point3acres璁哄潧
[size=100%]            indexs = sum;
[size=100%][size=100%]
[size=100%]31

. 鍥磋鎴戜滑@1point 3 acres
[size=100%]            sum += pairs.count;
[size=100%][size=100%]
[size=100%]32


[size=100%]        }
[size=100%][size=100%]
[size=100%]33


[size=100%]        
[size=100%][size=100%]
[size=100%]34


[size=100%]        int[] res = new int[Q-1];
[size=100%][size=100%]
[size=100%]35

[size=100%][size=100%][size=1.25em]▾



[size=100%]        for (int k = 1; k < Q; k++) {
[size=100%][size=100%]
[size=100%]36
. 1point 3acres 璁哄潧

[size=100%]            int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
[size=100%][size=100%]
[size=100%]37


[size=100%]            if (index < 0) index = -index-1;
[size=100%][size=100%]
[size=100%]38


[size=100%]            res[k-1] = pairs[index].val;
[size=100%][size=100%]
[size=100%]39


[size=100%]        }
[size=100%][size=100%]
[size=100%]40


[size=100%]        return res;
[size=100%][size=100%]
[size=100%]41


[size=100%]    }
[size=100%][size=100%]
[size=100%]42


[size=100%]   
. 1point 3acres 璁哄潧[size=100%][size=100%]
[size=100%]43

[size=100%][size=100%][size=1.25em]▾



[size=100%]    static class Pair{
[size=100%][size=100%]
[size=100%]44


[size=100%]        public int val = 0;
[size=100%][size=100%]
[size=100%]45


[size=100%]        public int count = 0;
[size=100%][size=100%]
[size=100%]46

[size=100%][size=100%][size=1.25em]▾



[size=100%]        public Pair(int val, int count) {
[size=100%][size=100%]
[size=100%]47


[size=100%]            this.val = val;
[size=100%][size=100%]
[size=100%]48
. 1point 3acres 璁哄潧

[size=100%]            this.count = count;. From 1point 3acres bbs
[size=100%][size=100%]
[size=100%]49


[size=100%]        }. more info on 1point3acres.com
[size=100%][size=100%]
[size=100%]50

. 鍥磋鎴戜滑@1point 3 acres
[size=100%]    }
[size=100%][size=100%]
[size=100%]51


[size=100%]}.鏈枃鍘熷垱鑷1point3acres璁哄潧


 楼主| helloc93 发表于 2016-10-2 16:18:38 | 显示全部楼层
关注一亩三分地公众号:
Warald_一亩三分地
  1. 1

  2. import java.io.*;
    . 鍥磋鎴戜滑@1point 3 acres
  3. 2

  4. import java.util.*;
  5. 3. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴



  6. 4
  7. . Waral 鍗氬鏈夋洿澶氭枃绔,


  8. public class Solution {
  9. 5



  10.     public static void main(String args[] ) throws Exception {
  11. 6. more info on 1point3acres.com

  12.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  13. 7

  14. . From 1point 3acres bbs
  15.         Scanner sc = new Scanner(System.in);. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  16. 8
  17. . more info on 1point3acres.com
  18.         int Q = sc.nextInt();. Waral 鍗氬鏈夋洿澶氭枃绔,
  19. 9

  20.         int M = sc.nextInt();
  21. 10.鐣欏璁哄潧-涓浜-涓夊垎鍦
  22. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  23.         Pair[] pairs = new Pair[M];. more info on 1point3acres.com
  24. 11



  25.         for (int i = 0; i < M; i++) {
  26. 12

  27.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());. more info on 1point3acres.com
  28. 13
    . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  29.         }
  30. 14

  31.         int[] res = calculate(pairs, Q);
  32. 15

  33.         for (int n: res) System.out.println(n);. 1point 3acres 璁哄潧
  34. 16.1point3acres缃

  35.     }. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  36. 17
  37. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  38. 18

  39. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  40. . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  41.     public static int[] calculate(Pair[] pairs, int Q) {
  42. 19
  43. . more info on 1point3acres.com
  44.         if (pairs.length == 0 || Q < 2) return new int[0];
  45. 20


  46. 21


  47. -google 1point3acres
  48.         Arrays.sort(pairs, new Comparator<Pair>(){
  49. 22. more info on 1point3acres.com



  50.             public int compare(Pair a, Pair b) { 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  51. 23

  52.                 return a.val-b.val;
  53. 24
  54. .鏈枃鍘熷垱鑷1point3acres璁哄潧
  55.             }
  56. 25

  57.         });
  58. 26-google 1point3acres


  59. 27

  60.         int[] indexs = new int[pairs.length];
  61. 28

  62.         int sum = 1;
  63. 29

  64. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  65.         for (int i = 0; i < indexs.length; i++) {. From 1point 3acres bbs
  66. 30
  67. . From 1point 3acres bbs
  68.             indexs[i] = sum;. 1point 3acres 璁哄潧
  69. 31

  70.             sum += pairs[i].count;
  71. 32

  72.         }. more info on 1point3acres.com
  73. 33


  74. 34

  75.         int[] res = new int[Q-1];
  76. 35



  77.         for (int k = 1; k < Q; k++) {
  78. 36. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

  79.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
  80. 37

  81.             if (index < 0) index = -index-1;
  82. 38

  83.             res[k-1] = pairs[index].val;
  84. 39

  85.         }
  86. 40

  87.         return res;
  88. 41. more info on 1point3acres.com

  89.     }
  90. 42


  91. 43

  92. .鐣欏璁哄潧-涓浜-涓夊垎鍦

  93.     static class Pair{
  94. 44

  95.         public int val = 0;
  96. 45

  97.         public int count = 0;
  98. 46

  99. . from: 1point3acres.com/bbs

  100.         public Pair(int val, int count) {
  101. 47

  102.             this.val = val;.鏈枃鍘熷垱鑷1point3acres璁哄潧
  103. 48
  104. . visit 1point3acres.com for more.
  105.             this.count = count;
  106. 49

  107.         }
  108. 50

  109.     }. Waral 鍗氬鏈夋洿澶氭枃绔,
  110. 51

  111. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| helloc93 发表于 2016-10-2 16:19:16 | 显示全部楼层
关注一亩三分地微博:
Warald
第一次在帖子了插入代码,不会搞。。
回复 支持 反对

使用道具 举报

 楼主| helloc93 发表于 2016-10-2 16:19:51 | 显示全部楼层
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
  1. 1. 鍥磋鎴戜滑@1point 3 acres

  2. import java.io.*;
  3. 2.1point3acres缃
  4. . Waral 鍗氬鏈夋洿澶氭枃绔,
  5. import java.util.*;
  6. 3
  7. . From 1point 3acres bbs
  8. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  9. 4



  10. public class Solution {
  11. 5


  12. . 鍥磋鎴戜滑@1point 3 acres
  13.     public static void main(String args[] ) throws Exception {. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  14. 6

  15. . Waral 鍗氬鏈夋洿澶氭枃绔,
  16.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  17. 7

  18.         Scanner sc = new Scanner(System.in);
  19. 8

  20.         int Q = sc.nextInt();
  21. 9
  22. . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  23.         int M = sc.nextInt();
  24. 10

  25.         Pair[] pairs = new Pair[M];
  26. 11


  27. . 1point3acres.com/bbs
  28.         for (int i = 0; i < M; i++) {.鏈枃鍘熷垱鑷1point3acres璁哄潧
  29. 12

  30.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  31. 13
  32. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  33.         } 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  34. 14

  35.         int[] res = calculate(pairs, Q);
  36. 15. 1point3acres.com/bbs

  37.         for (int n: res) System.out.println(n);
  38. 16. from: 1point3acres.com/bbs

  39.     }. visit 1point3acres.com for more.
  40. 17


  41. 18



  42.     public static int[] calculate(Pair[] pairs, int Q) {
  43. 19

  44.         if (pairs.length == 0 || Q < 2) return new int[0];
  45. 20


  46. 21

  47. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

  48.         Arrays.sort(pairs, new Comparator<Pair>(){
  49. 22


  50. .鏈枃鍘熷垱鑷1point3acres璁哄潧
  51.             public int compare(Pair a, Pair b) {
  52. 23

  53.                 return a.val-b.val;
  54. 24.鏈枃鍘熷垱鑷1point3acres璁哄潧
  55. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  56.             }. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  57. 25
  58. . 1point3acres.com/bbs
  59.         });.鐣欏璁哄潧-涓浜-涓夊垎鍦
  60. 26-google 1point3acres


  61. 27

  62.         int[] indexs = new int[pairs.length];. more info on 1point3acres.com
  63. 28

  64.         int sum = 1;
  65. 29



  66.         for (int i = 0; i < indexs.length; i++) {. 鍥磋鎴戜滑@1point 3 acres
  67. 30

  68.             indexs[i] = sum;
  69. 31
  70. . 1point 3acres 璁哄潧
  71.             sum += pairs[i].count;
  72. 32

  73.         }
  74. 33


  75. 34

  76.         int[] res = new int[Q-1];
  77. 35
  78. . Waral 鍗氬鏈夋洿澶氭枃绔,


  79.         for (int k = 1; k < Q; k++) {
  80. 36

  81.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
  82. 37. 鍥磋鎴戜滑@1point 3 acres

  83.             if (index < 0) index = -index-1;
  84. 38

  85.             res[k-1] = pairs[index].val;.1point3acres缃
  86. 39

  87.         }
  88. 40

  89.         return res;
  90. 41

  91.     }-google 1point3acres
  92. 42 鏉ユ簮涓浜.涓夊垎鍦拌鍧.


  93. 43. from: 1point3acres.com/bbs



  94.     static class Pair{. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  95. 44.鐣欏璁哄潧-涓浜-涓夊垎鍦

  96.         public int val = 0;
  97. 45

  98.         public int count = 0;
  99. 46



  100.         public Pair(int val, int count) {
  101. 47

  102.             this.val = val;
  103. 48

  104.             this.count = count;
  105. 49

  106.         }
  107. 50
  108. .鐣欏璁哄潧-涓浜-涓夊垎鍦
  109.     }
  110. 51

  111. }
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2017-7-25 07:13

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表