9.9携程后台笔试AK代码

三个编程题
1. linux命令
2. 一组数划分为k个区间
3. 01字符串 消去1
public class test10 { //第一题     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int n = Integer.parseInt(sc.nextLine());         String[] ops = new String[n];         for(int i=0; i < n; i++){             ops[i] = sc.nextLine();         }         StringBuilder sb = new StringBuilder();         for(int i=0; i < n; i++){             if(ops[i].startsWith("cd")){                 String s = ops[i].substring(3);                 if(s.equals("..")){                     int le = sb.length();                     while(le > 0 && sb.charAt(le-1) != '\\'){                         sb.deleteCharAt(le-1);                         le--;                     }                     if(le >= 1) sb.deleteCharAt(le-1);                 }                 else sb.append('\\').append(s);             }             else{                 if(sb.length() == 0) System.out.println('\\');                 else System.out.println(sb.toString());             }         }     } //***第二题     public static void main2(String[] args) {         Scanner sc = new Scanner(System.in);         int n = sc.nextInt();         int k = sc.nextInt();         int[] nums = new int[n];         for(int i=0; i < n; i++) nums[i] = sc.nextInt();         int max = nums[0], min = nums[0];         for(int num : nums){             max = Math.max(max, num);             min = Math.min(min, num);         }         int l = 0, r = max - min, m = 0;         while(l < r){             m = (l + r) / 2;             if(check(nums, k, m)) r = m;             else l = m + 1;         }         System.out.println(l);     }     private static boolean check(int[] nums, int k, int x){         int max = nums[0], min = nums[0];         for(int i=1; i < nums.length; i++){             max = Math.max(max, nums[i]);             min = Math.min(min, nums[i]);             if(max - min > x){                 k--;                 if(k <= 0) return false;                 max = min = nums[i];             }         }         return k > 0;     } //***第三题     public static void main3(String[] args) {         Scanner sc = new Scanner(System.in);         int n = sc.nextInt();         int m = sc.nextInt();         String str = sc.next();         int[][] score = new int[m][2];         for(int i=0; i < m; i++){             score[i][0] = sc.nextInt();             score[i][1] = sc.nextInt();         }         //这里用Map不行,因为Map无序         Arrays.sort(score, (a, b)->{             return a[0] - b[0];         });         List<Integer> list = new ArrayList<>();         int cnt = 0, max = 0;         for(char ch : str.toCharArray()){             if(ch == '1') cnt++;             else{                 max = Math.max(max, cnt);                 if(cnt != 0) list.add(cnt);                 cnt = 0;             }         }         max = Math.max(max, cnt);         if(cnt != 0) list.add(cnt);         int[] dp = new int[max+1];         for(int i=1; i <= max; i++){             for(int j=0; j < m && score[j][0] <= i; j++){                 int x = score[j][0], y = score[j][1];                 for(int t=1; t * x <= i; t++){                     dp[i] = Math.max(dp[i], dp[i - t * x] + t * y);                 }             }         }         int ans = 0;         for(int num : list){             ans += dp[num];         }         System.out.println(ans);     } }

#笔经#
全部评论
个人注解: /*主函数*/{ // 首先找出整个数组中的两个最值         for(int num : nums){             max = Math.max(max, num);             min = Math.min(min, num);        } // 这里是采用了二分的思路,因为段的不平衡度是非严格递增的(重点),也就是有序的,故可以使用二分法 // left为0,即平衡度的最小值;right为max-min,即段为整个数组时的平衡度,此时拆分后的段的平衡度不可能超过这个值         int l = 0, r = max - min, m = 0;         while(l < r){             m = (l + r) / 2; // 检查平衡度小于等于m的段是否存在,若存在,我们缩小右边界,看看是否存在更小的平衡度             if(check(nums, k, m)) r = m; // 若不存在,说明需要往大于m的方向寻找             else l = m + 1; } // 因为left是从0开始的,而且每次只递增1,那么最后跳出时就是我们所求的结果         System.out.println(l); } // 是否存在满足平衡度<=x的最长的段  boolean check(int[] nums, int k, int x){       int max = nums[0], min = nums[0];       for(int i=1; i < nums.length; i++){           max = Math.max(max, nums[i]);           min = Math.min(min, nums[i]); // 当平衡度大于x时,开启下一个段,看是否存在更小的平衡度。直到达到段数的限制,则返回false           if(max - min > x){               k--;               if(k <= 0) return false;               max = min = nums[i];      }   }         return k > 0; }
5
送花
回复
分享
发布于 2021-09-10 09:34
第二题解法长见识了
点赞
送花
回复
分享
发布于 2021-09-09 21:04
网易互娱
校招火热招聘中
官网直投
int n = Integer.parseInt(sc.nextLine()); 大佬请教一下,这里为什么不能直接使用int n = sc.nextInt()呢?
点赞
送花
回复
分享
发布于 2021-09-09 21:23
tql
点赞
送花
回复
分享
发布于 2021-09-09 21:40
大佬!!!!!!!
点赞
送花
回复
分享
发布于 2021-09-09 23:40
有朋友分享一下笔试题吗
点赞
送花
回复
分享
发布于 2021-09-11 10:18

相关推荐

8 19 评论
分享
牛客网
牛客企业服务