当前位置: 首页 > 面试经验 >

字节9.3前端笔试编程题

优质
小牛编辑
101浏览
2023-09-03

字节9.3前端笔试编程题

  1. 小红拿到长度为n的数组,陡峭值是相邻两数差的绝对值之和,求只修改第i个元素,让f(i)最小的各个陡峭值

     import java.util.Scanner;
    
     public class Main {
         public static void main(String[] args) {
             Scanner in = new Scanner(System.in);
             int n = in.nextInt();
             long[] nums = new long[n];
             for (int i = 0; i < n; i++) {
                 nums[i] = in.nextLong();
             }
             if (n == 1) {
                 System.out.print("0");
                 return;
             }
             long count = 0; // 默认陡峭值
             for (int i = 1; i < n; i++) {
                 count += Math.abs(nums[i] - nums[i - 1]);
             }
             long[] result = new long[n];
             for (int i = 0; i < n; i++) {
                 if (i == 0) {
                     result[i] = count - Math.abs(nums[i + 1] - nums[i]);
                 } else if (i == n - 1) {
                     result[i] = count - Math.abs(nums[i - 1] - nums[i]);
                 } else {
                     long temp = Math.abs(nums[i] - nums[i - 1]) +
                             Math.abs(nums[i] - nums[i + 1]) -
                             Math.abs(nums[i - 1] - nums[i + 1]);
                     result[i] = count - temp;
                 }
             }
    
             for (int i = 0; i < n; i++) {
                 System.out.print(result[i] + " ");
             }
         }
     }
    
  2. 小红的字符串构造,每个小写字母都要出现至少两次,而且相同字母的最小距离刚好是k。

     const fun = function (k) {
         const arr = [
             'a', 'b', 'c', 'd', 'e',
             'f', 'g', 'h', 'i', 'j',
             'k', 'l', 'm', 'n', 'o',
             'p', 'q', 'r', 's', 't',
             'u', 'v', 'w', 'x', 'y',
             'z',
             'a', 'b', 'c', 'd', 'e',
             'f', 'g', 'h', 'i', 'j',
             'k', 'l', 'm', 'n', 'o',
             'p', 'q', 'r', 's', 't',
             'u', 'v', 'w', 'x'
         ]
         let pos = 0
         let str = ''
         while (pos < 26) {
             const temp = arr.slice(pos, pos + k).join("")
             str += temp + temp
             pos += k
         }
         console.log(str)
     }
    
     fun(1)
     fun(2)
     fun(3)
     fun(4)
    
#字节##笔试##前端#
 类似资料: