From 29eb97e14cfeffd4e852566acc0d66517a2fa73c Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Mon, 15 Apr 2024 13:04:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20LeetCode=20=E9=A2=98=E7=9B=AE=2048=20?= =?UTF-8?q?=E5=92=8C=2033=20=E7=9A=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/Array/LeetCode33.java | 74 +++++++++++++++ .../whaifree/leetCode/Array/LeetCode48.java | 90 +++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/Array/LeetCode33.java create mode 100644 src/main/java/cn/whaifree/leetCode/Array/LeetCode48.java diff --git a/src/main/java/cn/whaifree/leetCode/Array/LeetCode33.java b/src/main/java/cn/whaifree/leetCode/Array/LeetCode33.java new file mode 100644 index 0000000..502d993 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Array/LeetCode33.java @@ -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(targetnums[left]) 在left到mid区间 + // else mid到right区间 + // 如果nums[left] > nums[mid] 即mid在k-right的区间 + // 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) { +// +// } +// } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Array/LeetCode48.java b/src/main/java/cn/whaifree/leetCode/Array/LeetCode48.java new file mode 100644 index 0000000..b53cb12 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Array/LeetCode48.java @@ -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(); + } + + } + } +}