From 7ca606bcba98b3a3c0e4481e9cde2d7e632b2201 Mon Sep 17 00:00:00 2001 From: yuanguangxin <274841922@qq.com> Date: Thu, 30 Jan 2020 22:47:33 +0800 Subject: [PATCH] add q14/q16/q20 --- .idea/workspace.xml | 97 +++++++++++++++++++++++-------------------- src/q14/Solution.java | 35 ++++++++++++++++ src/q16/Solution.java | 46 ++++++++++++++++++++ src/q20/Solution.java | 37 +++++++++++++++++ 4 files changed, 171 insertions(+), 44 deletions(-) create mode 100644 src/q14/Solution.java create mode 100644 src/q16/Solution.java create mode 100644 src/q20/Solution.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index dc1b861..f687277 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,9 +1,10 @@ - - - + + + + - - - @@ -242,65 +250,66 @@ - - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/src/q14/Solution.java b/src/q14/Solution.java new file mode 100644 index 0000000..2aa67fe --- /dev/null +++ b/src/q14/Solution.java @@ -0,0 +1,35 @@ +package q14; + +/** + * 水平扫描 o(n) + */ +public class Solution { + public String longestCommonPrefix(String[] strs) { + if (strs.length == 0) { + return ""; + } + if (strs.length == 1) { + return strs[0]; + } + String pre = ""; + int i = 0; + while (true) { + if (strs[0].length() == i) { + return pre; + } + char temp = strs[0].charAt(i); + for (int k = 1; k < strs.length; k++) { + if (strs[k].length() == i || temp != strs[k].charAt(i)) { + return pre; + } + } + pre += temp; + i++; + } + } + + public static void main(String[] args) { + String[] s = new String[]{"c", "c"}; + System.out.println(new Solution().longestCommonPrefix(s)); + } +} diff --git a/src/q16/Solution.java b/src/q16/Solution.java new file mode 100644 index 0000000..8e30633 --- /dev/null +++ b/src/q16/Solution.java @@ -0,0 +1,46 @@ +package q16; + +import java.util.Arrays; + +/** + * q15类型题 数组遍历 + 双指针遍历 o(n^2) + */ + +public class Solution { + public int threeSumClosest(int[] nums, int target) { + if (nums.length < 3) { + return 0; + } + + Arrays.sort(nums); + int rs = nums[0] + nums[1] + nums[2]; + + for (int i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + int left = i + 1; + int right = nums.length - 1; + while (left < right) { + int sum = nums[i] + nums[left] + nums[right]; + int c = sum - target; + if (Math.abs(c) < Math.abs(rs - target)) { + rs = sum; + } + if (c == 0) { + return target; + } else if (c > 0) { + right--; + } else { + left++; + } + } + } + return rs; + } + + public static void main(String[] args) { + int[] a = new int[]{-3, -2, -5, 3, -4}; + System.out.println(new Solution().threeSumClosest(a, -1)); + } +} diff --git a/src/q20/Solution.java b/src/q20/Solution.java new file mode 100644 index 0000000..8d63af7 --- /dev/null +++ b/src/q20/Solution.java @@ -0,0 +1,37 @@ +package q20; + +import java.util.Stack; + +public class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + for (int i = 0; i < s.length(); i++) { + char t = s.charAt(i); + if (t == '(' || t == '[' || t == '{') { + stack.push(t); + } else { + if (stack.empty()) { + return false; + } + if (t == ')') { + if (stack.pop() != '(') { + return false; + } + } else if (t == ']') { + if (stack.pop() != '[') { + return false; + } + } else { + if (stack.pop() != '{') { + return false; + } + } + } + } + return stack.empty(); + } + + public static void main(String[] args) { + System.out.println(new Solution().isValid("()")); + } +}