5.16 学习记录

208 阅读2分钟

项目学习

在打包时,通过-P XXX指令,指定打入jar的配置环境

在Pom中的profiles profile 标签下 如:

<id> dev </id>
<properties></profiles>
<profiles.active>dev<profiles.active>
<activeProfile>dev</activeProfile>
<logLevel>info</logLevel>
<properties>
<activation>
<activeByDefault>
true
</activeByDefault>
</activation>

与配置文件application-dev.yml对应,若在打包时的指令为-P dev 则会将其配置进去

fourinone分布式系统开发

个人任务:分布式调度模块的开发 参考yarn

本周任务:学习yarn和fourinone相关模块代码,确定api接口

每日一题

题目:给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).

思路

首先将原数组排序,成为一个有序数组。

因为是三数之和,所以遍历数组确定第一个数,成为哨兵,用两个指针表示第二个数和第三个数。

两个指针初始分别指向除哨兵外最小的数和最大的数(也就是最左边和最右边),当左指针下标小于右指针时进行循环,计算三数之和,如果三数之和比结果值更接近target,则更新target。若三数之和小于taget,则将左指针向右移;反之则将右指针向左移。当然如果和恰好等于target,直接返回target。

代码

public static int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int result = nums[0] + nums[1] + nums[2];
        for(int i = 0; i< nums.length-2; i++) {
            int l = i+1;
            int r = nums.length - 1 ;
              while( l < r) {
                  int threeNum = nums[i] + nums[l] +nums[r];
                  if(Math.abs(threeNum - target) < Math.abs(result - target)) {
                      result = threeNum;
                  }
                  if(threeNum < target) {
                      l++;
                  } else if(threeNum > target) {
                      r--;
                  } else return target;
              }
        }
        return result;
    }