From 0c85130435d7c0bdc230ba0554ede99d3e65c8bd Mon Sep 17 00:00:00 2001 From: livenQiang Date: Mon, 17 Jun 2019 21:22:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=B8=A4=E6=95=B0=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_02/id_5/LeetCode_1_05.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Week_02/id_5/LeetCode_1_05.java diff --git a/Week_02/id_5/LeetCode_1_05.java b/Week_02/id_5/LeetCode_1_05.java new file mode 100644 index 00000000..2db8ac60 --- /dev/null +++ b/Week_02/id_5/LeetCode_1_05.java @@ -0,0 +1,28 @@ +import java.util.HashMap; +import java.util.Map; + +public class LeetCode_1_05 { + + /** + * 两数之和 + * - 做这个题的时候,已经做了三数之和了。 + * - 那我就不再使用枚举的方法了 + * - + * @param nums + * @param target + * @return + */ + public int[] twoSum(int[] nums, int target) { + Map book = new HashMap<>(); + for (int i = 0;i Date: Mon, 17 Jun 2019 22:41:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=B8=8D=E9=87=8D=E5=A4=8D=E5=AD=97=E7=AC=A6=E5=AD=90=E4=B8=B2?= =?UTF-8?q?=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_02/id_5/LeetCode_3_05.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Week_02/id_5/LeetCode_3_05.java diff --git a/Week_02/id_5/LeetCode_3_05.java b/Week_02/id_5/LeetCode_3_05.java new file mode 100644 index 00000000..904fe6e0 --- /dev/null +++ b/Week_02/id_5/LeetCode_3_05.java @@ -0,0 +1,30 @@ +import java.util.HashSet; + +public class LeetCode_3_05 { + + /** + * 计算 字符串中不含有重复字符的最大子串的长度 + * - 要点 滑动窗口 HashSet + * + * @param s + * @return + */ + public int lengthOfLongestSubstring(String s) { + int length = s.length(); + HashSet window = new HashSet<>(); + int i = 0, j = 0, maxLength = 0; + + while (i < length && j < length) { + if (!window.contains(s.charAt(j))) { + window.add(s.charAt(j)); + j++; + maxLength = Math.max(maxLength, j - i); + }else{ + window.remove(s.charAt(i)); + i++; + } + } + return maxLength; + } + +}