当前位置:网站首页>Leetcode double pointer water problem solution
Leetcode double pointer water problem solution
2022-07-18 09:36:00 【KissKernel】

List of articles
- [392. Judging subsequences ](https://leetcode.cn/problems/is-subsequence/)
- [541. Reverse string II](https://leetcode.cn/problems/reverse-string-ii/)
- [ Interview questions 16.24. Sum of number pairs ](https://leetcode.cn/problems/pairs-with-sum-lcci/)
- [696. Counting binary substrings ](https://leetcode.cn/problems/count-binary-substrings/)
392. Judging subsequences
Ideas : Define two pointers to s and t And then determine s and t Whether the characters are the same , If it's the same then i and j meanwhile ++, If not, let t Skip this character to compare the next character , Finally, if i If it has been matched, it means s yes t The string of .
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. Reverse string II
Ideas : First define a starting point pointer i And a counter count, The starting point plus the counter is less than size Just go ahead , Count , Count to 2k Just before the reversal k Characters , Then enter the final judgment count Count to the end , That is, the last interval is not satisfied 2k When , Judge this value and then k To 2k Between the two is still 0,k Between , Reverse .
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;
}
};
Interview questions 16.24. Sum of number pairs
Ideas : Double pointer : Define a left pointer i And the right pointer j, Sort the array , Then start as i and j The sum of positions is greater than target that j– If it is less than, then i++, Equal to put the logarithm into the answer array .
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. Counting binary substrings
Ideas : Define a i The pointer , Constantly traverse the string s, Then one count Record continuous 1 perhaps 0 Number of occurrences , Then two adjacent 0 and 1 or 1 and 0 The smaller value of the number of occurrences is these two groups 0 and 1 The largest logarithm that can be composed .
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;
}
};
边栏推荐
- Is it safe to open an account and speculate in stocks online at present?
- nacos win10单机启动命令
- 君乐宝要IPO了,资本却笑不出来
- Laravel 异步执行任务
- STM32F030外部中断配置
- 4-Redis架构设计到使用场景-string、list、set、zset、hash使用场景
- Exness: crude oil stopped falling and rebounded. Pay attention to the performance of US terrorist data in the evening
- 模拟卷Leetcode【普通】1823. 找出游戏的获胜者
- Codeforces Round #803 (Div. 2) A. XOR Mixup
- 5-Redis架构设计到使用场景-存储原理-数据类型底层结构
猜你喜欢

Codeforces Round #804 B Almost Ternary Matrix

一键生成VR全景图展示

Leetcode-62-different paths

Codeforces Round #803 (Div. 2) C. 3SUM Closure

Exness: crude oil stopped falling and rebounded. Pay attention to the performance of US terrorist data in the evening

Educational Codeforces Round 131 A - D

Codeforces Round #804 C The Third Problem

Codeforces Round #802 C. Helping the Nature

Scripting rules and variable definitions

论文翻译解读:Translating Embeddings for Modeling Multi-relational Data【TransE】
随机推荐
Codeforces Round #802 A. Optimal Path
internship:移动端源码的分析
Codeforces Round #803 (Div. 2) A. XOR Mixup
[ArcGIS creates a small picture frame of islands and nine segment lines in the South China Sea]
微服务学习
LeetCode-14-最长公共前缀
Codeforces Round #804 A The Third Three Number Problem
Codeforces Round #806 (Div. 4) A - G
liquibase初次使用
一键生成VR全景图展示
【快速下载VsCode】
Timeline and playable cause unity crash problem record
AIRIOT答疑第4期|如何使用數據分析引擎?
包过滤规则
华为交换技术:BGP基础实验
Educational Codeforces Round 131 A - D
腾势全新豪华中大型MPV曝光,安全、舒适一个不落
功能、模块质量和非功能性测试
Build a data-based system and start a new era of oid track
C writes the database connection mode to the configuration file