class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int slowIndex{},fastIndex{};
int n = nums.size();
int count{};
for(;fastIndex < n;++fastIndex){
if(nums[fastIndex] != val){
count++;
swap(nums[slowIndex++],nums[fastIndex]);
}
}
return count;
}
};
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int slowIndex{},fastIndex{1},count{};
for(;fastIndex < nums.size();++fastIndex){
if(nums[fastIndex] != nums[slowIndex]){
nums[slowIndex+1] = nums[fastIndex];
slowIndex++;
}
}
return slowIndex+1;
}
};
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int slowIndex{},fastIndex{};
int n = nums.size();
for(;fastIndex < n;++fastIndex){
if(nums[fastIndex]){
swap(nums[slowIndex++],nums[fastIndex]);
}
}
}
};
class Solution {
public:
bool backspaceCompare(string s, string t) {
int nS = s.size();
int nT = t.size();
int indexS = nS -1;
int indexT = nT -1;
while(indexS >= 0 || indexT >= 0){
int countS{};
int countT{};
while(indexS >= 0){
if(s[indexS] == '#'){
countS++;
indexS--;
}else if(countS > 0){
indexS--;
countS--;
}else{
break;
}
}
while(indexT >= 0){
if(t[indexT] == '#'){
countT++;
indexT--;
}else if(countT > 0){
indexT--;
countT--;
}else{
break;
}
}
if(indexS >= 0 && indexT >=0){
if(s[indexS] != t[indexT]) return false;
}
else if(indexS >= 0 || indexT >=0) return false;
--indexS;
--indexT;
}
return true;
}
};
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int n = nums.size();
int slowIndex{};
int fastIndex = n-1;
vector<int> ans(n);
int index = n-1;
while(slowIndex <= fastIndex){
if(abs(nums[slowIndex]) >= abs(nums[fastIndex])){
ans[index--] = nums[slowIndex]*nums[slowIndex];
slowIndex++;
}else{
ans[index--] = nums[fastIndex]*nums[fastIndex];
fastIndex--;
}
}
return ans;
}
};
class Solution {
public:
void reverseString(vector<char>& s) {
int n = s.size();
for(int i{},j = n-1;i<n/2;i++,j--){
swap(s[i],s[j]);
}
}
};
class Solution {
public:
string replaceSpace(string s) {
int spaceCount{};
for(char c : s){
if(c == ' ') spaceCount++;
}
int n = s.size();
s.resize(n+spaceCount*2);
for(int i = s.size() -1,j = n-1;i>=0;--i){
if(s[j] != ' '){
s[i] = s[j--];
continue;
}else{
s[i--] = '0';
s[i--] = '2';
s[i] = '%';
j--;
}
}
return s;
}
};
class Solution {
public:
void emptySpace(string &s){
int n = s.size();
int slowIndex{},fastIndex = 0;
while(fastIndex < n && s[fastIndex] == ' ') fastIndex++;
for(;fastIndex < n;++fastIndex){
if(fastIndex-1 > 0 && s[fastIndex] == s[fastIndex-1] && s[fastIndex] == ' '){
continue;
}else{
s[slowIndex++] = s[fastIndex];
}
}
if(slowIndex - 1 >0 && s[slowIndex-1] == ' '){
s.resize(slowIndex-1);
}else{
s.resize(slowIndex);
}
}
void reverse(string &s,int start,int end){
for(int i = start,j = end; i<j;++i,--j){
swap(s[i],s[j]);
}
}
string reverseWords(string s) {
emptySpace(s);
reverse(s,0,s.size()-1);
for(int i{};i<s.size();++i){
int j = i;
while(j < s.size() && s[j] != ' ') j++;
reverse(s,i,j-1);
i = j;
}
return s;
}
};
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur == nullptr) return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse(cur,temp);
}
ListNode* reverseList(ListNode* head) {
return reverse(nullptr,head);
}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummpyNode = new ListNode(0);
dummpyNode->next = head;
ListNode* slowNode = dummpyNode;
ListNode* fastNode = dummpyNode;
while(n-- && fastNode != nullptr) fastNode = fastNode->next;
fastNode = fastNode->next;
while(fastNode != nullptr){
slowNode = slowNode->next;
fastNode = fastNode->next;
}
slowNode->next = slowNode->next->next;
return dummpyNode->next;
}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA{},lenB{};
ListNode* curA = headA;
ListNode* curB = headB;
while(curA!=nullptr){
lenA++;
curA = curA->next;
}
while(curB != nullptr){
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
if(lenB > lenA){
swap(lenA,lenB);
swap(curA,curB);
}
int delta = lenA - lenB;
while(delta--){
curA = curA->next;
}
while(curA!=nullptr){
if(curB == curA) return curB;
curB = curB->next;
curA = curA->next;
}
return {};
}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(!head) return{};
ListNode* slow = head;
ListNode* fast = head;
while(fast->next!=nullptr && fast->next->next!=nullptr){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
slow = head;
while(slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
}
return {};
}
};
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n =nums.size();
int left{},right{};
vector<vector<int>> ans;
sort(nums.begin(),nums.end());
for(int i{};i<n;++i){
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
left = i+1;
right = n-1;
while(left < right){
int sumNum = nums[i] + nums[left] + nums[right];
if(sumNum > 0){
right--;
}else if(sumNum < 0){
left++;
}else{
ans.push_back({nums[i],nums[left],nums[right]});
while(left < right && nums[right] == nums[right-1]) right--;
while(left < right && nums[left] == nums[left+1]) left++;
right--;
left++;
}
}
}
return ans;
}
};
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
int left{},right{};
vector<vector<int>> ans;
sort(nums.begin(),nums.end());
for(int i{};i<n;++i){
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
for(int j = i+1;j<n;++j){
if(j > i+1 && nums[j] == nums[j-1]){
continue;
}
left = j+1;
right = n-1;
while(left <right){
if(nums[i] + nums[j] > target - (nums[left] + nums[right])){
right--;
}else if(nums[i] + nums[j] < target - (nums[left] + nums[right])){
left++;
}else{
ans.push_back({nums[i], nums[j], nums[left] ,nums[right]});
while(left < right && nums[right] == nums[right-1]) right--;
while(left < right && nums[left] == nums[left+1]) left++;
left++;
right--;
}
}
}
}
return ans;
}
};