目录
0215 algo
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 实现)
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;
}
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%
的用户
'''
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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
自己的思路是:两个左右兵 如下:
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%
的用户*/
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
https://leetcode-cn.com/problems/n-repeated-element-in-size-2n-array
原文:https://www.cnblogs.com/paulkg12/p/12311066.html