时间复杂度o(n)
空间复杂度o(1)
- vector<int>a/string a; reverse(a.begin(),a.end());
- vector<vector<int>>a
上下翻转(翻转行):
cpp
vector<vector<int>> a = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
reverse(a.begin(), a.end());
// 结果:
// {7, 8, 9}
// {4, 5, 6}
// {1, 2, 3}
左右翻转(翻转每行的列):
cpp
for (auto& row : a) {
reverse(row.begin(), row.end());
}
// 结果:
// {3, 2, 1}
// {6, 5, 4}
// {9, 8, 7}