• 来也科技飞扬季笔试 2023 届秋招专场 java


    第一题(签到题)

    代码:

    1. public int[] twoSum(int[] nums, int target) {
    2. int[] out = new int[ 2 ];
    3. for ( int i = 0; i < nums.length; i ++ ) {
    4. for ( int j = 0; j < nums.length; j ++ ) {
    5. if ( j == i )
    6. continue;
    7. if ( nums[ i ] + nums[ j ] ==target ) {
    8. out[0] = i;
    9. out[1] = j;
    10. return out;
    11. }
    12. }
    13. }
    14. return out;
    15. }

    第二题


    思路:

    总水量 = 所有的水池装的水之和 = 所有水池中的每个 i 位置的装水量相加之和

    所有的水池是由 一跟柱子height[ i ] + 后面的第一根>=height[ i ] 的柱子组成的 

    也就是说,水池的左边边界柱子(也是水池装水的最高高度),必须是后面还有高度不低于它的柱子才可以

     代码:

    1. public int trap( int[] height ) {
    2. if ( height.length == 1 || height.length == 0)
    3. return 0;
    4. int max = 0;
    5. int n = height.length;
    6. int h = height[ 0 ], hIndex = 0;
    7. // 遍历找出第一个樯的高度 != 0 的下标,因为只有在非0的墙后面才可能装水
    8. for( int k = 0; k < n; k ++ ) {
    9. if( height[ k ] != 0 ) {
    10. h = height[ k ];
    11. hIndex = k;
    12. for ( int i = k; i < n; i ++ ) {
    13. System.out.print( i +"》");
    14. boolean f = false;
    15. if ( i == hIndex ) {
    16. f = true;
    17. h = height[ hIndex ];
    18. int j = 0;
    19. // 寻找后面比目前最高的墙还高的墙的下标
    20. while ( h >= 0 ) {
    21. boolean flag = false;
    22. for (j = i + 1; j < n; j++) {
    23. if (height[j] >= h) {
    24. hIndex = j;
    25. System.out.print("[" + hIndex + "]");
    26. flag = true;
    27. break;
    28. }
    29. }
    30. if ( flag )
    31. break;
    32. // 后见没有比现在最高的高度h高的墙,就h--,再继续遍历后面的寻找
    33. if (j == n) {
    34. h --;
    35. continue;
    36. }
    37. }
    38. }
    39. // 加上本i位置所能装的水量(最高的墙 - 本i位置的墙的高度)
    40. if ( !f ) {
    41. max += h - height[i];
    42. System.out.print(h - height[i] + " ");
    43. }
    44. }
    45. break; // 找到非0的墙后,break掉
    46. }
    47. }
    48. System.out.println();
    49. return max;
    50. }

     面试题 17.21. 直方图的水量

    第三题

     思路:

            递归

    代码(成功90%):

    1. static List<List<Integer>> list = new ArrayList<>();
    2. String str = "";
    3. public List<List<Integer>> subsets(int[] nums) {
    4. for ( int j = 0; j < nums.length; j ++ ) {
    5. List<Integer> list1 = new ArrayList<>();
    6. list1.add( nums[ j ] );
    7. getResult( nums, list1, j + 1 );
    8. }
    9. getResult( nums, new ArrayList<>(), 0 );
    10. return list;
    11. }
    12. private void getResult(int[] nums, List<Integer> integerList, int i) {
    13. String s = "[";
    14. for( int j = 0; j < integerList.size(); j ++ ) {
    15. s += integerList.get( j );
    16. }
    17. s += "]";
    18. if( ! str.contains( s ) ) {
    19. str += s + "---";
    20. list.add(new ArrayList<>(integerList));
    21. }
    22. if ( i == nums.length )
    23. return;
    24. for ( int j = i; j < nums.length; j ++ ) {
    25. List<Integer> list2 = new ArrayList<>( integerList );
    26. list2.add( nums[ j ] );
    27. getResult( nums, list2, j + 1 );
    28. }
    29. }

    成功90%,最后一个用例显示解答错误,也不知道什么原因,也不应该是超时,因为我直接测试了最大范围的用例,运行时间27ms而已,哎不知道啥错误原因

  • 相关阅读:
    对比扫描结果 ndiff
    [iOS]-GCD(Grand Central Dispatch)
    PTA--1030 Travel Plan(最短路+记录路径)
    分享计算机msvcp100.dll,丢失或找不到的7个解决方法
    Java 面试题 —— 强类型语言和弱类型语言的区别
    【工具】VS2019编译速度过慢问题的解决
    【LeetCode】1423 可获得的最大点数(中等题)
    如何判断BUG是前端BUG还是后端BUG
    [MySQL]实训七
    QGIS编译(跨平台编译)之五十七:Qt Creator环境下qgis_app库的pro文件
  • 原文地址:https://blog.csdn.net/QRLYLETITBE/article/details/126806422