2/27每日总结
所花时间:3小时
代码量:如下:
博客量:本学期截至目前11篇
了解到的知识点:求最长单词链
在今天上午没课,学习了一点Android stuio的知识,然后下午上了4节java课,学习了最长单词链。
代码如下:
public int longestStrChain(String[] words) {
Arrays.sort(words, Comparator.comparingInt(String::length));
Map<String, Integer> map = new HashMap<>();
int res = 0;
for (String word : words) {
int max = 0;
for (int i = 0; i < word.length(); i++) {
String s = word.substring(0, i) + word.substring(i + 1);
if (map.containsKey(s)) {
max = Math.max(max, map.get(s));
}
}
map.put(word, max + 1);
res = Math.max(res, max+1);
}
return res;
}
func longestStrChain(words []string) int {
sort.Slice(words, func(i, j int) bool {
return len(words[i])<len(words[j])
})
mapx:=make(map[string]int,0)
res := 0
for _, word := range words {
max := 0
for i := 0; i < len(word); i++ {
s:=word[0:i]+word[i+1:]
if v,ok:=mapx[s];ok {
max = getMax(max, v)
}
}
mapx[word]= max + 1
res = getMax(res, max+1)
}
return res
}
func getMax(x,y int) int{
if x>y {
return x
}
return y
}

浙公网安备 33010602011771号