当前位置:网站首页>六月刷题02——字符串
六月刷题02——字符串
2022-07-06 09:02:00 【追逐梦想的阿光】
六月刷题02——字符串
今日刷题内容: 字符串
前言
- 更新每天刷题的题解内容
- 注重个人理解,看难度更新题目数量
- 题目来源于力扣
- 争取每日都能做出至少一题
- 语言java、python、c\c++
一、今日题目
二、解题思路
1. 2278. 字母在字符串中的百分比
- 一次遍历得到该字母出现的次数
- 用次数
*100除于字符串长度n即可
class Solution {
public int percentageLetter(String s, char letter) {
int[] hash = new int[256];
int n = s.length();
for (char c: s.toCharArray()){
hash[c]++;
}
return (int)(hash[letter] * 100 / n);
}
}
2. 551. 学生出勤记录 I
- 滑动窗口,用左右指针
- 右指针用来标记遇到
L的长度,左指针自增- 如果字符
A次数小于2并且L没有连续出现三次则为真
class Solution {
public boolean checkRecord(String s) {
int[] hash = new int[256];
boolean flag = true;
int count = 0;
int l = 0, r = -1, n = s.length();
char[] arr = s.toCharArray();
for (char c: arr){
hash[c]++;
}
while(l < n){
r = l;
count = 0;
while (r < n && arr[r] == 'L'){
count++;
r++;
if (count == 3) {
flag = false;
break;
}
}
l++;
}
if(hash['A'] < 2 && flag){
return true;
}
return false;
}
}
3. 2255. 统计是给定字符串前缀的字符串数目
- 普通模拟,遍历每个单词,找出满足条件的单词
- 统计次数即可
class Solution {
public int countPrefixes(String[] words, String s) {
char[] arr = s.toCharArray();
int i, count = 0;
boolean flag;
for (String word: words){
i = 0;
flag = true;
for (char c: word.toCharArray()){
if (i < arr.length && arr[i] == c) i++;
else{
flag = false;
break;
}
}
if (flag) count++;
}
return count;
}
}
4. 1071. 字符串的最大公因子
- 做的时候只想到了暴力
本题还有多种解法参照题解
class Solution {
public String gcdOfStrings(String str1, String str2) {
if (str1.length() > str2.length()){
return gcdOfStrings(str2, str1); // 使第一个字符串为长度更小的
}
StringBuffer sb = new StringBuffer();
String ret;
int i;
if (str1.length() == 0) return "";
if (str2.contains(str1)){
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
for (i = 0; i < arr1.length; i++){
if (arr1[i] == arr2[i]){
sb.append(arr1[i]);
}
else{
break;
}
}
int len = sb.length();
if (len == 0) return "";
for (i = len; i < arr2.length; i++){
if (arr2[i] != sb.charAt(i % len)){
return "";
}
}
ret = sb.toString();
while (arr2.length % ret.length() != 0 || arr1.length % ret.length() != 0){
ret = sb.substring(0, len-1);
len -= 1;
}
return ret;
}
return "";
}
}
边栏推荐
- Compilation of libwebsocket
- Selenium+Pytest自动化测试框架实战(下)
- Redis cluster
- The five basic data structures of redis are in-depth and application scenarios
- 面渣逆袭:Redis连环五十二问,图文详解,这下面试稳了
- Lua script of redis
- O & M, let go of monitoring - let go of yourself
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- AcWing 2456. Notepad
- [shell script] use menu commands to build scripts for creating folders in the cluster
猜你喜欢
![[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

基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)

Redis' performance indicators and monitoring methods

Withdrawal of wechat applet (enterprise payment to change)

Mapreduce实例(四):自然排序

Pytest parameterization some tips you don't know / pytest you don't know

Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555

QML type: locale, date

Design and implementation of online shopping system based on Web (attached: source code paper SQL file)

Mapreduce实例(八):Map端join
随机推荐
Use of activiti7 workflow
Kratos ares microservice framework (III)
Global and Chinese market of electric pruners 2022-2028: Research Report on technology, participants, trends, market size and share
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
为拿 Offer,“闭关修炼,相信努力必成大器
IDS cache preheating, avalanche, penetration
Advanced Computer Network Review(3)——BBR
Redis之哨兵模式
Selenium+Pytest自动化测试框架实战
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
018.有效的回文
五层网络体系结构
【图的三大存储方式】只会用邻接矩阵就out了
[Yu Yue education] reference materials of power electronics technology of Jiangxi University of science and technology
Persistence practice of redis (Linux version)
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
Mapreduce实例(八):Map端join
AcWing 2456. 记事本
In depth analysis and encapsulation call of requests