当前位置:网站首页>五月刷题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
边栏推荐
- 英雄联盟轮播图手动轮播
- Publish and subscribe to redis
- 【每日一题】搬运工 (DFS / DP)
- Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
- Kratos ares microservice framework (I)
- How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
- 为拿 Offer,“闭关修炼,相信努力必成大器
- Selenium+pytest automated test framework practice
- CSP student queue
- Global and Chinese markets for small seed seeders 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
[deep learning] semantic segmentation: paper reading: (2021-12) mask2former
Lua script of redis
In order to get an offer, "I believe that hard work will make great achievements
Redis' bitmap
Parameterization of postman
Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction
[Yu Yue education] Wuhan University of science and technology securities investment reference
Redis' performance indicators and monitoring methods
随机推荐
[shell script] - archive file script
Scoped in webrtc_ refptr
Reids之删除策略
Opencv+dlib realizes "matching" glasses for Mona Lisa
Mapreduce实例(五):二次排序
Redis之哨兵模式
Publish and subscribe to redis
Pytest's collection use case rules and running specified use cases
[deep learning] semantic segmentation: paper reading: (2021-12) mask2former
MapReduce工作机制
Mysql database recovery (using mysqlbinlog command)
Minio distributed file storage cluster for full stack development
基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
Global and Chinese market for annunciator panels 2022-2028: Research Report on technology, participants, trends, market size and share
Mathematical modeling 2004b question (transmission problem)
Mapreduce实例(八):Map端join
软件负载均衡和硬件负载均衡的选择
Kratos ares microservice framework (II)
Multivariate cluster analysis
Appears when importing MySQL