diff --git a/src/main/java/cn/whaifree/leetCode/Hash/LeetCode18.java b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode18.java new file mode 100644 index 0000000..f858b24 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode18.java @@ -0,0 +1,127 @@ +package cn.whaifree.leetCode.Hash; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 18. 四数之和 + + * 给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复): + * 0 <= a, b, c, d < n + * a、b、c 和 d 互不相同 + * nums[a] + nums[b] + nums[c] + nums[d] == target + * 你可以按 任意顺序 返回答案 。 + * + * + * + * 示例 1: + * + * 输入:nums = [1,0,-1,0,-2,2], target = 0 + * 输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] + * 示例 2: + * + * 输入:nums = [2,2,2,2,2], target = 8 + * 输出:[[2,2,2,2]] + * + * + * 提示: + * + * 1 <= nums.length <= 200 + * -109 <= nums[i] <= 109 + * -109 <= target <= 109 + * + // 参考LeetCode15题 + * + */ +public class LeetCode18 { + @Test + public void test() { +// System.out.println(new Solution().fourSum(new int[]{1, 0, -1, 0, -2, 2}, 0)); + System.out.println(1000000000+1000000000+1000000000+1000000000); // -294967296 + int[] nums = {1000000000,1000000000,1000000000,1000000000}; + int target = -294967296; + List> expected = new ArrayList<>(); + expected.add(Arrays.asList(2, 5, 6, 7)); + expected.add(Arrays.asList(3, 4, 6, 7)); + expected.add(Arrays.asList(3, 5, 6, 6)); + System.out.println(new Solution().fourSum(nums, target)); + } + + /** + * TODO 没完全做出来 + * 和LeetCode 15题类似,但需要考虑 2 2 2 2 2 target=8 这样去重的情况 + */ + class Solution { + public List> fourSum(int[] nums, int target) { + + Arrays.sort(nums); + + List> lists = new ArrayList<>(); + + // 第一个数>0,并且>目标值,递增的数组,一定为空 + /** + * int[] nums = {1000000000,1000000000,1000000000,1000000000}; + * int target = -294967296; + * 不加这段代码,这个用例会报错 + * System.out.println(1000000000+1000000000+1000000000+1000000000); // -294967296 + * 因为: -109 <= nums[i] <= 109 + * -109 <= target <= 109 + */ + if (nums[0] > 0 && nums[0] > target) { + return lists; + } + + for (int i = 0; i < nums.length - 3; i++) { + + + + // TODO 保证能够进入一次循环 i>0 + if (i > 0 && nums[i] == nums[i - 1]) { + // 去重 + continue; + } + for (int j = i + 1; j < nums.length - 2; j++) { + + if (j > i + 1 && nums[j] == nums[j - 1]) { + // 这里需要考虑这样一种情况 [-2, -1, 0, 0, 1, 2] 如果用nums[j] == num[j + 1] 判断 -2 0 0 2 这种会被忽略 + // 去重可以考虑向前或者向后去重,注意

去重是要让指针和已经指过的对比

+ + continue; + } + + + int left = j + 1; + int right = nums.length - 1; + + while (left < right) { + int sum = nums[right] + nums[left] + nums[i] + nums[j]; + if (sum > target) { + right--; + } else if (sum < target) { + left++; + } else { + lists.add(Arrays.asList(nums[right], nums[left], nums[i], nums[j])); + while (left != right && nums[left] == nums[left + 1]) { + left++; + } + while (left != right && nums[right] == nums[right - 1]) { + right--; + } + right--; + left++; + } + } + + + } + } + + return lists; + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java new file mode 100644 index 0000000..c666da5 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java @@ -0,0 +1,81 @@ +package cn.whaifree.leetCode.String; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + + +public class LeetCode151 { + + @Test + public void test() { + String s = " the sky is blue"; + + + char[] chars = new Solution1().deleteSpace(s.toCharArray()); + for (char aChar : chars) { + System.out.println("=" + aChar + "="); + } + } + + class Solution { + public String reverseWords(String s) { + List list = new LinkedList<>(); + String[] split = s.split(" "); + for (String s1 : split) { + if (!(s1.equals("") || s1.equals(" "))) { + list.add(s1); + } + } + StringBuilder buffer = new StringBuilder(); + int index = list.size() - 1; + while (index > 0) { + buffer.append(list.get(index--)+" "); + } + buffer.append(list.get(0)); + return buffer.toString(); + } + + } + + // 不要使用辅助空间,空间复杂度要求为O(1) + class Solution1 { + public String reverseWords(String s) { + char[] chars = s.toCharArray(); + // 移除多余空格 + // 反转char + // 反转每个字符 + return null; + } + + public char[] deleteSpace(char[] chars) { + + int superFlowSpace = 0; + + int index = 0; + System.out.println(chars[index] == ' '); + while (chars[index] == ' ') { + superFlowSpace++; + index++; + } + index = 0; + while (index < chars.length - 1 - superFlowSpace) { + chars[index] = chars[index + superFlowSpace]; + if (' ' == chars[index + superFlowSpace] && ' ' == chars[index + superFlowSpace + 1]) { + superFlowSpace++; + } else { + index++; + } + + } + + + return Arrays.copyOfRange(chars, 0, chars.length - superFlowSpace); + } + + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode344.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode344.java new file mode 100644 index 0000000..242423c --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode344.java @@ -0,0 +1,40 @@ +package cn.whaifree.leetCode.String; + +import org.junit.Test; + +public class LeetCode344 { + + @Test + public void test() { + char[] s = "helco1".toCharArray(); + new Solution().reverseString(s); + for (char c : s) { + System.out.println(c); + } + } + + + + class Solution { + + + public void reverseString(char[] s) { +// for (int i = 0; i < s.length / 2; i++) { +// char tmp = s[i]; +// s[i] = s[s.length - i - 1]; +// s[s.length - i - 1] = tmp; +// } + + int left = 0; + int right = s.length - 1; + while (left < right) { + char tmp = s[left]; + s[left] = s[right]; + s[right] = tmp; + left++; + right--; + } + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode541.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode541.java new file mode 100644 index 0000000..e122ac5 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode541.java @@ -0,0 +1,37 @@ +package cn.whaifree.leetCode.String; + +import org.junit.Test; + +public class LeetCode541 { + + @Test + public void test() { + String s = "1234567890"; + int k = 4; + System.out.println(new Solution().reverseStr(s, k)); + } + + class Solution { + public String reverseStr(String s, int k) { + char[] chars = s.toCharArray(); + for (int i = 0; i < chars.length; i = i + k * 2) { + reverse(chars, i, i + k - 1); + } + return new String(chars); + } + + public void reverse(char[] chars, int start, int end) { + // 边界判断, 如果要逆转的区间越界,就把后面这一段都逆转了,取最大值就好了 + if (end > chars.length - 1) { + end = chars.length - 1; + } + while (start < end) { + char tmp = chars[start]; + chars[start] = chars[end]; + chars[end] = tmp; + start++; + end--; + } + } + } +}