LeetCode 748. 最短补全词 / 911. 在线选举 / 709. 转换成小写字母

Posted Zephyr丶J

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 748. 最短补全词 / 911. 在线选举 / 709. 转换成小写字母相关的知识,希望对你有一定的参考价值。

748. 最短补全词

2021.12.10 每日一题

题目描述

给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 。

补全词 是一个包含 licensePlate 中所有的字母的单词。在所有补全词中,最短的那个就是 最短补全词 。

在匹配 licensePlate 中的字母时:

忽略 licensePlate 中的 数字和空格 。
不区分大小写。
如果某个字母在 licensePlate 中出现不止一次,那么该字母在补全词中的出现次数应当一致或者更多。
例如:licensePlate = “aBc 12c”,那么它的补全词应当包含字母 ‘a’、‘b’ (忽略大写)和两个 ‘c’ 。可能的 补全词 有 “abccdef”、“caaacab” 以及 “cbca” 。

请你找出并返回 words 中的 最短补全词 。题目数据保证一定存在一个最短补全词。当有多个单词都符合最短补全词的匹配条件时取 words 中 最靠前的 那个。

示例 1:

输入:licensePlate = “1s3 PSt”, words = [“step”, “steps”, “stripe”, “stepple”]
输出:“steps”
解释:最短补全词应该包括 “s”、“p”、“s”(忽略大小写) 以及 “t”。
“step” 包含 “t”、“p”,但只包含一个 “s”,所以它不符合条件。
“steps” 包含 “t”、“p” 和两个 “s”。
“stripe” 缺一个 “s”。
“stepple” 缺一个 “s”。
因此,“steps” 是唯一一个包含所有字母的单词,也是本例的答案。

示例 2:

输入:licensePlate = “1s3 456”, words = [“looks”, “pest”, “stew”, “show”]
输出:“pest”
解释:licensePlate 只包含字母 “s” 。所有的单词都包含字母 “s” ,其中 “pest”、“stew”、和 “show” 三者最短。答案是 “pest” ,因为它是三个单词中在 words 里最靠前的那个。

示例 3:

输入:licensePlate = “Ah71752”, words = [“suggest”,“letter”,“of”,“husband”,“easy”,“education”,“drug”,“prevent”,“writer”,“old”]
输出:“husband”

示例 4:

输入:licensePlate = “OgEu755”, words = [“enough”,“these”,“play”,“wide”,“wonder”,“box”,“arrive”,“money”,“tax”,“thus”]
输出:“enough”

示例 5:

输入:licensePlate = “iMSlpe4”, words = [“claim”,“consumer”,“student”,“camera”,“public”,“never”,“wonder”,“simple”,“thought”,“use”]
输出:“simple”

提示:

1 <= licensePlate.length <= 7
licensePlate 由数字、大小写字母或空格 ’ ’ 组成
1 <= words.length <= 1000
1 <= words[i].length <= 15
words[i] 由小写英文字母组成

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shortest-completing-word
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

简单题,代码还写了一长串…

class Solution 
    public String shortestCompletingWord(String licensePlate, String[] words) 
        int[] count = new int[26];
        int kind = 0;   //记录种类
        for(int i = 0; i < licensePlate.length(); i++)
            char c = licensePlate.charAt(i);
            if(Character.isLowerCase(c))
                if(count[c - 'a'] == 0)
                    kind++;
                count[c - 'a']++;
            
            else if(Character.isUpperCase(c))
                if(count[c - 'A'] == 0)
                    kind++;
                count[c - 'A']++;
            
        
        
        int l = words.length;
        int min = 16;
        String res = "";
        for(int i = 0; i < l; i++)
            String s = words[i];
            int[] temp = new int[26];
            int k = 0;
            for(char c : s.toCharArray())
                if(temp[c - 'a'] != -1)
                    temp[c - 'a']++;
                if(count[c - 'a'] > 0 && temp[c - 'a'] >= count[c - 'a'])
                    //将该位置标记
                    temp[c - 'a'] = -1;
                    k++;
                     
            
            
            if(k == kind)
                if(s.length() < min)
                    res = s;
                    min = s.length();
                
            
        
        return res;
    

911. 在线选举

2021.12.11 每日一题

题目描述

