2574.cpp (402B)
1 class Solution { 2 public: 3 vector<int> leftRightDifference(const vector<int> &nums) const { 4 const int sum = accumulate(begin(nums), end(nums), 0); 5 const int n = size(nums); 6 vector<int> res(n); 7 int acc = 0; 8 9 for (int i = 0; i < n; i++) { 10 res[i] = abs(sum - acc - nums[i] - acc); 11 acc += nums[i]; 12 } 13 14 return res; 15 } 16 };