当前位置:网站首页>五月刷题02——字符串
五月刷题02——字符串
2022-07-06 09:02:00 【追逐梦想的阿光】
五月刷题02——字符串
今日刷题内容: 字符串
前言
- 一个算法废材的刷题之路开更了, 更新每天刷题的题解内容
- 注重个人理解,看难度更新题目数量
- 题目来源于力扣
- 新开专栏,争取每日都能做出至少一题=v=
- 语言java、python、c\c++
一、今日题目
二、解题思路
1. 500. 键盘行*****
- 用三个字符串来记录每个键盘行的字符
- 通过每个单词的第一个字符确认所在字符行
- 验证后序字符是否在同一字符行
- 如果在同一字符行,将结果加入到结果列表中
class Solution:
def findWords(self, words: List[str]) -> List[str]:
str1 = "qwertyuiopQWERTYUIOP"
str2 = "asdfghjklASDFGHJKLK"
str3 = "zxcvbnmZXCVBNM"
ret = []
# 通过每个单词的开头确认来自哪个键盘行
for word in words:
flag = True # 标识当前单词是否来自同一个键盘行
if word[0] in str1:
for i in range(1, len(word)):
if word[i] not in str1:
flag = False
break
elif word[0] in str2:
for i in range(1, len(word)):
if word[i] not in str2:
flag = False
break
elif word[0] in str3:
for i in range(1, len(word)):
if word[i] not in str3:
flag = False
break
if flag:
ret.append(word) # 如果来自同一个键盘行
return ret
2. 1160. 拼写单词*****
- 用一个
hash表来记录已掌握的字符- 遍历每个单词的每个字母,用
flag来标识当前单词- 遍历当前单词的所有字符,一旦有一个字符不在已掌握的字符中,
flag为false,退出当前循环- 下一个单词时需要恢复原来的
hash表,使用到深复制
class Solution {
public int countCharacters(String[] words, String chars) {
int[] hash = new int[256];
// 用一个hash表来存储已掌握的词汇
for(char c: chars.toCharArray()){
hash[c]++;
}
int ret = 0; // 统计字母个数
int[] temp = new int[256];
for (String word: words){
boolean flag = true;
// 恢复原来的hash表
System.arraycopy(hash, 0, temp, 0, hash.length);
for (char w: word.toCharArray()){
if (temp[w] <= 0){
flag = false;
break;
} else{
temp[w]--;
}
}
// 如果单词中的每个字符都在已掌握的字符串中
if (flag){
ret += word.length();
}
}
return ret;
}
}
3. 1047. 删除字符串中的所有相邻重复项*****
- 定义两个相邻指针
l, r,采用滑动窗口的方式移动两个指针- 需要注意的是,当左指针
l大于0时,此时左右指针的字符相同,将相同字符删除后应该将左右指针回退- 否则将左右指针右移即可
class Solution {
public String removeDuplicates(String s) {
int l = 0;
int r = 1;
int n = s.length();
StringBuffer sb = new StringBuffer(s); // 初始化结果
while(r < sb.length() && l < sb.length()){
if (sb.charAt(l) == sb.charAt(r)){
sb.deleteCharAt(l);
sb.deleteCharAt(l);
// 如果此时左指针大于0,应该将左右指针同时回退一位
if (l > 0){
l--;
r--;
}
}else{
l++;
r++;
}
}
return sb.toString();
}
}
4. 1935. 可以输入的最大单词数*****
- 通过空格分割字符串成为单词数组
- 遍历单词数组中的每个单词的字符
- 一旦发现有字符属于坏掉的键盘键,跳过该字符,否则将结果
count+1
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
words = text.split(" ")
count = 0
for word in words:
flag = True
for char in word:
if char in brokenLetters:
flag = False
break
if flag:
count += 1
return count
边栏推荐
- Reids之缓存预热、雪崩、穿透
- Go redis initialization connection
- Mathematical modeling 2004b question (transmission problem)
- Global and Chinese market of cup masks 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets for small seed seeders 2022-2028: Research Report on technology, participants, trends, market size and share
- Redis之cluster集群
- 数据建模有哪些模型
- 【深度学习】语义分割:论文阅读(NeurIPS 2021)MaskFormer: per-pixel classification is not all you need
- [oc]- < getting started with UI> -- common controls uibutton
- Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
猜你喜欢

In order to get an offer, "I believe that hard work will make great achievements

An article takes you to understand the working principle of selenium in detail
![[deep learning] semantic segmentation - source code summary](/img/2c/50eaef4a11fe2ee9c53a5cebdd69ce.png)
[deep learning] semantic segmentation - source code summary
![[three storage methods of graph] just use adjacency matrix to go out](/img/79/337ee452d12ad477e6b7cb6b359027.png)
[three storage methods of graph] just use adjacency matrix to go out

【深度学习】语义分割:论文阅读(NeurIPS 2021)MaskFormer: per-pixel classification is not all you need

The carousel component of ant design calls prev and next methods in TS (typescript) environment

Blue Bridge Cup_ Single chip microcomputer_ PWM output

Redis之五大基础数据结构深入、应用场景

Redis之发布订阅

Master slave replication of redis
随机推荐
[Yu Yue education] Wuhan University of science and technology securities investment reference
go-redis之初始化連接
go-redis之初始化连接
What is an R-value reference and what is the difference between it and an l-value?
Global and Chinese markets of SERS substrates 2022-2028: Research Report on technology, participants, trends, market size and share
Mapreduce实例(八):Map端join
Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Redis之性能指标、监控方式
IDS' deletion policy
Lua script of redis
MapReduce工作机制
[shell script] - archive file script
为什么要数据分层
Servlet learning diary 7 -- servlet forwarding and redirection
Connexion d'initialisation pour go redis
What is MySQL? What is the learning path of MySQL
Leetcode:608 树节点
【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
软件负载均衡和硬件负载均衡的选择
[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction