LeetCode之Flipping an Image(Kotlin)

问题:
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].


方法:
参考代码实现。

具体实现:

class FlippingAnImage {
    fun flipAndInvertImage(A: Array<IntArray>): Array<IntArray> {
        for (i in A.indices) {
            for (j in 0..A.lastIndex / 2) {
                val temp = A[i][j]
                A[i][j] = invert(A[i][A.lastIndex - j])
                A[i][A.lastIndex - j] = invert(temp)
            }
        }
        return A
    }

    private fun invert(num: Int): Int {
        if (num == 0) {
            return 1
        } else {
            return 0
        }
    }
}

fun main(args: Array<String>) {
    val array = arrayOf(intArrayOf(1, 1, 0), intArrayOf(1, 0, 1), intArrayOf(0, 0, 0))
    val flippingAnImage = FlippingAnImage()
    flippingAnImage.flipAndInvertImage(array)
    CommonUtils.print2DIntArray(array)
}

有问题随时沟通

具体代码实现可以参考Github

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 14,246评论 3 20
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,150评论 0 10
  • 音乐是我唯一的疗伤药 小时候总喜欢听爸爸唱歌,不管老歌还是流行歌曲,我总会安安静静地听他唱完每一首歌,后来听的时间...
    凡人仆阅读 1,722评论 10 5
  • 我没那么喜欢你 所谓回忆 不过是回想过去的工具 没什么了不起 我没那么喜欢你 你不用担心 我不会太想念你 最多无聊...
    斐炎凉阅读 3,214评论 27 18
  • 印象深刻的三部分: 1.心理学研究对象的心理过程分为认知过程,情绪过程,意志过程 2.个性心理分为个性倾向和个性心...
    简爱H阅读 871评论 0 1