Skip to content

Commit 0e59909

Browse files
Merge pull request algorithm001#705 from GradyWong/master
BJ001-1904069 第四周作业
2 parents bfb0f1f + ef4280c commit 0e59909

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Week_04/id_69/LeetCode_169_69.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int majorityElement(int[] nums) {
3+
// 初始放入第一个元素并开始计数
4+
int maj = nums[0];
5+
int count = 1;
6+
// 第二个元素开始比较
7+
for (int i = 1; i < nums.length; i++) {
8+
if (maj == nums[i])
9+
count++;
10+
else {
11+
count--;
12+
if (count == 0) {
13+
maj = nums[i + 1];
14+
}
15+
}
16+
}
17+
return maj;
18+
}
19+
}

Week_04/id_69/LeetCode_720_69.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public String longestWord(String[] words) {
3+
// 对字符串数组进行排序
4+
Arrays.sort(words);
5+
6+
Set<String> set = new HashSet<>();
7+
String result = new String();
8+
for (String str : words) {
9+
if (set.contains(str.substring(0, str.length() - 1)) || str.length() == 1) {
10+
// 保存更长的哪一个字符串
11+
result = str.length() > result.length() ? str : result;
12+
set.add(str);
13+
}
14+
}
15+
return result;
16+
}
17+
}

0 commit comments

Comments
 (0)