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


一亩三分地论坛

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

一亩三分地官方iOS手机应用下载
查看: 831|回复: 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
.1point3acres缃

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

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

. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
.鏈枃鍘熷垱鑷1point3acres璁哄潧
[size=100%]public class Solution {
[size=100%][size=100%]
[size=100%]5
. more info on 1point3acres.com
[size=100%][size=100%][size=1.25em]▾

鏉ユ簮涓浜.涓夊垎鍦拌鍧.

[size=100%]    public static void main(String args[] ) throws Exception {
[size=100%][size=100%]
[size=100%]6
.鏈枃鍘熷垱鑷1point3acres璁哄潧

[size=100%]        /* Enter your code here. Read input from STDIN. Print output to STDOUT */. 1point3acres.com/bbs
[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


[size=100%]        int M = sc.nextInt();
[size=100%][size=100%]
[size=100%]10
. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

[size=100%]        Pair[] pairs = new Pair[M];
[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
. from: 1point3acres.com/bbs
.鐣欏璁哄潧-涓浜-涓夊垎鍦
[size=100%]            pairs = new Pair(sc.nextInt(), sc.nextInt());. 鍥磋鎴戜滑@1point 3 acres
[size=100%][size=100%]
[size=100%]13
. from: 1point3acres.com/bbs

[size=100%]        }
[size=100%][size=100%]
[size=100%]14
. Waral 鍗氬鏈夋洿澶氭枃绔,

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

. from: 1point3acres.com/bbs
[size=100%]        for (int n: res) System.out.println(n);
[size=100%][size=100%]
[size=100%]16

鏉ユ簮涓浜.涓夊垎鍦拌鍧.
[size=100%]    }
[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) {
[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

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

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



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

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

. 1point 3acres 璁哄潧
. 1point 3acres 璁哄潧
[size=100%]            public int compare(Pair a, Pair b) {
[size=100%][size=100%]
[size=100%]23


[size=100%]                return a.val-b.val;
[size=100%][size=100%]
[size=100%]24

.1point3acres缃
[size=100%]            }
[size=100%]
[size=100%]
[size=100%]25


[size=100%]        });.鐣欏璁哄潧-涓浜-涓夊垎鍦
[size=100%][size=100%]
[size=100%]26
.鏈枃鍘熷垱鑷1point3acres璁哄潧
. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%]        
[size=100%][size=100%]
[size=100%]27


[size=100%]        int[] indexs = new int[pairs.length];
[size=100%][size=100%]
[size=100%]28


[size=100%]        int sum = 1;
[size=100%][size=100%]
[size=100%]29
. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%][size=100%][size=1.25em]▾
.鏈枃鍘熷垱鑷1point3acres璁哄潧


