feat: 添加了LeetCode48和LeetCode69的解决方案
This commit is contained in:
parent
030780afa8
commit
cd7ab8e9ff
50
src/main/java/cn/whaifree/redo/redoAll/LeetCode48.java
Normal file
50
src/main/java/cn/whaifree/redo/redoAll/LeetCode48.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
46
src/main/java/cn/whaifree/redo/redoAll/LeetCode69.java
Normal file
46
src/main/java/cn/whaifree/redo/redoAll/LeetCode69.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user