From cd7ab8e9ffa49e380a67c9050315176e9ba073e3 Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Thu, 29 Aug 2024 01:13:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BA=86LeetCode48?= =?UTF-8?q?=E5=92=8CLeetCode69=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9?= =?UTF-8?q?=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/whaifree/redo/redoAll/LeetCode48.java | 50 +++++++++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode69.java | 46 +++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode48.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode69.java diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode48.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode48.java new file mode 100644 index 0000000..5ea6fd8 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode48.java @@ -0,0 +1,50 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/29 0:05 + * @注释 + */ +public class LeetCode48 { + + @Test + public void test() { + int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}}; + new Solution().rotate(matrix); + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[i].length; j++) { + System.out.print(matrix[i][j] + " "); + } + System.out.println(); + } + + } + + class Solution { + public void rotate(int[][] matrix) { + + 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; + } + } + + } + + public void swap(int[][] matrix, int i, int j) { + int tmp = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = tmp; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode69.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode69.java new file mode 100644 index 0000000..b07c02b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode69.java @@ -0,0 +1,46 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/29 0:15 + * @注释 + */ +public class LeetCode69 { + + @Test + public void test() { + System.out.println(new Solution().mySqrt(8)); + } + + class Solution { + /** + * p2=x + * p=x1/2 + * log x p = 1/2 + * lnx / lnp = 1/2 + * lnp = 2 lnx + * p = e(2Lnx) + * p = log p + * + * @param x + * @return + */ + public int mySqrt(int x) { + int left = 0; + int right = x; + int mid = 0; + while (left <= right) { + mid = left + (right - left) / 2; + if ((long) mid * mid <= x) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return right; + } + } +}