雙指標法相關的題
力扣-27. 移除元素
題目連結:https://leetcode-cn.com/problems/remove-element/
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
//雙指標法
int slowIndex = 0;
for(int fastIndex = 0;fastIndex < nums.size();fastIndex++){
if(nums[fastIndex] != val){
nums[slowIndex++] = nums[fastIndex];
}
}
return slowIndex;
}
};
力扣-167.兩數之和 II -輸入有序陣列
題目連結:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int left = 0;
int right = numbers.size()-1;
while(left < right){
int sum = numbers[left] + numbers[right];
if(sum == target)
return {left+1,right+1};
else if(sum > target)
right--;
else
left++;
}
return {};
}
};
力扣-977.有序陣列的平方
題目連結:https://leetcode-cn.com/problems/squares-of-a-sorted-array/
class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
int n = A.size();
int left = 0;
int right = n-1;
int idex = n-1;
vector<int> result;
result.resize(n);
while(left <= right){
if(A[left] * A[left] > A[right] * A[right]){
result[idex--] = A[left]*A[left];
left++;
}else{
result[idex--] = A[right]*A[right];
right--;
}
}
return result;
}
};
劍指offer-24. 反轉連結串列
題目連結:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
//雙指標法:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr; //雙指標
ListNode* cur = head;
while(cur){
ListNode* next = cur->next; //定義臨時變數儲存下一節點
cur->next = prev;
prev = cur;
cur = next;
}
return prev;
}
};
//遞迴方法:
class Solution {
public:
ListNode* reverse(ListNode* cur,ListNode* pre){
if(cur == nullptr) //終止條件
return pre;
//遞迴函式單層處理函式
ListNode* next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
return reverse(cur,pre); //將pre作為頭結點一層一層往上return
}
ListNode* reverseList(ListNode* head) {
return reverse(head,nullptr);
}
};
力扣-283.移動0
題目連結:https://leetcode-cn.com/problems/move-zeroes/
class Solution {
public:
void moveZeroes(vector<int>& nums) {
if(nums.size() < 2)
return;
int i = 0;
int j = 1; //定義兩個索引
while(i < nums.size() && j < nums.size()){
if(nums[i] == 0){
if(nums[j] != 0){
swap(nums[i],nums[j]);
i++;
}
}else{
i++;
}
j++;
}
}
};
力扣-26.刪除排序陣列中的重複項
題目連結:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size() == 0)
return 0;
int slow = 0;
for(int fast = 0;fast <nums.size()-1;fast++){
if(nums[fast] != nums[fast+1])
nums[slow++] = nums[fast];
}
nums[slow++] = nums[nums.size()-1];
return slow;
}
};
力扣-86.分隔連結串列
題目連結:https://leetcode-cn.com/problems/partition-list/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* h1 = new ListNode(985);
ListNode* h2 = new ListNode(211);
ListNode* t1 = h1;
ListNode* t2 = h2;
while(head){
if(head-> val < x){
t1->next = head;
t1 = t1->next;
}else{
t2->next = head;
t2 = t1->next;
}
head = head->next;
}
t2->next = nullptr;
t1->next = h2->next;
return h1->next;
}
};
力扣-11.盛最多水的容器
題目連結:https://leetcode-cn.com/problems/container-with-most-water/
class Solution {
public:
int maxArea(vector<int>& height) {
int sz = height.size();
if(sz < 2)
return 0;
int maxCount = 0;
int i = 0;
int j = sz-1;
while(i < j){
int sum = min(height[i],height[j])*(j-i);
maxCount = sum > maxCount ? sum : maxCount;
if(height[i] < height[j])
i++;
else
j--;
}
return maxCount;
}
};
力扣-75.顏色分類
題目連結:https://leetcode-cn.com/problems/sort-colors/
class Solution {
public:
void sortColors(vector<int>& nums) {
int i = 0;
int j = nums.size()-1;
int l = 0;
while(i < j && l <= j){
if(nums[l] == 2){
swap(nums[l],nums[j]);
j--;
}else if(nums[l] == 0){
swap(nums[i],nums[l]);
i++;
l++;
}else{
l++;
}
}
}
};
力扣-42. 接雨水
題目連結:https://leetcode-cn.com/problems/trapping-rain-water/
class Solution {
public:
int trap(vector<int>& height) {
int left = 0;
int right = height.size()-1;
int left_max = 0;
int right_max = 0;
int ans = 0;
while(left <right){
if(height[left] < height[right]){
if(height[left] >= left_max){
left_max = height[left];
}else{
ans += left_max - height[left];
}
left++;
}else{
if(height[right] >= right_max){
right_max = height[right];
}else{
ans += right_max - height[right];
}
right--;
}
}
return ans;
}
};
相關文章
- 雙指標習題:Kalindrome Array指標
- 6.12.雙指標專題指標
- 雙指標相關演算法指標演算法
- 雙指標指標
- LeetCode解題記錄(雙指標專題)LeetCode指標
- 雙指標法指標
- 有關this指標指向問題指標
- 方法相關
- 雙指標演算法的一個簡單題解指標演算法
- 關於二維陣列指標的問題陣列指標
- 演算法-雙指標演算法指標
- 關於指標指標
- 指標相關指標
- leetcode 題解:python3@ 官方題解_暴力法_雙指標法LeetCodePython指標
- 雙指標維護筆記指標筆記
- 指標-小泉的難題指標
- 演算法相關演算法
- 力扣之迴文數(雙指標中的對撞指標公式模板)力扣指標公式
- 關於C++當中的指標懸空問題C++指標
- leetcode 11 題解:python3@ 官方題解_暴力法_雙指標法LeetCodePython指標
- hot100-一刷-02雙指標(共4道題)指標
- 靈茶山艾府-相向雙指標指標
- 滑動視窗與雙指標指標
- 「LeetCode Top100」之雙指標LeetCode指標
- 雙指標妙解三數之和指標
- 雙指標查詢陣列的連續規律子陣列問題指標陣列
- 圖解兩數之和:雙指標法圖解指標
- 關於函式指標函式指標
- 有關指標的那些事兒《一》指標
- 佛說法相,即非法相,是名法相
- 關於Swift中的指標的那些事Swift指標
- 詳解c++指標的指標和指標的引用C++指標
- 10,函式和方法相關的東西函式
- LeetCode -844. 比較含退格的字串(棧、雙指標)LeetCode字串指標
- 關於引用(python中的偽指標)的理解Python指標
- 0x03 Leetcode Hot100 雙指標LeetCode指標
- 常見演算法技巧之——雙指標思想演算法指標
- 函式指標練習題函式指標