题目:
代码:
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
vector<int> ans;
vector<vector<int>> temp(200000);
for(int i=0;i<nums.size();i++){
for(int j=0;j<nums[i].size();j++){
temp[i+j].push_back(nums[i][j]);
}
}
for(int i=0;i<200000;i++){
for(int j=temp[i].size()-1;j>=0;j--){
ans.push_back(temp[i][j]);
}
}
return ans;
}
};
/*class Solution {//明显空间要小一些
public:
vector<int> ans[200050];
vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
for(int i = 0; i < nums.size(); i++)
for(int j = 0; j < nums[i].size(); j++)
ans[i+j].push_back(nums[i][j]);
//cout<<"ans["<<i+j<<"] push"<<nums[i][j]<<endl;
vector<int> res;
for(int i = 0; i <= 200000; i++){
if(ans[i].size() == 0) continue;
for(int j = ans[i].size()-1; j >= 0; j--)
res.push_back(ans[i][j]);
}
return res;
}
};*/
总结:处理定长的时候还是用数组占用空间小一些