# leetcode(35, 36): 转置矩阵、主要元素

## 题目描述

class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& A) {
int row = A.size();
int col = A[0].size();

vector<vector<int>> temp(col, vector<int>(row, 0));
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
temp[j][i] = A[i][j];
}
}
return temp;
}
};



## 题目描述

class Solution {
public:
int majorityElement(vector<int>& nums) {
if(nums.size() == 0) return -1;
if(nums.size() == 1) return nums[0];

unordered_map<int, int> hash;
for(int i = 0; i < nums.size(); i++)
hash[i] = 0;
for(auto num:nums)
hash[num]++;

int total = 0;
int max_time = 0;
int max_num = nums[0];
for(auto h:hash){
total++;
if(h.second > max_time){max_time = h.second; max_num = h.first;}
}

if(max_time == 1 || max_time <= nums.size()/2 ) return -1;
return max_num;
}
};


## 总结：

1，二维vector的初始化方法

• vector<vector > newOne(row, vector(col, 0));

• vector<vector > res;
— res.resize( r) ; //r行

for (int k = 0; k < r; ++k)
— res[k].resize( c ); //每行为c列

10-23 185

12-06 41
12-24 54
10-09 59
08-21 812
05-05 40
05-17 24
07-28
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie