Skip to content
Open

135 #896

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Week_04/id_135/LeetCode_169_135.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 16 additions & 0 deletions Week_04/id_135/LeetCode_720_135.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public String longestWord(String[] words) {
Arrays.sort(words);

Set<String> 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;
}
}