From c71f90dfb68c2d6b52fba630b00e65ba3f8ec6da Mon Sep 17 00:00:00 2001 From: yuanguangxin <274841922@qq.com> Date: Wed, 29 Jan 2020 02:13:06 +0800 Subject: [PATCH] add q3/q8/q11 --- .idea/workspace.xml | 107 +++++++++++++++++++++++------------------- src/q11/Solution.java | 32 +++++++++++++ src/q3/Solution.java | 27 +++++++++++ src/q8/Solution.java | 40 ++++++++++++++++ 4 files changed, 158 insertions(+), 48 deletions(-) create mode 100644 src/q11/Solution.java create mode 100644 src/q3/Solution.java create mode 100644 src/q8/Solution.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c365dad..74ea762 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,10 @@ - - + + + + - - - @@ -223,65 +233,66 @@ - - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/src/q11/Solution.java b/src/q11/Solution.java new file mode 100644 index 0000000..cbdfbaf --- /dev/null +++ b/src/q11/Solution.java @@ -0,0 +1,32 @@ +package q11; + +/** + * 双指针遍历 o(n) + */ +public class Solution { + public int maxArea(int[] height) { + if (height.length < 2) { + return 0; + } + int left = 0; + int right = height.length - 1; + int result = 0; + while (right > left) { + int c = (Math.min(height[right], height[left])) * (right - left); + if (c >= result) { + result = c; + } + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + return result; + } + + public static void main(String[] args) { + int[] a = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}; + System.out.println(new Solution().maxArea(a)); + } +} diff --git a/src/q3/Solution.java b/src/q3/Solution.java new file mode 100644 index 0000000..510edd3 --- /dev/null +++ b/src/q3/Solution.java @@ -0,0 +1,27 @@ +package q3; + +import java.util.HashMap; + +/** + * Hash+双指针滑动窗口 o(n) + */ +public class Solution { + public int lengthOfLongestSubstring(String s) { + int left = 0; + int right = 0; + int len = 0; + HashMap map = new HashMap<>(); + while (right < s.length()) { + Integer index = map.get(s.charAt(right)); + map.put(s.charAt(right), right); + if (index != null && index >= left) { + left = index + 1; + } + if (right - left + 1 > len) { + len = right - left + 1; + } + right++; + } + return len; + } +} diff --git a/src/q8/Solution.java b/src/q8/Solution.java new file mode 100644 index 0000000..bc2f875 --- /dev/null +++ b/src/q8/Solution.java @@ -0,0 +1,40 @@ +package q8; + +/** + * o(n) 重点还是判断溢出 + */ +public class Solution { + + public int myAtoi(String str) { + str = str.trim(); + if (str.length() < 1) { + return 0; + } + boolean negative = false; + if (str.charAt(0) == '-') { + negative = true; + str = str.substring(1); + } else if (str.charAt(0) == '+') { + str = str.substring(1); + } + + int rs = 0; + for (int i = 0; i < str.length(); i++) { + char t = str.charAt(i); + if (Character.isDigit(t)) { + int temp = rs * 10 - '0' + t; + if ((temp - t + '0') / 10 != rs || temp < 0) { + return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE; + } + rs = temp; + } else { + break; + } + } + return negative ? -rs : rs; + } + + public static void main(String[] args) { + System.out.println(new Solution().myAtoi("2147483648")); + } +}