File tree 5 files changed +223
-2
lines changed
solution/1000-1099/1072.Flip Columns For Maximum Number of Equal Rows
5 files changed +223
-2
lines changed Original file line number Diff line number Diff line change 55
55
56
56
<!-- 这里可写通用的实现逻辑 -->
57
57
58
+ ** 方法一:哈希表**
59
+
58
60
<!-- tabs:start -->
59
61
60
62
### ** Python3**
61
63
62
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
65
64
66
``` python
65
-
67
+ class Solution :
68
+ def maxEqualRowsAfterFlips (self , matrix : List[List[int ]]) -> int :
69
+ cnt = Counter()
70
+ for row in matrix:
71
+ t = []
72
+ for v in row:
73
+ if row[0 ] == 1 :
74
+ v ^= 1
75
+ t.append(str (v))
76
+ s = ' ' .join(t)
77
+ cnt[s] += 1
78
+ return max (cnt.values())
66
79
```
67
80
68
81
### ** Java**
69
82
70
83
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
84
72
85
``` java
86
+ class Solution {
87
+ public int maxEqualRowsAfterFlips (int [][] matrix ) {
88
+ Map<String , Integer > map = new HashMap<> ();
89
+ for (int [] row : matrix) {
90
+ if (row[0 ] == 1 ) {
91
+ for (int i = 0 ; i < row. length; ++ i) {
92
+ row[i] ^ = 1 ;
93
+ }
94
+ }
95
+ StringBuilder sb = new StringBuilder ();
96
+ for (int x : row) {
97
+ sb. append(x);
98
+ }
99
+ String s = sb. toString();
100
+ map. put(s, map. getOrDefault(s, 0 ) + 1 );
101
+ }
102
+ return map. values(). stream(). max(Integer :: compareTo). get();
103
+ }
104
+ }
105
+ ```
106
+
107
+ ### ** C++**
108
+
109
+ ``` cpp
110
+ class Solution {
111
+ public:
112
+ int maxEqualRowsAfterFlips(vector<vector<int >>& matrix) {
113
+ unordered_map<string, int> cnt;
114
+ int ans = 0;
115
+ for (auto& row : matrix)
116
+ {
117
+ string s = "";
118
+ for (int v : row)
119
+ {
120
+ if (row[ 0] == 1) v ^= 1;
121
+ s += to_string(v);
122
+ }
123
+ ++cnt[ s] ;
124
+ ans = max(ans, cnt[ s] );
125
+ }
126
+ return ans;
127
+ }
128
+ };
129
+ ```
73
130
131
+ ### **Go**
132
+
133
+ ```go
134
+ func maxEqualRowsAfterFlips(matrix [][]int) int {
135
+ ans := 0
136
+ cnt := map[string]int{}
137
+ for _, row := range matrix {
138
+ s := []byte{}
139
+ for _, v := range row {
140
+ if row[0] == 1 {
141
+ v ^= 1
142
+ }
143
+ s = append(s, byte(v+'0'))
144
+ }
145
+ t := string(s)
146
+ cnt[t]++
147
+ ans = max(ans, cnt[t])
148
+ }
149
+ return ans
150
+ }
151
+
152
+ func max(a, b int) int {
153
+ if a > b {
154
+ return a
155
+ }
156
+ return b
157
+ }
74
158
```
75
159
76
160
### ** ...**
Original file line number Diff line number Diff line change 52
52
### ** Python3**
53
53
54
54
``` python
55
-
55
+ class Solution :
56
+ def maxEqualRowsAfterFlips (self , matrix : List[List[int ]]) -> int :
57
+ cnt = Counter()
58
+ for row in matrix:
59
+ t = []
60
+ for v in row:
61
+ if row[0 ] == 1 :
62
+ v ^= 1
63
+ t.append(str (v))
64
+ s = ' ' .join(t)
65
+ cnt[s] += 1
66
+ return max (cnt.values())
56
67
```
57
68
58
69
### ** Java**
59
70
60
71
``` java
72
+ class Solution {
73
+ public int maxEqualRowsAfterFlips (int [][] matrix ) {
74
+ Map<String , Integer > map = new HashMap<> ();
75
+ for (int [] row : matrix) {
76
+ if (row[0 ] == 1 ) {
77
+ for (int i = 0 ; i < row. length; ++ i) {
78
+ row[i] ^ = 1 ;
79
+ }
80
+ }
81
+ StringBuilder sb = new StringBuilder ();
82
+ for (int x : row) {
83
+ sb. append(x);
84
+ }
85
+ String s = sb. toString();
86
+ map. put(s, map. getOrDefault(s, 0 ) + 1 );
87
+ }
88
+ return map. values(). stream(). max(Integer :: compareTo). get();
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### ** C++**
94
+
95
+ ``` cpp
96
+ class Solution {
97
+ public:
98
+ int maxEqualRowsAfterFlips(vector<vector<int >>& matrix) {
99
+ unordered_map<string, int> cnt;
100
+ int ans = 0;
101
+ for (auto& row : matrix)
102
+ {
103
+ string s = "";
104
+ for (int v : row)
105
+ {
106
+ if (row[ 0] == 1) v ^= 1;
107
+ s += to_string(v);
108
+ }
109
+ ++cnt[ s] ;
110
+ ans = max(ans, cnt[ s] );
111
+ }
112
+ return ans;
113
+ }
114
+ };
115
+ ```
61
116
117
+ ### **Go**
118
+
119
+ ```go
120
+ func maxEqualRowsAfterFlips(matrix [][]int) int {
121
+ ans := 0
122
+ cnt := map[string]int{}
123
+ for _, row := range matrix {
124
+ s := []byte{}
125
+ for _, v := range row {
126
+ if row[0] == 1 {
127
+ v ^= 1
128
+ }
129
+ s = append(s, byte(v+'0'))
130
+ }
131
+ t := string(s)
132
+ cnt[t]++
133
+ ans = max(ans, cnt[t])
134
+ }
135
+ return ans
136
+ }
137
+
138
+ func max(a, b int) int {
139
+ if a > b {
140
+ return a
141
+ }
142
+ return b
143
+ }
62
144
```
63
145
64
146
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int maxEqualRowsAfterFlips (vector<vector<int >>& matrix) {
4
+ unordered_map<string, int > cnt;
5
+ int ans = 0 ;
6
+ for (auto & row : matrix)
7
+ {
8
+ string s = " " ;
9
+ for (int v : row)
10
+ {
11
+ if (row[0 ] == 1 ) v ^= 1 ;
12
+ s += to_string (v);
13
+ }
14
+ ++cnt[s];
15
+ ans = max (ans, cnt[s]);
16
+ }
17
+ return ans;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ func maxEqualRowsAfterFlips (matrix [][]int ) int {
2
+ ans := 0
3
+ cnt := map [string ]int {}
4
+ for _ , row := range matrix {
5
+ s := []byte {}
6
+ for _ , v := range row {
7
+ if row [0 ] == 1 {
8
+ v ^= 1
9
+ }
10
+ s = append (s , byte (v + '0' ))
11
+ }
12
+ t := string (s )
13
+ cnt [t ]++
14
+ ans = max (ans , cnt [t ])
15
+ }
16
+ return ans
17
+ }
18
+
19
+ func max (a , b int ) int {
20
+ if a > b {
21
+ return a
22
+ }
23
+ return b
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxEqualRowsAfterFlips (self , matrix : List [List [int ]]) -> int :
3
+ cnt = Counter ()
4
+ for row in matrix :
5
+ t = []
6
+ for v in row :
7
+ if row [0 ] == 1 :
8
+ v ^= 1
9
+ t .append (str (v ))
10
+ s = '' .join (t )
11
+ cnt [s ] += 1
12
+ return max (cnt .values ())
You can’t perform that action at this time.
0 commit comments