From bb363cac31765cdcedf76359f242e338a51df817 Mon Sep 17 00:00:00 2001 From: oxcz Date: Sun, 19 May 2019 22:24:11 +0800 Subject: [PATCH] 4 weeks --- Week_04/id_135/LeetCode_169_135.java | 17 +++++++++++++++++ Week_04/id_135/LeetCode_720_135.java | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 Week_04/id_135/LeetCode_169_135.java create mode 100644 Week_04/id_135/LeetCode_720_135.java diff --git a/Week_04/id_135/LeetCode_169_135.java b/Week_04/id_135/LeetCode_169_135.java new file mode 100644 index 00000000..363ce794 --- /dev/null +++ b/Week_04/id_135/LeetCode_169_135.java @@ -0,0 +1,17 @@ +class Solution { + public int majorityElement(int[] nums) { + int count = 1; + int maj = nums[0]; + for (int i = 1; i < nums.length; i++) { + if (maj == nums[i]) + count++; + else { + count--; + if (count == 0) { + maj = nums[i + 1]; + } + } + } + return maj; + } +} \ No newline at end of file diff --git a/Week_04/id_135/LeetCode_720_135.java b/Week_04/id_135/LeetCode_720_135.java new file mode 100644 index 00000000..508dfee6 --- /dev/null +++ b/Week_04/id_135/LeetCode_720_135.java @@ -0,0 +1,16 @@ +class Solution { + public String longestWord(String[] words) { + Arrays.sort(words); + + Set set = new HashSet<>(); + String res = ""; + for (String s : words) { + //如果单词只有一个字母,那一定是共有的 + if (s.length() == 1 || set.contains(s.substring(0, s.length() - 1))) { + res = s.length() > res.length() ? s : res; + set.add(s); + } + } + return res; + } +} \ No newline at end of file