题目链接
https://leetcode.cn/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
知识点
string的遍历、vector的初始化
解题思路
二重循环,外层循环控制vector中每个位置上的值,内层循环用于遍历string
我的通过代码
class Solution {
public:
vector<int> minOperations(string boxes) {
vector<int> v(boxes.size());
for (int i = 0; i < boxes.size(); ++i) {
for (int j = 0; j < boxes.size(); ++j) {
if (boxes[j] == '1') {
v[i] += abs(i - j);
}
}
}
return v;
}
};