当前位置:网站首页>leetcode双指针水题题解
leetcode双指针水题题解
2022-07-15 19:49:00 【KissKernel】

文章目录
392. 判断子序列
思路:定义两个指针分别指向s和t然后判断s和t的字符是否相同,如果相同那么i和j同时++,如果不相同则让t跳过该字符去比较下一个字符,最后如果i已经匹配完了那么就说明s是t的子串。
class Solution {
public:
bool isSubsequence(string s, string t) {
int i = 0,j = 0;
while(i<s.size() && j<t.size())
{
if(s[i]==t[j])
{
i++;
j++;
}
else
{
j++;
}
}
return i==s.size();
}
};
541. 反转字符串 II
思路:先定义一个起点指针i和一个计数器count,起点加上计数器小于size就继续,计数,计数到2k就反转前k个字符,然后进入最后判断count计数到最后,也就是最后一个区间不满足2k的时候,判断这个值再k到2k之间还是0,k之间,进行反转。
class Solution {
public:
string reverseStr(string s, int k) {
int count = 0;
int i = 0;
while(i+count<s.size())
{
count++;
if(count==2*k)
{
reverse(&s[i],&s[i+k]);
i += count;
count = 0;
}
}
if(count>=k && count<2*k)
{
reverse(&s[i],&s[i+k]);
}
else if(count>0 && count<k)
{
reverse(&s[i],&s[i+count]);
}
return s;
}
};
面试题 16.24. 数对和
思路:双指针:定义一个左指针i和右指针j,将数组排序,然后开始如i和j位置之和大于target那么j–若是小于则i++,等于就将这对数放进答案数组。
class Solution {
public:
vector<vector<int>> pairSums(vector<int>& nums, int target) {
vector<vector<int>> ans;
int i = 0,j = nums.size()-1;
sort(nums.begin(),nums.end());
while(i<j)
{
int n = nums[i]+nums[j];
if(n>target)
{
j--;
}
else if(n<target)
{
i++;
}
else
{
ans.push_back({nums[i],nums[j]});
i++;j--;
}
}
return ans;
}
};
696. 计数二进制子串
思路:定义一个i指针,不断遍历字符串s,然后一个count记录连续的1或者0出现的个数,然后两个相邻的0和1或 1和0出现次数的较小值就是这两组0和1能组成的最大对数。
class Solution {
public:
int countBinarySubstrings(string s) {
int num1 = 0,i = 0;
int ans = 0,count = 0;
while(i<s.size())
{
count = 0;
char c = s[i];
while(i<s.size() && c == s[i])
{
count++;
i++;
}
ans+=min(num1,count);
num1 = count;
}
return ans;
}
};
边栏推荐
- The correlation between BTC and technology stocks weakened, and market uncertainty increased? 53% of investors will turn to alternative assets
- 5-Redis架构设计到使用场景-存储原理-数据类型底层结构
- DMS如何赋权数据库函数权限呢?
- LeetCode-240-搜索二维矩阵Ⅱ
- LeetCode-128-最长连续序列
- datax扩展vertica插件
- Codeforces Round #805 A - G
- 国信证券手机开户安全吗?开户如何办理
- 脚本编写规则和变量定义
- 网络信息查看及配置
猜你喜欢

设计稳定的微服务系统时不得不考虑的场景

Codeforces Round #806 (Div. 4) A - G

Codeforces Round #803 (Div. 2) D. Fixed Point Guessing

BTC与科技股相关性减弱 市场不确定性增强?53%投资者将转向另类资产
![In a real case, college students were cheated when taking orders. I hope you won't be cheated [painful lesson]](/img/7d/142e69d6da1899cefbcc99d7929fba.png)
In a real case, college students were cheated when taking orders. I hope you won't be cheated [painful lesson]

Airiot Q & A issue 4 | how to use data analysis engine?

LeetCode-62-不同路径

答读者问(6):单细胞TPM矩阵如何分析?

AIRIOT答疑第4期|如何使用數據分析引擎?

Codeforces Round #802 B. Palindromic Numbers
随机推荐
【MT2126】 数字游戏
Win11提示Outlook搜索错误怎么办?Win11提示Outlook搜索错误
[mt2126] Digital Games
[open source trusted privacy computing framework "argot"] ant announced the official open source for global developers
idea 新建项目时克隆仓库代码
Codeforces Round #803 (Div. 2) D. Fixed Point Guessing
迪赛智慧数——折(渐变堆叠图):全国居民人均可支配收入
海量遥感数据处理与GEE云计算技术实践应用
Legal regulation and Enlightenment of face recognition technology in the United States
liquibase初次使用
【MT2109】矩形
SQL也能玩转AI ?没错!MLOps Meetup V3 回顾|OpenMLBD+SQLFlow+Byzer
Codeforces Round #804 C The Third Problem
一键生成VR全景图展示
请教一个问题, FLinkCDC同步mysql的数据,必须要root权限吗?
Can SQL also play AI? you 're right! Mlops meetup V3 review openmlbd+sqlflow+byzer
Cloud document management software docuware cloud how to solve five it problems
浅析电子签章应用安全与技术
Codeforces Round #802 A. Optimal Path
internship:移动端源码的分析