提交代码:实现 LeetCode 题目 48 和 33 的解法

This commit is contained in:
whaifree 2024-04-15 13:04:45 +08:00
parent 441579c86c
commit 29eb97e14c
2 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,74 @@
package cn.whaifree.leetCode.Array;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/15 10:32
* @注释
*/
public class LeetCode33 {
@Test
public void test()
{
int[] nums = new int[]{4, 5, 6, 7, 0, 1, 2};
int target = 0;
int i = new Solution().search(nums, target);
System.out.println(i);
}
class Solution {
/**
*
* @param nums 旋转后的值
* @param target
* @return
*/
public int search(int[] nums, int target) {
// 二分查找关键在于如何找到两个递增区间的交结处k
// 判断left和mid的关系
// 如果nums[left] < nums[mid] 即mid在left-k的区间
// if(target<nums[mid] && target>nums[left]) 在left到mid区间
// else mid到right区间
// 如果nums[left] > nums[mid] 即mid在k-right的区间
// if(target>nums[mid]&&target<nums[right]) mid到right之间
// else left到mid之间
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[left] <= nums[mid] ) {
if (target < nums[mid] && target >= nums[left]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
}
//
// class Solution1 {
// public int search(int[] nums, int target) {
//
// }
// }
}

View File

@ -0,0 +1,90 @@
package cn.whaifree.leetCode.Array;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/15 12:00
* @注释
*/
public class LeetCode48 {
@Test
public void test()
{
// [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
int[][] matrix = new int[][]{{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16}};
Solution solution = new Solution();
solution.rotate(matrix);
}
class Solution {
public void rotate(int[][] matrix) {
// 1 1 1 3
// 1 2 2 3
// 1 3 3 3
// 1 1 1 4
// 1 2 2 4
// 1 3 3 4
// 1 4 4 4
// 1 1 1 4
// 1 4 4 4
// 4 4 4 1
// 4 1 1 1
// 1 2 2 1
// 2 1 2 3
// 2 3 3 2
// 3 2 2 1
// 2 1 1 2
int length = matrix.length;
for (int i = 0; i < length / 2; i++) {
for (int j = i; j < length - 1 - i; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[length - j - 1][i];
matrix[length - j - 1][i] = matrix[length - i - 1][length - j - 1];
matrix[length - i - 1][length - j - 1] = matrix[j][length - i - 1];
matrix[j][length - i - 1] = tmp;
}
}
// int i = 0;
// for (int j = i; j < length - 1; j++) {
// int tmp = matrix[i][j];
// matrix[i][j] = matrix[length - j - 1][i];
// matrix[length - j - 1][i] = matrix[length - i - 1][length - j - 1];
// matrix[length - i - 1][length - j - 1] = matrix[j][length - i - 1];
// matrix[j][length - i - 1] = tmp;
// }
//
// i = 1;
// for (int j = i; j < length - 1 - i; j++) {
// int tmp = matrix[i][j];
// matrix[i][j] = matrix[length - j - 1][i];
// matrix[length - j - 1][i] = matrix[length - i - 1][length - j - 1];
// matrix[length - i - 1][length - j - 1] = matrix[j][length - i - 1];
// matrix[j][length - i - 1] = tmp;
// }
for (int k = 0; k < length; k++) {
for (int l = 0; l < length; l++) {
System.out.print(matrix[k][l] + " ");
}
System.out.println();
}
}
}
}