当前位置:网站首页>五月刷题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
边栏推荐
- 【深度学习】语义分割:论文阅读:(2021-12)Mask2Former
- Kratos战神微服务框架(一)
- 【图的三大存储方式】只会用邻接矩阵就out了
- Redis之Geospatial
- [deep learning] semantic segmentation - source code summary
- Blue Bridge Cup_ Single chip microcomputer_ PWM output
- Mapreduce实例(七):单表join
- Detailed explanation of cookies and sessions
- Advance Computer Network Review(1)——FatTree
- [daily question] Porter (DFS / DP)
猜你喜欢

【图的三大存储方式】只会用邻接矩阵就out了

Redis' performance indicators and monitoring methods

Mapreduce实例(十):ChainMapReduce

Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)

Mathematical modeling 2004b question (transmission problem)

Redis geospatial

Research and implementation of hospital management inpatient system based on b/s (attached: source code paper SQL file)

Redis之主从复制
![[shell script] - archive file script](/img/50/1bef6576902890dfd5771500414876.png)
[shell script] - archive file script

MapReduce工作机制
随机推荐
【shell脚本】——归档文件脚本
Kratos ares microservice framework (I)
IDS' deletion policy
In order to get an offer, "I believe that hard work will make great achievements
In depth analysis and encapsulation call of requests
有软件负载均衡,也有硬件负载均衡,选择哪个?
CSP student queue
Kratos战神微服务框架(二)
AcWing 2456. Notepad
Redis之性能指标、监控方式
xargs命令的基本用法
Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
Redis cluster
Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share
Kratos ares microservice framework (II)
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
DCDC power ripple test
软件负载均衡和硬件负载均衡的选择
IDS cache preheating, avalanche, penetration