FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

135 by oxcz · Pull Request #896 · algorithm001/algorithm · GitHub

Open

135 #896

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

Filter by extension

Filter by extension .java  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 17 additions & 0 deletions Week_04/id_135/LeetCode_169_135.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;
}
}

Back | FazBrowse Home | New Git URL