diff --git a/Week_01/zby/LeetCode_15.java b/Week_01/zby/LeetCode_15.java new file mode 100644 index 0000000000000000000000000000000000000000..815a916dc3a3d8ee4df562fdf16c8693595b22af --- /dev/null +++ b/Week_01/zby/LeetCode_15.java @@ -0,0 +1,142 @@ +package zby; + +import com.sun.javafx.binding.StringFormatter; + +import java.text.Format; +import java.util.*; + +public class LeetCode_15 { + + /** + * 15. 三数之和 + *

+ * 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。 + *

+ * 注意:答案中不可以包含重复的三元组。 + * + * @param args + */ + public static void main(String[] args) { + LeetCode_15 code = new LeetCode_15(); + int[] nums = {-1, 0, 1, 2, -1, -4}; + List> lists = code.threeSum3(nums); + System.out.println(lists); + } + + /** + * 19 ms 41.2 MB + * + * @param nums + * @return + */ + private List> threeSum3(int[] nums) { + List result = new ArrayList<>(nums.length / 3); + Arrays.sort(nums); + int v, i, j; + for (int k = 0; k < nums.length; k++) { + if (nums[k] > 0) break; + if (k > 0 && nums[k] == nums[k - 1]) continue; + i = k + 1; + j = nums.length - 1; + while (i < j) { + v = nums[k] + nums[i] + nums[j]; + if (v == 0) { + result.add(new int[]{nums[k], nums[i++], nums[j--]}); + while (i < j && nums[i] == nums[i - 1]) i++; + while (i < j && nums[j] == nums[j + 1]) j--; + } else if (v < 0) { + i++; + } else { + j--; + } + } + } + return result; + } + + /** + * 18 ms 42.6 MB + * + * @param nums + * @return + */ + private List> threeSum2(int[] nums) { + List> result = new ArrayList<>(); + if (nums == null || nums.length == 0) return result; + Arrays.sort(nums); + for (int k = 0; k < nums.length; k++) { + if (nums[k] > 0) break; + if (k > 0 && nums[k] == nums[k - 1]) continue; + int i = k + 1, j = nums.length - 1; + int target = -nums[k]; + while (i < j) { + if (nums[i] + nums[j] > target) j--; + else if (nums[i] + nums[j] < target) i++; + else { + ArrayList temp = new ArrayList<>(); + temp.add(nums[i]); + temp.add(nums[j]); + temp.add(nums[k]); + result.add(temp); + while (i < j && nums[i] == nums[i + 1]) i++; + while (i < j && nums[j] == nums[j - 1]) j--; + i++; + j--; + } + } + } + return result; + } + + /** + * 31 ms 42.4 MB + * + * @param nums + * @return + */ + private List> threeSum1(int[] nums) { + List> result = new ArrayList<>(); + Arrays.sort(nums); + for (int k = 0; k < nums.length - 2; k++) { + if (nums[k] > 0) break; + if (k > 0 && nums[k - 1] == nums[k]) continue; + int i = k + 1, j = nums.length - 1; + while (i < j) { + int s = nums[i] + nums[j] + nums[k]; + if (s < 0) { + while (i < j && nums[i] == nums[++i]) ; + } else if (s > 0) { + while (i < j && nums[j] == nums[--j]) ; + } else { + result.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], nums[k]))); + while (i < j && nums[i] == nums[++i]) ; + while (i < j && nums[j] == nums[--j]) ; + } + } + } + return result; + } + + + /** + * 超出时间限制 N/A N/A + * + * @param nums + * @return + */ + public List> threeSum(int[] nums) { + Set> result = new HashSet<>(); + for (int i = 0; i < nums.length - 2; i++) { + for (int j = i + 1; j < nums.length - 1; j++) { + for (int k = j + 1; k < nums.length; k++) { + if (nums[i] + nums[j] + nums[k] == 0) { + ArrayList list = new ArrayList<>(Arrays.asList(nums[i], nums[j], nums[k])); + Collections.sort(list); + result.add(list); + } + } + } + } + return new ArrayList<>(result); + } +} diff --git a/Week_01/zby/LeetCode_70.java b/Week_01/zby/LeetCode_70.java new file mode 100644 index 0000000000000000000000000000000000000000..68c5a415ee4b046eafa974cc7d44d3dd58b44782 --- /dev/null +++ b/Week_01/zby/LeetCode_70.java @@ -0,0 +1,82 @@ +package zby; + +import javax.sound.midi.Soundbank; + +public class LeetCode_70 { + + /** + * 70. 爬楼梯 + * 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 + *

+ * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? + *

+ * 注意:给定 n 是一个正整数。 + * + * @param args + */ + public static void main(String[] args) { + LeetCode_70 code = new LeetCode_70(); + + long s = System.currentTimeMillis(); + System.out.println(code.climbStairs2(50)); + long e = System.currentTimeMillis() - s; + System.out.println(e); + } + + + /** + * 超出时间限制 N/A N/A + * + * @param n + * @return + */ + public int climbStairs(int n) { + if (n < 2) { + return 1; + } + return climbStairs(n - 1) + climbStairs(n - 2); + } + + + /** + * 0 ms 34.9 MB + * + * @param n + * @return + */ + public int climbStairs1(int n) { + arr = new int[n]; + return clim(n); + } + + private int[] arr; + + private int clim(int n) { + if (n < 2) { + return 1; + } + int t = arr[n - 1]; + if (t == 0) { + t = clim(n - 1) + clim(n - 2); + arr[n - 1] = t; + } + return t; + } + + /** + * 0 ms 34.8 MB + * + * @param n + * @return + */ + public int climbStairs2(int n) { + int p = 0, q = 0, r = 1; + for (int i = 1; i <= n; i++) { + p = q; + q = r; + r = p + q; + } + return r; + } + +}