---
# System prepended metadata

title: 2125. Number of Laser Beams in a Bank
tags: [leetcode]

---

# 2125. Number of Laser Beams in a Bank
## 思路
每次計算到這一列有幾條雷射光
## 演算法
```
1. 計算bank每列有幾個1，放入row中(row[i]代表第i列有row[i]個1)
2. 定義ans = 0, last = 0
3. 掃過row:
    3.1. 若row[i]不為0，ans += last * row[i]; last = row[i]
4. 回傳ans
```

## 程式碼
```cpp=
class Solution {
public:
    int numberOfBeams(vector<string>& bank) {
        int len = bank.size();
        vector <int> row(len);
        for (int i = 0; i < len; i++) {
            int cnt = 0;
            for (int j = 0; j < bank[i].length(); j++) {
                if (bank[i][j] == '1') cnt++;
            }
            row[i] = cnt;
        }
        int ans = 0, last = 0;
        for (int i = 0; i < len; i++) {
            if (row[i]) {
                ans += last * row[i];
                last = row[i];
            }
        }
        return ans;
    }
};
```