# 832. Flip image

https://leetcode-cn.com/problems/flipping-an-image/

Given a binary matrix A, we want to flip the image horizontally first, then reverse the image and return the result.
Flip the picture horizontally is to flip each line of the picture, that is, in reverse order. For example, the result of flipping [1, 1, 0] horizontally is [0, 1, 1].
Reversing the picture means that all 0's in the picture are replaced by 1's, and all 1's are replaced by 0's. For example, the result of reversing [0, 1, 1] is [1, 0, 0].

Tips:
1 <= A.length = A.length <= 20
0 <= A[i][j] <= 1

According to the title, we can know how to play horizontal flip and reverse..

First, create a temporary variable to hold the results

Horizontal flip is to exchange the number with index 0, the penultimate number and the number with index 1 with the penultimate number... While reverse is to change 0 into 1 and 1 into 0, so we can directly reverse while horizontal flip

Reverse transfer the current number directly minus 1, and then ✖️ - 1 can change 0 to 1 and 1 to 0

```class Solution {
public int[][] flipAndInvertImage(int[][] image) {
int[][] temp = new int[image.length][image.length];
for (int i = 0; i < image.length; i++) {
for (int i1 = 0; i1 < image[i].length; i1++) {
temp[i][i1] = (image[i][image[i].length - 1 - i1] - 1) * -1;
}
}
return temp;
}
}
``` # 1582. Special positions in binary matrix

https://leetcode-cn.com/problems/special-positions-in-a-binary-matrix/

Give you a matrix mat with the size of rows x cols, where mat[i][j] is 0 or 1. Please return the number of special positions in the matrix mat.
Special position definition: if mat[i][j] == 1 and all other elements in row I and column j are 0 (the subscripts of rows and columns start from 0), the position (i, j) is called a special position.

In this problem, I use the map structure to store rows and columns with only one 1 in each row, and then find out whether there is only one row with 1 in the front row and whether it is the row and column stored in the current map. If there are more than 1 in the current column, delete the current key and value, and the rest is the position that meets the result, It's just that this method takes a little more running time and memory

```class Solution {
public int numSpecial(int[][] mat) {
int res = 0;
Map<Integer,Integer> m = new HashMap<>();
for (int i = 0; i < mat.length; i++) {
for (int i1 = 0; i1 < mat[i].length; i1++) {
if(mat[i][i1] == 1){
if(!m.containsKey(i)){
m.put(i,i1);
}else{
m.remove(i);
break;
}
}
}
}

for (Integer row : m.keySet()) {
for (int i = 0; i < mat.length; i++) {
if(i != row && mat[i][m.get(row)] == 1) {m.put(row,null);break;};
}
if (m.get(row) != null) res++;
}
return res;
}
}
``` # 1672. Total assets of the richest customers

https://leetcode-cn.com/problems/richest-customer-wealth/

Give you an integer grid accounts of m x n, where accounts[i][j] is the number of assets hosted by the ith customer in the j bank. Returns the total amount of assets owned by the richest customer.
The total assets of customers are the sum of the assets they hold in each bank. The richest customer is the customer with the largest total assets.

This problem is to write a for loop and a recursive
The idea is to calculate the sum of the array of each row, and then judge which row has the largest sum. The result is the sum of the largest row array

## for loop

```class Solution {
public int maximumWealth(int[][] accounts) {
Integer res = 0;
for (int i = 0; i < accounts.length; i++) {
Integer r = 0;
for (int j = 0; j < accounts[i].length; j++) {
r+= accounts[i][j];
}
if(res < r){
res = r;
}
}
return res;
}
}
```

## Recursion + for

```class Solution {
public int maximumWealth(int[][] accounts) {
Integer res = 0;
for (int i = 0; i < accounts.length; i++) {
int a = getValue(accounts[i],accounts[i].length,0);
if(res < a){
res = a;
}
}
return res;
}

public Integer getValue(int[] nump,int index,int n){
if(index == 0) return 0;
n = nump[index-1];
return n + getValue(nump,index - 1,n);
}
}
```

## recursion

```class Solution {
public int maximumWealth(int[][] accounts) {
return getNums(accounts,accounts.length,0);
}

public Integer getNums(int[][] nums,int index,int n){
if(index == 0) return n;
int a = getValue(nums[index - 1],nums[index - 1].length,0);
if(a > n) n = a;
return getNums(nums,index - 1,n);
}
public Integer getValue(int[] nump,int index,int n){
if(index == 0) return 0;
n = nump[index-1];
return n + getValue(nump,index - 1,n);
}
}
```  Keywords: Java leetcode

Added by sx on Sun, 24 Oct 2021 07:43:10 +0300