[算法与数据结构][array operations][leetcode1299:easy]Replace Elements with Greatest Element on Right Side

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.

After doing so, return the array.

class Solution {
public:
    vector<int> replaceElements(vector<int>& arr) {
    	assert(arr.size() > 0);
        vector<int> ret(arr.size(), -1);
        ret[arr.size()-1] = -1;
        for(int i=arr.size()-2; i >= 0; i-- ){
            ret[i] = ret[i+1] > arr[i+1] ? ret[i+1] : arr[i+1];
        }
        return ret;
    }
};

你可能感兴趣的:(算法和数据结构)