From 7b9990113e41a1c7f05616caaacc64ea6bfb91c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=9F=E8=B6=85?= Date: Sun, 16 Jun 2019 22:22:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BD=9C=E4=B8=9A):=20=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_02/id_9/LeetCode_242_9.java | 67 ++++++++++++++++++++++++++++++++ Week_02/id_9/LeetCode_98_9.java | 29 ++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 Week_02/id_9/LeetCode_242_9.java create mode 100644 Week_02/id_9/LeetCode_98_9.java diff --git a/Week_02/id_9/LeetCode_242_9.java b/Week_02/id_9/LeetCode_242_9.java new file mode 100644 index 00000000..9782a887 --- /dev/null +++ b/Week_02/id_9/LeetCode_242_9.java @@ -0,0 +1,67 @@ +package com.github.lifelab.leetcode.problemset; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * 有效的字母异位词 @see https://leetcode-cn.com/problems/valid-anagram/ + * + * @author Weichao Li (liweichao0102@gmail.com) + * @since 2019-06-16 + */ +public class Solution242 { + + public boolean isAnagram(String s, String t) { + if (Objects.isNull(s) || Objects.isNull(t)) { + return false; + } + if (s.length() != t.length()) { + return false; + } + + char[] temp = new char[26]; + + for (int i = 0; i < s.length(); i++) { + temp[s.charAt(i) - 'a']++; + temp[t.charAt(i) - 'a']--; + } + + for (int i = 0; i < temp.length; i++) { + if (temp[i] != 0) { + return false; + } + } + return true; + } + + public boolean isAnagram1(String s, String t) { + if (Objects.isNull(s) || Objects.isNull(t)) { + return false; + } + if (s.length() != t.length()) { + return false; + } + char[] sarray = s.toCharArray(); + char[] tarray = t.toCharArray(); + Arrays.sort(sarray); + Arrays.sort(tarray); + return Arrays.equals(sarray, tarray); + } + + public boolean isAnagram2(String s, String t) { + if (Objects.isNull(s) || Objects.isNull(t)) { + return false; + } + if (s.length() != t.length()) { + return false; + } + Map maps = new HashMap<>(t.length()); + for (int i = 0; i < s.length(); i++) { + maps.compute(s.charAt(i), (k, v) -> maps.getOrDefault(k, 0) + 1); + maps.compute(t.charAt(i), (k, v) -> maps.getOrDefault(k, 0) - 1); + } + return maps.values().stream().parallel().allMatch(e -> Objects.isNull(e) | e == 0); + } +} \ No newline at end of file diff --git a/Week_02/id_9/LeetCode_98_9.java b/Week_02/id_9/LeetCode_98_9.java new file mode 100644 index 00000000..d323c83b --- /dev/null +++ b/Week_02/id_9/LeetCode_98_9.java @@ -0,0 +1,29 @@ +package com.github.lifelab.leetcode.problemset; + +/** + * 验证二叉搜索树 @see https://leetcode-cn.com/problems/validate-binary-search-tree/submissions/ + * + * @author Weichao Li (liweichao0102@gmail.com) + * @since 2019-06-16 + */ +public class Solution98 { + + long temp = Long.MIN_VALUE; + + public boolean isValidBST(TreeNode root) { + //terminator + if (root == null) { + return true; + } + + // process + if (isValidBST(root.left)) { + if (temp < root.val) { + temp = root.val; + //drill down + return isValidBST(root.right); + } + } + return false; + } +}