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