Loading

LeetCode 554. 砖墙

 

思路

方法:哈希表

 1 class Solution {
 2 public:
 3     int leastBricks(vector<vector<int>>& wall) {
 4         unordered_map<int, int> mp;
 5         for(int i = 0; i < wall.size(); ++i) {
 6             int sum = 0;
 7             for(int j = 0; j < wall[i].size()-1; ++j) {
 8                 sum += wall[i][j];
 9                 if(mp.count(sum) == 0) {
10                     mp[sum] = 1;
11                 } else {
12                     mp[sum]++;
13                 }
14             }
15         }
16 
17         int height = wall.size();
18         int res = height;
19         for(auto iter = mp.begin(); iter != mp.end(); ++iter) {
20             if(height - iter->second < res) {
21                 res = height - iter->second;
22             }
23         }
24 
25         return res;
26     }
27 };

 

原文 : LeetCode官方题解 - 砖墙

posted @ 2021-02-27 18:32  拾月凄辰  阅读(58)  评论(0编辑  收藏  举报