Skip to content

082-第四周作业 #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions Week_04/id_82/LeetCode_455_082.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public int findContentChildren(int[] g, int[] s) {
int ret = 0;

Arrays.sort(g);
Arrays.sort(s);

int i = 0, j = 0;
while (i < g.length && j < s.length) {
if (g[i] <= s[j]) {
ret++;
i++;
j++;
}else if (g[i] > s[j]) {
j++;
}
}

return ret;
}
29 changes: 29 additions & 0 deletions Week_04/id_82/LeetCode_720_082.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public String longestWord(String[] words) {
//给定字符串数组,找出包含最长的字符串(包含其他单词的字母),如果存在相同长度,取字母排序小的
//思路:对数组排序,再利用Set对字母存储,小的单词一定包含在后面大的单词里面。后面只需要取前缀相同的

Set<String> set=new HashSet<String>();
for(int i=0;i<words.length;i++){
set.add(words[i]);
}
int length=0;
String word="";
for(int i=0;i<words.length;i++){
if(words[i].length()>length||(words[i].length()==length&&words[i].compareTo(word)<0)){
//如果存在相同长度的字符串,取字母排序较小的
int len=words[i].length();
while(len>0&&set.contains(words[i].substring(0,len))){
//求相同的部分
len--;
}
if(len==0){
//说明该单词的所有字符串均为公共字母,将其标记为匹配串
length=words[i].length();
word=words[i];
}
}
}
return word;
}
}