feat: 添加了LeetCode48和LeetCode69的解决方案

This commit is contained in:
whaifree 2024-08-29 01:13:31 +08:00
parent 030780afa8
commit cd7ab8e9ff
2 changed files with 96 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}