原本是简单题,但是k < 0的情况由于选用的方法不好,浪费太多时间了。代码也有很多冗余
class Solution { public: vector<int> decrypt(vector<int>& code, int k) { int size = code.size(); vector<int> res(size,0); if(k == 0) return res; if(k > 0){ for(int i = 0;i < k;i++) res[0] += code[i+1]; for(int i = 1;i < size - k;i++){ res[i] = res[i-1] - code[i] + code[i+k]; } for(int i = size - k;i < size;i++){ res[i] = res[i-1] - code[i] + code[i-size+k]; } } else{ k = -k; for(int i = size-1;i >= size - k;i--) res[size-1] += code[i-1]; for(int i = size - 2;i >= k;i--){ res[i] = res[i+1] - code[i] + code[i-k]; } for(int i = k-1;i >= 0;i--){ res[i] = res[i+1] - code[i] + code[size-k+i]; } } return res; } };
标签:code,int,res,--,炸弹,1652,leetcode,size From: https://www.cnblogs.com/uacs2024/p/18582669