二分,我上来就是用二分做的,没有用遍历查询,因为我知道网易的题目复杂度卡的很严,能用O(longn)做的O(n)绝对过不了。但是第一次提交也没能AC。我就想到了累加的时候可能超过int所能表示的最大值,换成long型就A了。一下是我的代码,写的很挫,见谅! public class FengShou {     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         while(in.hasNext()){             int n = in.nextInt();             long[] arr = new long[n];             long count = 0;             for(int i = 0; i < n; i++){                 count += in.nextInt();                 arr[i] = count;             }             int m = in.nextInt();             long[] queries = new long[m];             int[] res = new int[m];             for(int i = 0; i < m; i++){                 queries[i] = in.nextLong();             }             for(int i = 0; i < m; i++){                 if(queries[i] <= arr[n - 1])                     res[i] = lower_bound(arr, 0, arr.length, queries[i]);             }                          for(int i : res){                 System.out.println(i + 1);             }          }         in.close();     }               public static int lower_bound(long[] arr, int begin, int end, long tag){         while(begin < end){             int mid = (begin + end) / 2;             if(arr[mid] < tag){                 begin = mid + 1;             }else                 end = mid;         }         return begin;     }           /* test case: 5  2 7 3 4 9 3 1 25 11  * */ }
点赞 2

相关推荐

点赞 评论 收藏
分享
07-14 12:29
门头沟学院 Java
后端岗,实习三周感觉有点想跑路了,担心秋招被拉黑,有没有佬是字节HR知道情况的
从零开始的转码生活:你实习三周都想跑路,将来拿到offer真的愿意在这干十几二十年吗
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务