当前位置:网站首页>六月刷题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 "";
}
}
边栏推荐
- Redis cluster
- Redis geospatial
- Redis' performance indicators and monitoring methods
- Redis之Geospatial
- Redis core configuration
- Redis' bitmap
- Leetcode problem solving 2.1.1
- Global and Chinese markets of SERS substrates 2022-2028: Research Report on technology, participants, trends, market size and share
- Lua script of redis
- Compilation of libwebsocket
猜你喜欢
随机推荐
xargs命令的基本用法
IDS' deletion policy
MySQL数据库优化的几种方式(笔面试必问)
Selenium+Pytest自动化测试框架实战(下)
Processes of libuv
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
[Chongqing Guangdong education] reference materials for nine lectures on the essence of Marxist Philosophy in Wuhan University
Blue Bridge Cup_ Single chip microcomputer_ PWM output
YARN组织架构
MapReduce工作机制
Seven layer network architecture
有软件负载均衡,也有硬件负载均衡,选择哪个?
Advanced Computer Network Review(4)——Congestion Control of MPTCP
Redis之发布订阅
Improved deep embedded clustering with local structure preservation (Idec)
AcWing 2456. 记事本
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
为拿 Offer,“闭关修炼,相信努力必成大器
Mathematical modeling 2004b question (transmission problem)
Workflow - activiti7 environment setup









