From 8749e5b188bf5d6b33db85de9af0b4d9cda88099 Mon Sep 17 00:00:00 2001
From: whaifree <49432110+whaibetter@users.noreply.github.com>
Date: Fri, 19 Apr 2024 14:58:31 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AF=B4=E6=98=8E:=20?=
=?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E5=86=97=E4=BD=99=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BA=86=E6=B5=8B=E8=AF=95=E7=94=A8?=
=?UTF-8?q?=E4=BE=8B=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=96=B0?=
=?UTF-8?q?=E7=9A=84=E8=A7=A3=E9=A2=98=E6=96=B9=E6=B3=95=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../leetCode/Dynamic/LeetCode714.java | 69 +++++++++++
.../whaifree/redo/redo_24_4_20/LeetCode5.java | 102 ++++++++++++++++
.../redo/redo_24_4_20/LeetCode647.java | 44 +++++++
.../redo/redo_24_4_20/LeetCode69.java | 69 +++++++++++
.../redo/redo_24_4_20/LeetCode75.java | 109 ++++++++++++++++++
src/test/java/TestInteger.java | 4 +-
6 files changed, 394 insertions(+), 3 deletions(-)
create mode 100644 src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode714.java
create mode 100644 src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode5.java
create mode 100644 src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode647.java
create mode 100644 src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode69.java
create mode 100644 src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode75.java
diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode714.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode714.java
new file mode 100644
index 0000000..0e9c28d
--- /dev/null
+++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode714.java
@@ -0,0 +1,69 @@
+package cn.whaifree.leetCode.Dynamic;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/19 12:27
+ * @注释
+ */
+public class LeetCode714 {
+
+ @Test
+ public void test()
+ {
+ int[] prices = {1, 3, 2, 8, 4, 9};
+ int maxProfit = new Solution1().maxProfit(prices, 2);
+ System.out.println(maxProfit);
+ }
+
+ class Solution {
+ /**
+ * dp[i][0] 表示手里没有股票的最大手头持有
+ * - i-1 就没有 dp[i-1][0]
+ * - i刚刚卖出 dp[i-1][1] + price[i] - fee
+ * dp[i][1] 表示手里有股票的最大手头持有
+ * - i-1 有 dp[i-1][1]
+ * - i刚刚买入 dp[i-1][0] - price[i] - fee
+ * @param prices
+ * @param fee
+ * @return
+ */
+ public int maxProfit(int[] prices, int fee) {
+ int[][] dp = new int[prices.length][2];
+ dp[0][1] = -prices[0] - fee;
+ for (int i = 1; i < prices.length; i++) {
+ dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i] - fee);
+ }
+ return dp[prices.length - 1][0];
+ }
+ }
+
+ class Solution1 {
+ /**
+ * dp[i][0] 表示手里没有股票的最大手头持有
+ * - i-1 就没有 dp[i-1][0]
+ * - i刚刚卖出 dp[i-1][1] + price[i] - fee
+ * dp[i][1] 表示手里有股票的最大手头持有
+ * - i-1 有 dp[i-1][1]
+ * - i刚刚买入 dp[i-1][0] - price[i] - fee
+ * @param prices
+ * @param fee
+ * @return
+ */
+ public int maxProfit(int[] prices, int fee) {
+ int[] dp = new int[2];
+ dp[1] = -prices[0] - fee;
+ for (int i = 1; i < prices.length; i++) {
+ dp[0] = Math.max(dp[0], dp[1] + prices[i]);
+ dp[1] = Math.max(dp[1], dp[0] - prices[i] - fee);
+ }
+ return dp[0];
+ }
+ }
+
+
+
+}
diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode5.java b/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode5.java
new file mode 100644
index 0000000..b865e3e
--- /dev/null
+++ b/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode5.java
@@ -0,0 +1,102 @@
+package cn.whaifree.redo.redo_24_4_20;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/19 13:18
+ * @注释
+ */
+public class LeetCode5 {
+
+ @Test
+ public void test()
+ {
+
+ String s = "bb";
+ String s1 = new Solution1().longestPalindrome(s);
+ System.out.println(s1);
+ }
+
+ class Solution {
+ /**
+ * dp[i][j] 表示i-j是不是回文
+ *
+ * @param s
+ * @return
+ */
+ public String longestPalindrome(String s) {
+ int length = s.length();
+ boolean[][] dp = new boolean[length][length];
+
+ int maxLeft = 0;
+ int maxRight = 0;
+
+ // dp[i][j] 由(i+1)~(j-1)确定,即子串
+ // 则 dp[i][j] 由dp[i+1][j-1]确定
+ for (int i = length; i >= 0; i--) {
+ for (int j = i; j < length; j++) {
+ if (s.charAt(i) == s.charAt(j)
+ && (i == j || i + 1 == j || dp[i + 1][j - 1])
+ // 一个元素是回文 两个元素也是回文
+ ) {
+ if ((maxRight - maxLeft) <= j - i) {
+ maxLeft = i;
+ maxRight = j;
+ }
+ dp[i][j] = true;
+ } else {
+ dp[i][j] = false;
+ }
+ }
+ }
+
+ return s.substring(maxLeft, maxRight + 1);
+ }
+ }
+
+ class Solution1 {
+ /**
+ *
+ * @param s
+ * @return
+ */
+ public String longestPalindrome(String s) {
+ // 从中间向外扩散获得最长的回文串
+ int length = s.length();
+ String maxRes = "";
+ for (int i = 0; i < length; i++) {
+ String s1 = maxLengthHuiWen(s, i, i);
+ String s2 = maxLengthHuiWen(s, i, i + 1);
+ if (maxRes.length() < s1.length()) {
+ maxRes = s1;
+ }
+ if (maxRes.length() < s2.length()){
+ maxRes = s2;
+ }
+ }
+ return maxRes;
+ }
+
+ /**
+ * 向外扩散的两个指针
+ * @param s
+ * @param start
+ * @param end
+ * @return
+ */
+ public String maxLengthHuiWen(String s, int start, int end) {
+ int length = s.length();
+ while (start >= 0 && end < length) {
+ if (s.charAt(start) != s.charAt(end)) {
+ break;
+ }
+ start--;
+ end++;
+ }
+ return s.substring(start + 1, end);
+ }
+
+ }
+}
diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode647.java b/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode647.java
new file mode 100644
index 0000000..aca5b78
--- /dev/null
+++ b/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode647.java
@@ -0,0 +1,44 @@
+package cn.whaifree.redo.redo_24_4_20;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/19 14:37
+ * @注释
+ */
+public class LeetCode647 {
+
+ @Test
+ public void test()
+ {
+ Solution solution = new Solution();
+ int res = solution.countSubstrings("abc");
+ System.out.println(res);
+ }
+
+ class Solution {
+ public int countSubstrings(String s) {
+ int res = 0;
+ for (int i = 0; i < s.length(); i++) {
+ res += count(s, i, i);
+ res += count(s, i, i + 1);
+ }
+ return res;
+ }
+
+ public int count(String s, int start, int end) {
+ int res = 0;
+ while (start >= 0 && end < s.length()) {
+ if (s.charAt(start) != s.charAt(end)) {
+ break;
+ }
+ res++;
+ start--;
+ end++;
+ }
+ return res;
+ }
+ }
+}
diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode69.java b/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode69.java
new file mode 100644
index 0000000..c2e4416
--- /dev/null
+++ b/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode69.java
@@ -0,0 +1,69 @@
+package cn.whaifree.redo.redo_24_4_20;
+
+import org.junit.Test;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/17 11:06
+ * @注释
+ */
+public class LeetCode69 {
+ @Test
+ public void test()
+ {
+
+ int i = new Solution1().mySqrt(10);
+ System.out.println(i);
+ }
+
+
+
+ class Solution {
+ /**
+ *
+ * x^(1/2) = p
+ * 1/2 = log x p = ln x / ln p
+ * ln p = 2 ln x
+ * p = e^(2lnx)
+ * @param x
+ * @return
+ */
+ public int mySqrt(int x) {
+ if (x == 0) {
+ return 0;
+ }
+ // 用1/2 用例为9会输出2,错误
+ int exp = (int) Math.exp( 1d/2 * Math.log(x));
+ if ((long) (exp + 1) * (exp + 1) <= x) { // 8--> exp = 1
+ return exp + 1;
+ }
+ return exp;
+ }
+ }
+
+ class Solution1 {
+
+ /**
+ * 二分查找 12
+ * @param x
+ * @return
+ */
+ public int mySqrt(int x) {
+ int left = 0;
+ int right = x;
+ int ans = 0;
+ while (left <= right) {
+ int mid = left + (right - left) / 2;
+ if ((long) mid * mid > x) {
+ right = mid - 1;
+ } else {
+ ans = mid;
+ left = mid + 1;
+ }
+ }
+ return ans;
+ }
+
+ }
+}
diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode75.java b/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode75.java
new file mode 100644
index 0000000..32629c9
--- /dev/null
+++ b/src/main/java/cn/whaifree/redo/redo_24_4_20/LeetCode75.java
@@ -0,0 +1,109 @@
+package cn.whaifree.redo.redo_24_4_20;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+
+/**
+ * @version 1.0
+ * @Author whai文海
+ * @Date 2024/4/17 11:38
+ * @注释
+ */
+public class LeetCode75 {
+
+ @Test
+ public void test() {
+ int[] nums = {1, 2, 0};
+ new Solution1().sortColors(nums);
+ System.out.println(Arrays.toString(nums));
+ }
+ class Solution {
+ /**
+ * 0 1 2三个元素,第一次遍历全部把0放前面
+ * @param nums
+ */
+ public void sortColors(int[] nums) {
+ int indexZero = 0;
+ for (int i = 0; i < nums.length; i++) {
+ if (nums[i] == 0) {
+ swap(nums, i, indexZero++);
+ }
+ }
+
+ for (int i = indexZero; i < nums.length; i++) {
+ if (nums[i] == 1) {
+ swap(nums, i, indexZero++);
+ }
+ }
+ }
+
+ public void swap(int[] nums, int i, int j) {
+ int temp = nums[i];
+ nums[i] = nums[j];
+ nums[j] = temp;
+ }
+ }
+
+ class Solution1 {
+ /**
+ * 双指针:两个指针指向0和1应该swap的地方
+ * @param nums
+ */
+ public void sortColors(int[] nums) {
+
+ int indexZero = 0;
+ int indexOne = 0;
+
+ for (int i = 0; i < nums.length; i++) {
+ if (nums[i] == 0) {
+ swap(nums, indexZero, i);
+ // 此时有可能index0指向的是1,这样交换会把1交换到后面去
+ // 即交换后的i指向的可能为1
+ // 需要把这个1给他替换到index1对应的位置
+ if (indexZero < indexOne) {
+ swap(nums, i, indexOne);
+ }
+ indexOne++;
+ indexZero++;
+ }else if (nums[i] == 1){
+ swap(nums, indexOne++, i);
+ }
+ }
+ }
+
+ public void swap(int[] nums, int i, int j) {
+ int temp = nums[i];
+ nums[i] = nums[j];
+ nums[j] = temp;
+ }
+ }
+
+ class Solution2 {
+ /**
+ * 双指针:将所有0变到头部,所有2变到尾部
+ * @param nums
+ */
+ public void sortColors(int[] nums) {
+
+ int index0 = 0;
+ int index2 = nums.length - 1;
+ for (int i = 0; i <= index2; i++) {
+ while (index2 >= i && nums[i] == 2) {
+ swap(nums, index2--, i);
+ }
+ if (nums[i] == 0) {
+ swap(nums, index0++, i);
+ }
+ }
+ }
+
+ public void swap(int[] nums, int i, int j) {
+ int temp = nums[i];
+ nums[i] = nums[j];
+ nums[j] = temp;
+ }
+ }
+
+
+}
diff --git a/src/test/java/TestInteger.java b/src/test/java/TestInteger.java
index bbcdd65..1f71ad7 100644
--- a/src/test/java/TestInteger.java
+++ b/src/test/java/TestInteger.java
@@ -7,9 +7,7 @@
public class TestInteger {
public static void main(String[] args) {
- Integer a = new Integer(100);
- Integer b = new Integer(100);
- System.out.println(a == b); //false 两个对象,指向
+
/**
* 0 new #2