2/27每日总结

所花时间:3小时

代码量:如下:

博客量:本学期截至目前11篇

了解到的知识点:求最长单词链

在今天上午没课,学习了一点Android stuio的知识,然后下午上了4节java课,学习了最长单词链。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
}

  

posted @   南北啊  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
1 2 3
4
点击右上角即可分享
微信分享提示