# Nicksxs's Blog

What hurts more, the pain of hard work or the pain of regret?

0%

### 题目介绍

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have `n` versions `[1, 2, ..., n]` and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API `bool isBadVersion(version)` which returns whether `version` is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

### 示例

#### Example 1:

Input: n = 5, bad = 4
Output: 4
Explanation:
Then 4 is the first bad version.

#### Example 2:

Input: n = 1, bad = 1
Output: 1

### 代码

``````public int firstBadVersion(int n) {
// 类似于双指针法
int left = 1, right = n, mid;
while (left < right) {
// 取中点
mid = left + (right - left) / 2;
// 如果不是错误版本，就往右找
left = mid + 1;
} else {
// 如果是的话就往左查找
right = mid;
}
}
// 这里考虑交界情况是，在上面循环中如果 left 是好的，right 是坏的，那进入循环的时候 mid == left
// 然后 left = mid + 1 就会等于 right，循环条件就跳出了，此时 left 就是那个起始的错误点了
// 其实这两个是同一个值
return left;
}``````

### 题目介绍

Given an integer array `nums` of length `n` and an integer `target`, find three integers in `nums` such that the sum is closest to `target`.

Return the sum of the three integers.

You may assume that each input would have exactly one solution.

### 示例

#### Example 1:

Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

#### Example 2:

Input: nums = [0,0,0], target = 1
Output: 0

#### Constraints:

• `3 <= nums.length <= 1000`
• `-1000 <= nums[i] <= 1000`
• `-10^4 <= target <= 10^4`

### code

``````public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
// 当前最近的和
int closestSum = nums + nums + nums[nums.length - 1];
for (int i = 0; i < nums.length - 2; i++) {
if (i == 0 || nums[i] != nums[i - 1]) {
// 左指针
int left = i + 1;
// 右指针
int right = nums.length - 1;
// 判断是否遍历完了
while (left < right) {
// 当前的和
int sum = nums[i] + nums[left] + nums[right];
// 小优化，相等就略过了
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
// 这里判断，其实也还是希望趋近目标值
if (sum < target) {
left++;
} else {
right--;
}
// 判断是否需要替换
if (Math.abs(sum - target) < Math.abs(closestSum - target)) {
closestSum = sum;
}
}
}
}
return closestSum;
}``````

### 解决方案

#### 老版本 设置 spring 不带 web 功能

``````SpringApplication app = new SpringApplication(BootStrap.class);
app.setWebEnvironment(false);
app.run(args);``````

#### 新版本

``spring.main.web-application-type=none``

``````SpringApplication app = new SpringApplication(KnowledgePlatformApplication.class);
app.setWebApplicationType(WebApplicationType.NONE);``````

``````public enum WebApplicationType {

/**
* The application should not run as a web application and should not start an
* embedded web server.
*/
NONE,

/**
* The application should run as a servlet-based web application and should start an
* embedded servlet web server.
*/
SERVLET,

/**
* The application should run as a reactive web application and should start an
* embedded reactive web server.
*/
REACTIVE

}``````

### 题目介绍

Given a 2D `grid` of size `m x n` and an integer `k`. You need to shift the `grid k` times.

In one shift operation:

Element at `grid[i][j]` moves to `grid[i][j + 1]`.
Element at `grid[i][n - 1]` moves to `grid[i + 1]`.
Element at `grid[m - 1][n - 1]` moves to `grid`.
Return the 2D grid after applying shift operation `k` times.

### 示例

#### Example 1:

Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]

#### Example 2:

Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]

#### Example 3:

Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]

### 提示

• `m == grid.length`
• `n == grid[i].length`
• `1 <= m <= 50`
• `1 <= n <= 50`
• `-1000 <= grid[i][j] <= 1000`
• `0 <= k <= 100`

### 代码

``````public List<List<Integer>> shiftGrid(int[][] grid, int k) {
// 行数
int m = grid.length;
// 列数
int n = grid.length;
// 偏移值，取下模
k = k % (m * n);
// 反向取下数量，因为我打算直接从头填充新的矩阵
/*
*    比如
*    1 2 3
*    4 5 6
*    7 8 9
*    需要变成
*    9 1 2
*    3 4 5
*    6 7 8
*    就要从 9 开始填充
*/
int reverseK = m * n - k;
List<List<Integer>> matrix = new ArrayList<>();
// 这类就是两层循环
for (int i = 0; i < m; i++) {
List<Integer> line = new ArrayList<>();
for (int j = 0; j < n; j++) {
// 数量会随着循环迭代增长, 确认是第几个
int currentNum = reverseK + i * n +  (j + 1);
// 这里处理下到达矩阵末尾后减掉 m * n
if (currentNum > m * n) {
currentNum -= m * n;
}
// 根据矩阵列数 n 算出在原来矩阵的位置
int last = (currentNum - 1) % n;
int passLine = (currentNum - 1) / n;