diff --git a/.gitignore b/.gitignore index 86defc3c..eb72862a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.iml .idea out +target diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..5e02b9f3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + + com.blankkj + leetcode + 0.0.1-SNAPSHOT + pom + + + 1.8 + + + + + + log4j + log4j + 1.2.17 + + + org.slf4j + slf4j-api + 1.7.21 + + + org.slf4j + slf4j-log4j12 + 1.7.21 + + + + + junit + junit + 4.12 + test + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + provided + false + false + true + ./target/dependency + + + + + + + + diff --git a/src/com/blankj/easy/_001/Solution.java b/src/com/blankj/easy/_001/Solution.java index ff0af5f4..72c63825 100644 --- a/src/com/blankj/easy/_001/Solution.java +++ b/src/com/blankj/easy/_001/Solution.java @@ -35,6 +35,31 @@ public int[] twoSum(int[] nums, int target) { return null; } + // Input array is sorted + // https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ + public int[] twoSumOrdered(int[] num, int target) { + int[] indice = new int[2]; + if(num == null || num.length < 2) { + return indice; + } + int left = 0; + int right = num.length - 1; + while(left < right) { + int sum = num[left] + num[right]; + if(sum == target) { + indice[0] = left; + indice[1] = right; + return indice; + } else if(sum < target) { + left++; + } else { + right--; + } + + } + return indice; + } + public static void main(String[] args) { Solution solution = new Solution(); int[] nums = new int[]{2, 7, 11, 15}; diff --git a/src/com/blankj/medium/_003/HashSetSolution.java b/src/com/blankj/medium/_003/HashSetSolution.java new file mode 100644 index 00000000..b9139e56 --- /dev/null +++ b/src/com/blankj/medium/_003/HashSetSolution.java @@ -0,0 +1,39 @@ +package com.blankj.medium._003; + +import java.util.HashSet; +import java.util.Set; + +/** + * Created by dash wang on 2/11/18. + *

+ * HashSet + 双指针 问题 + */ +public class HashSetSolution { + public int lengthOfLongestSubstring(String s) { + int len = s.length(); + Set set = new HashSet<>(); + int max = 0; + int head = 0, tail = 0; + for (head = 0; head < len; head++) { + // 出现重复, 移动尾指针,直到不重复为止 + while (set.contains(s.charAt(head))) { + set.remove(s.charAt(tail)); + tail++; + } + set.add(s.charAt(head)); + int tempLen = head - tail + 1; + if (tempLen > max) { + max = tempLen; + } + } + return max; + } + + public static void main(String[] args) { + HashSetSolution solution = new HashSetSolution(); + System.out.println(solution.lengthOfLongestSubstring("abcabcbb")); + System.out.println(solution.lengthOfLongestSubstring("bbbbb")); + System.out.println(solution.lengthOfLongestSubstring("pwwkew")); + System.out.println(solution.lengthOfLongestSubstring("Abcabcbb")); + } +} diff --git a/src/com/blankj/medium/_015/Solution.java b/src/com/blankj/medium/_015/Solution.java index b4af0e08..0860ac83 100644 --- a/src/com/blankj/medium/_015/Solution.java +++ b/src/com/blankj/medium/_015/Solution.java @@ -10,6 +10,9 @@ * blog : http://blankj.com * time : 2017/10/14 * desc : + * 题意是让你从数组中找出所有三个和为 0 的元素构成的非重复序列,这样的话我们可以把数组先做下排序, + * 然后遍历这个排序数组,用两个指针分别指向当前元素的下一个和数组尾部,判断三者的和与 0 的大小来移动两个指针, + * 其中细节操作就是优化和去重。 * */ public class Solution { @@ -17,20 +20,25 @@ public List> threeSum(int[] nums) { List> list = new ArrayList<>(); int len = nums.length; if (len < 3) return list; + // 先排序 Arrays.sort(nums); int max = nums[len - 1]; + // 最大值小于0, 那么不存在sum == 0 的解 if (max < 0) return list; for (int i = 0; i < len - 2; ) { + // 最小值小于0, 那么不存在sum == 0 的解 if (nums[i] > 0) break; if (nums[i] + 2 * max < 0) { while (nums[i] == nums[++i] && i < len - 2) ; continue; } + // 对于任意一个nums[i],在数组中的其他数中解2sum问题,目标为target-sums[i] int left = i + 1, right = len - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { list.add(Arrays.asList(nums[i], nums[left], nums[right])); + // 去重 while (nums[left] == nums[++left] && left < right) ; while (nums[right] == nums[--right] && left < right) ; } else if (sum < 0) ++left; diff --git a/src/com/blankj/medium/_033/Solution2.java b/src/com/blankj/medium/_033/Solution2.java new file mode 100644 index 00000000..ee0d472e --- /dev/null +++ b/src/com/blankj/medium/_033/Solution2.java @@ -0,0 +1,65 @@ +package com.blankj.medium._033; + +/** + * Created by dash wang on 2/11/18. + */ +public class Solution2 { + + private int findDivisionIndex(int[] nums, int len) { + int left = 0, right = 1; + while (right < len && nums[left] < nums[right]) { + left++; + right++; + } + return left; + } + + private int binarySearchRecursive(int[] nums, int low, int high, int target) { + int foundIndex = -1; + while (low < high) { + int mid = (low + high) / 2; + if (nums[mid] == target) { + return mid; + } else if (nums[mid] > target) { + return binarySearchRecursive(nums, low, mid - 1, target); + } else { + return binarySearchRecursive(nums, mid + 1, high, target); + } + } + return foundIndex; + } + + private int binarySearchIteration(int[] nums, int low, int high, int target) { + int foundIndex = -1; + while(low < high) { + int mid = (low + high) / 2; + if(nums[mid] == target) { + return mid; + } else if(nums[mid] > target) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return foundIndex; + } + + public int search(int[] nums, int target) { + int index = -1; + int len = nums.length; + // Find division index + int divisionIndex = findDivisionIndex(nums, len); + // Decide which side to search + if (nums[0] <= target && nums[divisionIndex] >= target) { + index = binarySearchIteration(nums, 0, divisionIndex, target); + } else if (nums[divisionIndex + 1] <= target && nums[len - 1] >= target) { + index = binarySearchIteration(nums, divisionIndex + 1, len, target); + } + return index; + } + + public static void main(String[] args) { + Solution2 solution2 = new Solution2(); + System.out.println(solution2.search(new int[]{2, 1}, 1)); + } +}