给你两个整数数组 persons 和 times 。在选举中,第 i 张票是在时刻为 times[i] 时投给候选人 persons[i] 的。

对于发生在时刻 t 的每个查询,需要找出在 t 时刻在选举中领先的候选人的编号。

在 t 时刻投出的选票也将被计入我们的查询之中。在平局的情况下,最近获得投票的候选人将会获胜。

实现 TopVotedCandidate 类:

TopVotedCandidate(int[] persons, int[] times) 使用 persons 和 times 数组初始化对象。
int q(int t) 根据前面描述的规则,返回在时刻 t 在选举中领先的候选人的编号。

示例:

输入:
[“TopVotedCandidate”, “q”, “q”, “q”, “q”, “q”, “q”]
[[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]]
输出:
[null, 0, 1, 1, 0, 0, 1]

解释:
TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
topVotedCandidate.q(3); // 返回 0 ,在时刻 3 ,票数分布为 [0] ,编号为 0 的候选人领先。
topVotedCandidate.q(12); // 返回 1 ,在时刻 12 ,票数分布为 [0,1,1] ,编号为 1 的候选人领先。
topVotedCandidate.q(25); // 返回 1 ,在时刻 25 ,票数分布为 [0,1,1,0,0,1] ,编号为 1 的候选人领先。(在平局的情况下,1 是最近获得投票的候选人)。
topVotedCandidate.q(15); // 返回 0
topVotedCandidate.q(24); // 返回 0
topVotedCandidate.q(8); // 返回 1

提示:

1 <= persons.length <= 5000
times.length == persons.length
0 <= persons[i] < persons.length
0 <= times[i] <= 10^9
times 是一个严格递增的有序数组
times[0] <= t <= 10^9
每个测试用例最多调用 10^4 次 q

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/online-election
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

先记录每个时间最多的候选人是谁,然后对于给定的时间,用二分查找找出这个人
一个简单的二分

class TopVotedCandidate 
    //首先记录每个时间区间内的最大值,然后t查找就是用二分查找完事
    int[] vote;
    int[] times;
    int n;
    public TopVotedCandidate(int[] persons, int[] times) 
        n = persons.length;
        this.times = times;
        vote = new int[n];
        int maxperson = persons[0];     //票最多的人
        int maxticket = 1;              //最多票的个数
        Map<Integer, Integer> map = new HashMap<>();
        map.put(persons[0], 1);
        vote[0] = persons[0];
        for(int i = 1; i < n; i++)
            map.put(persons[i], map.getOrDefault(persons[i], 0) + 1);
            int temp = map.get(persons[i]); //当前票数
            //如果当前票数大于等于最大票数,换人
            if(temp >= maxticket)
                maxperson = persons[i];
                maxticket = temp;
            
            vote[i] = maxperson;
        
    
    
    public int q(int t) 
        int left = 0;
        int right = n - 1;
        while(left < right)
            int mid = (right - left + 1) / 2 + left;
            int time = times[mid];
            if(time <= t)
                left = mid;
            else
                right = mid - 1;
            
        
        return vote[left];
    


/**
 * Your TopVotedCandidate object will be instantiated and called as such:
 * TopVotedCandidate obj = new TopVotedCandidate(persons, times);
 * int param_1 = obj.q(t);
 */

709. 转换成小写字母

2021.12.13 每日一题

题目描述

给你一个字符串 s ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。

示例 1:

输入:s = “Hello”
输出:“hello”

示例 2:

输入:s = “here”
输出:“here”

示例 3:

输入:s = “LOVELY”
输出:“lovely”

提示:

1 <= s.length <= 100
s 由 ASCII 字符集中的可打印字符组成

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/to-lower-case
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

调用API

class Solution 
    public String toLowerCase(String s) 
        return s.toLowerCase();
    

以上是关于LeetCode 748. 最短补全词 / 911. 在线选举 / 709. 转换成小写字母的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode748 最短补全词[数组 字母] HERODING的LeetCode之路

解题报告Leecode 748. 最短补全词——Leecode每日一题系列

解题报告Leecode 748. 最短补全词——Leecode每日一题系列

《LeetCode之每日一题》:231.最短补全词

算法千题案例每日LeetCode打卡——92.最短补全词

算法千题案例每日LeetCode打卡——92.最短补全词