每日一题 双指针算法
leetcode 27 移出元素 https://leetcode.cn/problems/remove-element/
代码 :
cpp :
1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution {public : int removeElement (vector<int >& nums, int val) { int fast = 0 ; int slow = 0 ; for (fast = 0 ; fast < nums.size (); fast++){ if (nums[fast] != val){ nums[slow++] = nums[fast]; } } return slow; } };
python :
1 2 3 4 5 6 7 8 9 class Solution : def removeElement (self, nums: List [int ], val: int ) -> int : slow, fast = 0 , 0 for fast in range (len (nums)): if nums[fast] != val: nums[slow] = nums[fast] slow += 1 return slow
leetcode 26.删除有序数组中的重复项 https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
代码
cpp :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution {public : int removeDuplicates (vector<int >& nums) { int fast; int slow = 0 ; for (fast = 1 ; fast < nums.size (); fast++){ if (nums[fast] != nums[slow]){ slow++; nums[slow] = nums[fast]; } } return slow + 1 ; } };
python :
1 2 3 4 5 6 7 8 class Solution : def removeDuplicates (self, nums: List [int ] ) -> int : slow, fast = 0 , 1 for fast in range (1 , len (nums)): if nums[slow] != nums[fast]: slow += 1 nums[slow] = nums[fast] return slow + 1
leetcode 283.移动零 https://leetcode.cn/problems/move-zeroes/
代码
cpp :
1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution {public : void moveZeroes (vector<int >& nums) { int fast = 0 ; int slow = 0 ; for (fast = 0 ; fast < nums.size (); fast++){ if (nums[fast] != 0 ){ swap (nums[slow], nums[fast]); slow++; } } } };
python :
1 2 3 4 5 6 7 class Solution : def moveZeroes (self, nums: List [int ] ) -> None : slow, fast = 0 , 0 for fast in range (0 , len (nums)): if nums[fast] != 0 : nums[slow], nums[fast] = nums[fast], nums[slow] slow += 1
leetcode 844比较含退格的字符串 https://leetcode.cn/problems/backspace-string-compare/
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Solution {public : string returnstringCount (string str) { int size=str.length (); int slow=0 ; for (int fast=0 ; fast<size; fast++){ if (str[fast]!='#' ){ str[slow++]=str[fast]; } else { if (slow>0 ){ slow--; } } } string str1 (str,0 ,slow) ; return str1; } bool backspaceCompare (string s,string t) { string sCount = returnstringCount (s); string tCount = returnstringCount (t); if (sCount==tCount){ return true ; } return false ; } };
python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution : def return_string_count (self, s: str ) -> str : slow = 0 size = len (s) s = list (s) for fast in range (size): if s[fast] != '#' : s[slow] = s[fast] slow += 1 else : if slow > 0 : slow -= 1 return '' .join(s[:slow]) def backspaceCompare (self, s: str , t: str ) -> bool : return self.return_string_count(s) == self.return_string_count(t)
leetcode 977. 有序数组的平方 https://leetcode.cn/problems/squares-of-a-sorted-array/
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution {public : vector<int > sortedSquares (vector<int >& nums) { int n = nums.size (); vector<int > ans (n) ; int i = 0 , j = n - 1 ; for (int p = n - 1 ; p >= 0 ; p--) { int x = nums[i] * nums[i]; int y = nums[j] * nums[j]; if (x > y) { ans[p] = x; i++; } else { ans[p] = y; j--; } } return ans; } };
python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution : def sortedSquares (self, nums: List [int ] ) -> List [int ]: n = len (nums) ans = [0 ] * n i, j = 0 , n - 1 for p in range (n - 1 , -1 , -1 ): x = nums[i] * nums[i] y = nums[j] * nums[j] if x > y: ans[p] = x i += 1 else : ans[p] = y j -= 1 return ans