-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: original solution for challenge1 of Pattern12
- Loading branch information
1 parent
dd18336
commit ce147b5
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package challenge1 | ||
|
||
/* | ||
Given a binary matrix representing an image, we want to flip the image horizontally, then invert it. | ||
To flip an image horizontally means that each row of the image is reversed. For example, flipping [0, 1, 1] horizontally results in [1, 1, 0]. | ||
To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [1, 1, 0] results in [0, 0, 1]. | ||
Input: [ | ||
[1,0,1], | ||
[1,1,1], | ||
[0,1,1] | ||
] | ||
Output: [ | ||
[0,1,0], | ||
[0,0,0], | ||
[0,0,1] | ||
] | ||
Input: [ | ||
[1,1,0,0], | ||
[1,0,0,1], | ||
[0,1,1,1], | ||
[1,0,1,0] | ||
] | ||
Output: [ | ||
[1,1,0,0], | ||
[0,1,1,0], | ||
[0,0,0,1], | ||
[1,0,1,0] | ||
] | ||
ref: https://leetcode-cn.com/problems/flipping-an-image/ | ||
*/ | ||
|
||
func flipAndInvertImage(image [][]int) [][]int { | ||
if len(image) == 0 { | ||
return [][]int{} | ||
} | ||
|
||
s := len(image[0]) | ||
for i := 0; i < len(image); i++ { | ||
for j := 0; j < (s+1)/2; j++ { | ||
image[i][j], image[i][s-j-1] = image[i][s-j-1]^1, image[i][j]^1 | ||
} | ||
} | ||
return image | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package challenge1 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_flipAndInvertImage(t *testing.T) { | ||
type args struct { | ||
image [][]int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want [][]int | ||
}{ | ||
{"case1", args{[][]int{}}, [][]int{}}, | ||
{"case2", args{[][]int{{1, 0, 1}, {1, 1, 1}, {0, 1, 1}}}, [][]int{{0, 1, 0}, {0, 0, 0}, {0, 0, 1}}}, | ||
{"case3", args{[][]int{{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}}}, [][]int{{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 1}, {1, 0, 1, 0}}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := flipAndInvertImage(tt.args.image); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("flipAndInvertImage() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |