首页 > 其他 > 详细

leetcode 0215

时间:2020-02-15 12:46:50      阅读:50      评论:0      收藏:0      [点我收藏+]

0215 algo

? 1002. 查找常用字符

https://leetcode-cn.com/problems/find-common-characters

描述

给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。

你可以按任意顺序返回答案。

?

示例 1:

输入:["bella","label","roller"]
输出:["e","l","l"]
示例 2:

输入:["cool","lock","cook"]
输出:["c","o"]

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

解答

本来的思路是 两个 array: book1 book2

book1 保存 list[string]list[0] 里面的 所有出现的字符的;

如下图:(todo 0215 实现)

技术分享图片

java other‘s implementation(todo watch me)

public List<String> commonChars(String[] A) {
    List<String> list = new ArrayList<>();
    int[] res = new int[26];
    for (char c : A[0].toCharArray()) {
        res[c - 'a']++;
    }
    for (int i = 1; i < A.length; i++) {
        int[] temp = new int[26];
        for (char c : A[i].toCharArray()) {
            temp[c - 'a']++;
        }
        for (int j = 0; j < 26; j++) {
            res[j] = Math.min(res[j], temp[j]);
        }
    }
    for (int i = 0; i < res.length; i++) {
        if (res[i] > 0) {
            for (int j = 0; j < res[i]; j++) {
                list.add(((char) ('a' + i) + ""));
            }
        }
    }
    return list;
}

cpp

py

class Solution:
    def commonChars(self, A: List[str]) -> List[str]:
        # mainly, we use Sting.count()
        ret = []
        if not A:
            return ret
        key = set(A[0]) # also, set("abcddd") return u: set(['a','b','c','d'])
        for k in key:
            minuim = min(a.count(k) for a in A)
            ret += minuim * k # here is the string CAT way
        return ret
'''
执行用时 :
72 ms
, 在所有 Python3 提交中击败了
19.02%
的用户
内存消耗 :
13.2 MB
, 在所有 Python3 提交中击败了
44.24%
的用户
'''

? 821. 字符的最短距离

https://leetcode-cn.com/problems/shortest-distance-to-a-character

描述

给定一个字符串?S?和一个字符?C。返回一个代表字符串?S?中每个字符到字符串?S?中的字符?C?的最短距离的数组。

示例 1:

输入: S = "loveleetcode", C = 'e'
输出: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

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

解答

自己的思路是:两个左右兵 如下:

技术分享图片

cpp

技术分享图片

class Solution {
public:
    vector<int> shortestToChar(string S, char C) {
        vector<int> pos(S.size(), -2);
        int pre = -1;
        //direction: right to left
        for (int i = S.size() - 1; i >= 0; i--){
            if(S[i] == C) {
                pre = i;
                pos[i] = 0;
            } else if(pre != -1) {
                pos[i] = pre - i;
            }
        }
        // clean env
        pre = -1;
        //direction: left to right
        for (int i = 0; i < S.size(); i++) {

            if(pre != -1) {
                pos[i] = min(i - pre, pos[i]);
            }
            //this is for the most right part(to the right of last C),
            // where all the pos[x] == -1, cause we dont set in first loop
            // first loop : <-----
            if(pos[i] == -2){
                pos[i] = i - pre;
            }
            // u must move this if at last ,why? todo
            if(S[i] == C) {
                pre = i;
                //pos[i] = 0;//useless, cause in first loop, we already set it to 0
            } 
        }
        return pos;
    }
};
/*执行用时 :
4 ms
, 在所有 C++ 提交中击败了
94.40%
的用户
内存消耗 :
9.3 MB
, 在所有 C++ 提交中击败了
5.10%
的用户*/

py

技术分享图片

class Solution:
    def shortestToChar(self, S: str, C: str) -> List[int]:
        c_pos = [i for i in range(len(S)) if C == S[i]]
        return ([min(abs(x - i) for i in c_pos) for x in range(len(S))])
'''
执行用时 :
104 ms
, 在所有 Python3 提交中击败了
21.55%
的用户
内存消耗 :
13.1 MB
, 在所有 Python3 提交中击败了
52.14%
的用户
'''

?

https://leetcode-cn.com/problems/reformat-department-table

描述

解答

cpp

py

?

https://leetcode-cn.com/problems/n-repeated-element-in-size-2n-array

描述

解答

cpp

py

leetcode 0215

原文:https://www.cnblogs.com/paulkg12/p/12311066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!