Leetcode question set 13 (remove elements)

Keywords: leetcode

Removing elements from an array in O(N) time usually uses a two fingered needle method.

27. Remove element 

Give you an array nums   And a value Val, you need to remove all values in place equal to   val   And returns the new length of the array after removal.

Instead of using extra array space, you must use only O(1) extra space and modify the input array in place.

The order of elements can be changed. You don't need to consider the elements in the array that exceed the new length.

Input: num = [3,2,2,3], Val = 3
Output: 2, Num = [2,2]
Explanation: the function should return a new length of 2, and the first two elements in num are 2. You don't need to consider the elements in the array that exceed the new length. For example, if the new length returned by the function is 2, and num = [2,2,3,3] or num = [2,2,0,0], it will also be regarded as the correct answer.

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int i = 0, j = 0;
        while(i<nums.size()){
            if (nums[i]==val) {
                ++i;
                continue;
            }else{
                nums[j++] = nums[i++];
            }
        }
        return j;
    }
};

 283. Move zero

Given an array num, write a function to move all zeros to the end of the array while maintaining the relative order of non-zero elements.

Example:

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int i = 0, j = 0;
        while( i < nums.size()){
            if (nums[i]==0) {++i; continue;}
            else {nums[j++] = nums[i++];}
        }
        while(j<i) nums[j++] = 0;
    }
};

 844. Compare strings with backspace

Given the two strings s and t, when they are input into the blank text editor respectively, please judge whether they are equal# Represents a backspace character.

If equal, return true; Otherwise, false is returned.

Note: if you enter a backspace character for empty text, the text continues to be empty.

Example 1:

Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: both S and T will become "ac".

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        int i = s.length()-1, j = t.length()-1, skipi = 0, skipj = 0;
        while(i >= 0 && j >= 0){
            while(i>=0){
                if (s[i]=='#') {++skipi; --i;}
                else if (skipi) {--skipi; --i;}
                else break;
            }
            while(j>=0){
                if (t[j]=='#') {++skipj; --j;}
                else if (skipj) {--skipj; --j;}
                else break;
            }
            if (i >= 0 && j >= 0) {
                if (s[i]==t[j]){
                    --i; --j;
                }else {
                    return false;
                }
            }
        }
        while(i>=0){
            if (s[i]=='#') {++skipi; --i;}
            else if (skipi) {--skipi; --i;}
            else break;
        }
        while(j>=0){
            if (t[j]=='#') {++skipj; --j;}
            else if (skipj) {--skipj; --j;}
            else break;
        }
        return i<0&&j<0;
    }
};

 977. Square of ordered array

Give you an integer array nums sorted in non decreasing order, and return a new array composed of the square of each number. It is also required to sort in non decreasing order.

Example 1:

Input: num = [- 4, - 1,0,3,10]
Output: [0,1,9,16100]
Explanation: after squaring, the array becomes [16,1,0,9100]
After sorting, the array becomes [0,1,9,16100]  

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int left = 0, right = 0, len = nums.size();
        vector<int> ans;
        while(right<len&&nums[right]<0) ++right;
        if (right == 0){
            for (left = 0; left < len ; ++left){
                ans.push_back(nums[left]*nums[left]);
            }
        }else if (right == len){
            for (left = right - 1; left >= 0; --left){
                ans.push_back(nums[left]*nums[left]);
            }
        }else{
            left = right - 1;
            while(left >= 0 && right < len){
                if (nums[left]*nums[left] < nums[right]*nums[right]){
                    ans.push_back(nums[left]*nums[left]);
                    --left;
                }else{
                    ans.push_back(nums[right]*nums[right]);
                    ++right;
                }
            }
            while(left >= 0){
                ans.push_back(nums[left]*nums[left]);
                --left;
            }
            while(right < len){
                ans.push_back(nums[right]*nums[right]);
                ++right;
            }
        }
        return ans;
    }
};

Posted by Izzy1979 on Mon, 08 Nov 2021 23:07:23 -0800