存档

Plus One

1:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int takeOver = 1;
        int n = digits.size();
        for(int i = n - 1; i >= 0; --i){
            digits[i] += takeOver;
            if(digits[i] > 9 && i != 0){
                digits[i] -= 10;
                takeOver = 1;
            }
            else if(digits[i] <= 9){
                return digits;
            }
            else{
                digits[i] -= 10;
                digits.insert(digits.begin(), 1);
            }
        }
        return digits;
    }
};


2:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int n = digits.size();
        for (int i = n - 1; i >= 0; --i){
            if(digits[i] == 9){
                digits[i] = 0;
            }
            else{
                digits[i]++;
                return digits;
            }
        }
        digits[0] = 1;
        digits.push_back(0);
        return digits;
    }
};






Remove Duplicates from Sorted Array

1:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int size = nums.size();
        if(size == 0)            
            return 0;
        int index = 0;
        for (int i = 1; i < size; ++i){
            if (nums[index] != nums[i])
                nums[++index] = nums[i];
        }
        return index + 1;
    }
};

2:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.size() < 1)
            return nums.size();
        vector<int>::iterator iter1, iter2;
        for(iter1 = nums.begin(), iter2 = nums.begin() + 1; iter2 != nums.end();){
            if(*iter1 == *iter2)
                iter2 = nums.erase(iter2);
                else{
                    iter1++;
                    iter2++;
                }
        }
        return nums.size();
    }
};


3:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
      int n = nums.size();
      if(n <= 1)
        return n;
      int count = 0;
      for(int i = 1; i < n; ++i){
          if(nums[i] == nums[i - 1])
            count++;
          else
            nums[i - count] = nums[i];
      }
      return n - count;
    }
};


Remove Duplicates from Sorted Array II

1:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int n = nums.size();
        if(n <= 2)
            return n;
        vector<int>::iterator iter1, iter2, iter3;
        for(iter1 = nums.begin(), iter2 = iter1 + 1, iter3 = iter1 + 2; iter1 != nums.end() && iter2 != nums.end() && iter3 != nums.end();){
            if(*iter1 != *iter2){
                ++iter1;
                ++iter2;
                ++iter3;
            }
            else if(*iter1 == *iter2 && *iter2 != *iter3){
                iter1 = iter3;
                iter2 = iter1 + 1;
                iter3 = iter2 + 1;
            }
            else if(*iter1 == *iter2 && *iter2 == *iter3){
                vector<int>::iterator iter;
                for(iter = iter3; iter != nums.end(); ++iter){
                    if(*iter != *iter1){
                        break;
                    }
                }
                iter1 = nums.erase(iter3, iter);
                iter2 = iter1 + 1;
                iter3 = iter2 + 1;
            }
        }
        return nums.size();
    }
};

2:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int n = nums.size();
        if(n <= 2)
            return n;
        int i = 1, j = 1;
        int count = 1;
        while(j < n){
            if(nums[j] != nums[j - 1]){
                count = 1;
                nums[i++] = nums[j];
            }
            else{
                if(count < 2){
                    nums[i++] = nums[j];
                    count++;
                }
            }
            ++j;
        }
        return i;
    }
};


Rotate Image

1:

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for(int i = 0; i < n / 2; ++i){
            for(int j = 0; j < n; ++j){
                swap(matrix[i][j], matrix[n - 1 - i][j]);
            }
        }
        
        for(int i = 0; i < n; ++i){
            for(int j = i + 1; j < n; ++j){
                swap(matrix[i][j], matrix[j][i]);
            }
        }
    }
};

2:

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        reverse(matrix.begin(), matrix.end());
        for(int i = 0; i < matrix.size(); ++i){
            for(int j = 0; j < i; ++j){
                swap(matrix[i][j], matrix[j][i]);
            }
        }
    }
};


anticlockwise_rotate:

class Solution {
public:
    void anti_rotate(vector<vector<int>>& matrix) {
       for(auto vi : matrix) 
           reverse(vi.begin(), vi.end());
	   for(int i = 0; i < matrix.size(); ++i){
		   for(int j = 0; j < i; ++j){
		       swap(matrix[i][j], matrix[j][i]);
		   }
	   }
    }
};



Search in Rotated Sorted Array

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();
        int first = 0, last = n - 1;
        while(first <= last){
            int mid = (first + last) / 2;
            if(nums[mid] == target)
                return mid;
            if(nums[first] <= nums[mid]){
                if(nums[first] <= target && nums[mid] > target){
                    last = mid - 1;
                }
                else{
                    first = mid + 1;
                }
            }
            else{
                if(nums[mid] < target && target <= nums[last]){
                    first = mid + 1;
                }
                else{
                    last = mid - 1;
                }
            }
        }
        return -1;
    }
};


two sum

1:

class Solution {
public:
    vector<int> twoSum(vector<int> &nums, int target) {
        vector<int> numsCopy = nums;
        sort(numsCopy.begin(), numsCopy.end());
        int nLength = (int)numsCopy.size();
        int index1 = 0, index2 = nLength - 1;
        while(index1 < index2){
            if(numsCopy[index1] + numsCopy[index2] == target)
                break;
            else if(numsCopy[index1] + numsCopy[index2] > target)
                --index2;
            else
                ++index1;
        }
        int result1, result2;
        for(int i = 0; i < nLength; ++i){
            if(nums[i] == numsCopy[index1]){
                result1 = i + 1;
                break;
            }
        }
        for(int i = nLength - 1; i >= 0; --i){
            if(nums[i] == numsCopy[index2]){
                result2 = i + 1;
                break;
            }
        }
        if(result1 > result2){
            swap(result1, result2);
        }
        vector<int> result{result1, result2};
        return result;
    }
};


2:

class Solution {
public:
    vector<int> twoSum(vector<int> &nums, int target) {
        map<int, int> mapping;
        vector<int> result;
        for(int i = 0; i < nums.size(); ++i){
            mapping[nums[i]] = i;
        }
        for(int i = 0; i < nums.size(); ++i){
            const int gap = target - nums[i];
            if(mapping.find(gap) != mapping.end() && mapping[gap] > i){
                result.push_back(i + 1);
                result.push_back(mapping[gap] + 1);
                break;
            }
        }
        return result;
    }
};


你可能感兴趣的:(存档)