[size=100%]        for (int i = 0; i < indexs.length; i++) {
. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷[size=100%][size=100%]
[size=100%]30
. more info on 1point3acres.com

[size=100%]            indexs = sum;
[size=100%][size=100%]
[size=100%]31


[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


.鏈枃鍘熷垱鑷1point3acres璁哄潧[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


[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


. From 1point 3acres bbs[size=100%]            res[k-1] = pairs[index].val;. Waral 鍗氬鏈夋洿澶氭枃绔,
[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%]    }-google 1point3acres
[size=100%][size=100%]
[size=100%]42


[size=100%]   
[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
. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
. from: 1point3acres.com/bbs
[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

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


[size=100%]        }
[size=100%][size=100%]
[size=100%]50
. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

[size=100%]    }
[size=100%][size=100%]
[size=100%]51


[size=100%]}


 楼主| helloc93 发表于 2016-10-2 16:18:38 | 显示全部楼层
  1. 1-google 1point3acres
  2. . visit 1point3acres.com for more.
  3. import java.io.*;.鏈枃鍘熷垱鑷1point3acres璁哄潧
  4. 2. more info on 1point3acres.com

  5. import java.util.*;. visit 1point3acres.com for more.
  6. 3
  7. . 1point3acres.com/bbs


  8. 4



  9. public class Solution {
  10. 5

  11. . visit 1point3acres.com for more.

  12.     public static void main(String args[] ) throws Exception {
  13. 6

  14. .鐣欏璁哄潧-涓浜-涓夊垎鍦
  15.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  16. 7 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  17.         Scanner sc = new Scanner(System.in);. Waral 鍗氬鏈夋洿澶氭枃绔,
  18. 8
  19. . From 1point 3acres bbs
  20.         int Q = sc.nextInt();
  21. 9

  22.         int M = sc.nextInt();
  23. 10

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

  26. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  27.         for (int i = 0; i < M; i++) {
  28. 12

  29.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  30. 13

  31.         }
  32. 14
  33. . 1point3acres.com/bbs
  34.         int[] res = calculate(pairs, Q);
  35. 15

  36.         for (int n: res) System.out.println(n);
  37. 16

  38.     }
  39. 17


  40. 18.鏈枃鍘熷垱鑷1point3acres璁哄潧

  41. . Waral 鍗氬鏈夋洿澶氭枃绔,
  42. . 鍥磋鎴戜滑@1point 3 acres
  43.     public static int[] calculate(Pair[] pairs, int Q) {
  44. 19

  45.         if (pairs.length == 0 || Q < 2) return new int[0];
  46. 20. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  47. . visit 1point3acres.com for more.
  48. 21


  49. .1point3acres缃
  50.         Arrays.sort(pairs, new Comparator<Pair>(){
  51. 22



  52.             public int compare(Pair a, Pair b) {
  53. 23

  54.                 return a.val-b.val;. from: 1point3acres.com/bbs
  55. 24
  56. .鐣欏璁哄潧-涓浜-涓夊垎鍦
  57.             }
  58. 25

  59.         }); 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  60. 26.1point3acres缃


  61. 27

  62.         int[] indexs = new int[pairs.length];
    . from: 1point3acres.com/bbs
  63. 28

  64.         int sum = 1;
  65. 29



  66.         for (int i = 0; i < indexs.length; i++) {
  67. 30

  68.             indexs[i] = sum;
  69. 31

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

  72.         }
  73. 33


  74. 34

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



  77.         for (int k = 1; k < Q; k++) {. more info on 1point3acres.com
  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;.1point3acres缃
  88. 41
  89. .鏈枃鍘熷垱鑷1point3acres璁哄潧
  90.     }
  91. 42


  92. 43
  93. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴


  94.     static class Pair{
  95. 44. 1point3acres.com/bbs

  96.         public int val = 0;-google 1point3acres
  97. 45
  98. . visit 1point3acres.com for more.
  99.         public int count = 0;
  100. 46. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴



  101.         public Pair(int val, int count) {. 鍥磋鎴戜滑@1point 3 acres
  102. 47-google 1point3acres

  103.             this.val = val;
  104. 48

  105.             this.count = count;
  106. 49

  107.         }
  108. 50

  109.     }
  110. 51. 1point3acres.com/bbs

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

使用道具 举报

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

使用道具 举报

 楼主| helloc93 发表于 2016-10-2 16:19:51 | 显示全部楼层
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!
. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
  1. 1

  2. import java.io.*;
  3. 2

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

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

  7. 4 鏉ユ簮涓浜.涓夊垎鍦拌鍧.



  8. public class Solution {
  9. 5



  10.     public static void main(String args[] ) throws Exception {
  11. 6
  12. . visit 1point3acres.com for more.
  13.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  14. 7
  15. . more info on 1point3acres.com
  16.         Scanner sc = new Scanner(System.in);
  17. 8

  18.         int Q = sc.nextInt();
  19. 9

  20.         int M = sc.nextInt();
  21. 10

  22.         Pair[] pairs = new Pair[M];
    . visit 1point3acres.com for more.
  23. 11



  24.         for (int i = 0; i < M; i++) {. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  25. 12

  26.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  27. 13

  28.         }
  29. 14. 1point3acres.com/bbs

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

  32.         for (int n: res) System.out.println(n);
  33. 16

  34.     }
  35. 17


  36. 18


  37. .鏈枃鍘熷垱鑷1point3acres璁哄潧
  38.     public static int[] calculate(Pair[] pairs, int Q) {
  39. 19. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

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


  42. 21

  43. .鏈枃鍘熷垱鑷1point3acres璁哄潧

  44.         Arrays.sort(pairs, new Comparator<Pair>(){
  45. 22. more info on 1point3acres.com

  46. . 1point3acres.com/bbs

  47.             public int compare(Pair a, Pair b) {
  48. 23

  49.                 return a.val-b.val;
  50. 24
    . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴

  51.             }
  52. 25. Waral 鍗氬鏈夋洿澶氭枃绔,

  53.         });
  54. 26


  55. 27
  56. . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  57.         int[] indexs = new int[pairs.length];
    鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  58. 28

  59.         int sum = 1;
  60. 29. visit 1point3acres.com for more.



  61.         for (int i = 0; i < indexs.length; i++) {
  62. 30

  63.             indexs[i] = sum;
  64. 31

  65.             sum += pairs[i].count;
  66. 32

  67.         }
  68. 33. From 1point 3acres bbs
  69. . 鍥磋鎴戜滑@1point 3 acres

  70. 34. 1point 3acres 璁哄潧
  71. . visit 1point3acres.com for more.
  72.         int[] res = new int[Q-1];.1point3acres缃
  73. 35

  74. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  75.         for (int k = 1; k < Q; k++) {
  76. 36.1point3acres缃

  77.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  78. 37.鐣欏璁哄潧-涓浜-涓夊垎鍦

  79.             if (index < 0) index = -index-1;
  80. 38
  81. . Waral 鍗氬鏈夋洿澶氭枃绔,
  82.             res[k-1] = pairs[index].val;
  83. 39

  84.         }
  85. 40

  86.         return res;
  87. 41

  88.     }
  89. 42


  90. 43
  91. . from: 1point3acres.com/bbs

  92. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  93.     static class Pair{
  94. 44

  95.         public int val = 0;
  96. 45

  97.         public int count = 0;
  98. 46
  99. -google 1point3acres


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

  102.             this.val = val;. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  103. 48
  104. . 1point 3acres 璁哄潧
  105.             this.count = count;
  106. 49.鐣欏璁哄潧-涓浜-涓夊垎鍦

  107.         }.鏈枃鍘熷垱鑷1point3acres璁哄潧
  108. 50

  109.     }
  110. 51

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

使用道具 举报

本版积分规则

关闭

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

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

custom counter

GMT+8, 2017-8-23 20:22

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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