From 22d52a17fc8ccb25edfcf36f367807252b13117c Mon Sep 17 00:00:00 2001 From: GradyWong Date: Sun, 12 May 2019 18:07:07 +0800 Subject: [PATCH 1/2] Create LeetCode_720_69.java --- Week_04/id_69/LeetCode_720_69.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Week_04/id_69/LeetCode_720_69.java diff --git a/Week_04/id_69/LeetCode_720_69.java b/Week_04/id_69/LeetCode_720_69.java new file mode 100644 index 00000000..265af967 --- /dev/null +++ b/Week_04/id_69/LeetCode_720_69.java @@ -0,0 +1,17 @@ +class Solution { + public String longestWord(String[] words) { + // 对字符串数组进行排序 + Arrays.sort(words); + + Set set = new HashSet<>(); + String result = new String(); + for (String str : words) { + if (set.contains(str.substring(0, str.length() - 1)) || str.length() == 1) { + // 保存更长的哪一个字符串 + result = str.length() > result.length() ? str : result; + set.add(str); + } + } + return result; + } +} From ef4280c19497ee2ad33f884591a2715ad9d771ff Mon Sep 17 00:00:00 2001 From: GradyWong Date: Sun, 12 May 2019 18:08:18 +0800 Subject: [PATCH 2/2] Create LeetCode_169_69.java --- Week_04/id_69/LeetCode_169_69.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Week_04/id_69/LeetCode_169_69.java diff --git a/Week_04/id_69/LeetCode_169_69.java b/Week_04/id_69/LeetCode_169_69.java new file mode 100644 index 00000000..aac3470e --- /dev/null +++ b/Week_04/id_69/LeetCode_169_69.java @@ -0,0 +1,19 @@ +class Solution { + public int majorityElement(int[] nums) { + // 初始放入第一个元素并开始计数 + int maj = nums[0]; + int count = 1; + // 第二个元素开始比较 + for (int i = 1; i < nums.length; i++) { + if (maj == nums[i]) + count++; + else { + count--; + if (count == 0) { + maj = nums[i + 1]; + } + } + } + return maj; + } +}