当前位置:网站首页>五月刷题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
边栏推荐
- The five basic data structures of redis are in-depth and application scenarios
- Global and Chinese market of electronic tubes 2022-2028: Research Report on technology, participants, trends, market size and share
- 数据建模有哪些模型
- Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share
- Advanced Computer Network Review(4)——Congestion Control of MPTCP
- 软件负载均衡和硬件负载均衡的选择
- Reids之缓存预热、雪崩、穿透
- IDS' deletion policy
- [daily question] Porter (DFS / DP)
- Pytest parameterization some tips you don't know / pytest you don't know
猜你喜欢
![[deep learning] semantic segmentation - source code summary](/img/2c/50eaef4a11fe2ee9c53a5cebdd69ce.png)
[deep learning] semantic segmentation - source code summary

IDS cache preheating, avalanche, penetration

Use of activiti7 workflow

为拿 Offer,“闭关修炼,相信努力必成大器

Redis之持久化实操(Linux版)

Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"

How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)

基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)

Kratos ares microservice framework (II)

Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
随机推荐
Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share
Hard core! One configuration center for 8 classes!
What is an R-value reference and what is the difference between it and an l-value?
【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
In order to get an offer, "I believe that hard work will make great achievements
[deep learning] semantic segmentation - source code summary
AcWing 2456. Notepad
Heap (priority queue) topic
[Yu Yue education] Wuhan University of science and technology securities investment reference
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
Redis分布式锁实现Redisson 15问
Redis connection redis service command
AcWing 2456. 记事本
The five basic data structures of redis are in-depth and application scenarios
Mysql database recovery (using mysqlbinlog command)
Redis' bitmap
Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
Meituan Er Mian: why does redis have sentinels?
为拿 Offer,“闭关修炼,相信努力必成大器
Opencv+dlib realizes "matching" glasses for Mona Lisa