-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFlippingAnImage.cs
65 lines (61 loc) · 2.28 KB
/
FlippingAnImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 832. Flipping an Image
/// Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
/// IE) Input: [ [1,1,0], [1,0,1], [0,0,0] ]
/// Output: [ [1,0,0], [0,1,0], [1,1,1] ]
/// Explanation: First reverse each row: [ [0,1,1], [1,0,1], [0,0,0] ]
/// Then, invert the image: [ [1,0,0], [0,1,0], [1,1,1] ]
/// </summary>
public class FlippingAnImage : Solution
{
private int[][] _arr;
public FlippingAnImage(int[][] array)
{
this._arr = array;
}
int[][] FlipAndInvertImage(int[][] array)
{
//Goals: Given a Binary 2D array,
//1) Reverse the array,
//2) Flip the numbers
int[][] results = new int[array.Length][];
for (int i = 0; i < array.Length; i++)
{
//Create inside array
results[i] = new int[array[i].Length];
int index = 0;
//Reverse the array
for (int j = array[i].Length - 1; j >= 0; j--)
{
//Flip the numbers
if (array[i][j] == 1)
{
results[i][index++] = 0;
}
else
{
results[i][index++] = 1;
}
}
}
return results;
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = FlipAndInvertImage(this._arr);
watch.Stop();
string resultString =
$"[ [{results[0][0]},{results[0][1]},{results[0][2]}], [{results[1][0]},{results[1][1]},{results[1][2]}], [{results[2][0]},{results[2][1]},{results[2][2]}] ]";
Console.WriteLine($"832. Flipping an Image\n" +
$"Input: [ [1,1,0], [1,0,1], [0,0,0] ]\n" +
$"Result: {resultString} \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}