leetcode 832. 翻轉圖像

給定一個二進制矩陣 A,我們想先水平翻轉圖像,然后反轉圖像并返回結果。

水平翻轉圖片就是將圖片的每一行都進行翻轉,即逆序。例如,水平翻轉 [1, 1, 0] 的結果是 [0, 1, 1]。

反轉圖片的意思是圖片中的 0 全部被 1 替換, 1 全部被 0 替換。例如,反轉 [0, 1, 1] 的結果是 [1, 0, 0]。

示例 1:

輸入: [[1,1,0],[1,0,1],[0,0,0]]
輸出: [[1,0,0],[0,1,0],[1,1,1]]
解釋: 首先翻轉每一行: [[0,1,1],[1,0,1],[0,0,0]];
然后反轉圖片: [[1,0,0],[0,1,0],[1,1,1]]

解題思路

在交換次序的同時翻轉矩陣

代碼

class Solution {public int[][] flipAndInvertImage(int[][] A) {int s=(A[0].length&1)==1?(A[0].length+1)>>1:A[0].length>>1;//根據列數,計算交換次數for (int i = 0; i < A.length; i++) {for(int j=0;j<s;j++){swap(A[i],j,A[0].length-1-j);}}return A;}public  void   swap(int[] c,int l,int r)//交換位置并翻轉{c[l]=c[l]==1?0:1;if(r==l) return;c[r]=c[r]==1?0:1;int temp=c[l];c[l]=c[r];c[r]=temp;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391726.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391726.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391726.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SVN服務備份操作步驟

SVN服務備份操作步驟1、準備源服務器和目標服務器源服務器&#xff1a;192.168.1.250目標服務器&#xff1a;192.168.1.251 root/rootroot 2、對目標服務器&#xff08;251&#xff09;裝SVN服務器&#xff0c; 腳本如下&#xff1a;yum install subversion 3、創建一個新的倉庫…

SpringCloud入門(一)

1. 系統架構演變概述 #mermaid-svg-F8dvnEDl6rEgSP97 .label{font-family:trebuchet ms, verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-F8dvnEDl6rEgSP97 .label text{fill:#333}#mermaid-svg-F8dvnEDl6rEgSP97 .node rect,#merm…

PullToRefreshListView中嵌套ViewPager滑動沖突的解決

PullToRefreshListView中嵌套ViewPager滑動沖突的解決 最近恰好遇到PullToRefreshListView中需要嵌套ViewPager的情況,ViewPager 作為頭部添加到ListView中&#xff0c;發先ViewPager在滑動過程中流暢性太差幾乎很難左右滑動。在網上也看了很多大神的介紹&#xff0c;看了ViewP…

神經網絡 卷積神經網絡_如何愚弄神經網絡?

神經網絡 卷積神經網絡Imagine you’re in the year 2050 and you’re on your way to work in a self-driving car (probably). Suddenly, you realize your car is cruising at 100KMPH on a busy road after passing through a cross lane and you don’t know why.想象一下…

數據特征分析-分布分析

分布分析用于研究數據的分布特征&#xff0c;常用分析方法&#xff1a; 1、極差 2、頻率分布 3、分組組距及組數 df pd.DataFrame({編碼:[001,002,003,004,005,006,007,008,009,010,011,012,013,014,015],\小區:[A村,B村,C村,D村,E村,A村,B村,C村,D村,E村,A村,B村,C村,D村,E村…

開發工具總結(2)之全面總結Android Studio2.X的填坑指南

前言&#xff1a;好多 Android 開發者都在說Android Studio太坑了&#xff0c;老是出錯&#xff0c;導致開發進度變慢&#xff0c;出錯了又不知道怎么辦&#xff0c;網上去查各種解決方案五花八門&#xff0c;有些可以解決問題&#xff0c;有些就是轉來轉去的寫的很粗糙&#x…

無聊的一天_一人互聯網公司背后的無聊技術

無聊的一天Listen Notes is a podcast search engine and database. The technology behind Listen Notes is actually very very boring. No AI, no deep learning, no blockchain. “Any man who must say I am using AI is not using True AI” :)Listen Notes是一個播客搜索…

如何在Pandas中使用Excel文件

From what I have seen so far, CSV seems to be the most popular format to store data among data scientists. And that’s understandable, it gets the job done and it’s a quite simple format; in Python, even without any library, one can build a simple CSV par…

Js實現div隨鼠標移動的方法

HTML: <div id"odiv" style" COLOR: #666; padding: 2px 8px; FONT-SIZE: 12px; MARGIN-RIGHT: 5px; position: absolute; background: #fff; display: block; border: 1px solid #666; top: 50px; left: 10px;"> Move_Me</div>第一種&…

leetcode 867. 轉置矩陣

給你一個二維整數數組 matrix&#xff0c; 返回 matrix 的 轉置矩陣 。 矩陣的 轉置 是指將矩陣的主對角線翻轉&#xff0c;交換矩陣的行索引與列索引。 示例 1&#xff1a; 輸入&#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9]] 輸出&#xff1a;[[1,4,7],[2,5,8],[3,6,9]] …

數據特征分析-對比分析

對比分析是對兩個互相聯系的指標進行比較。 絕對數比較(相減)&#xff1a;指標在量級上不能差別過大&#xff0c;常用折線圖、柱狀圖 相對數比較(相除)&#xff1a;結構分析、比例分析、空間比較分析、動態對比分析 df pd.DataFrame(np.random.rand(30,2)*1000,columns[A_sale…

Linux基線合規檢查中各文件的作用及配置腳本

1./etc/motd 操作&#xff1a;echo " Authorized users only. All activity may be monitored and reported " > /etc/motd 效果&#xff1a;telnet和ssh登錄后的輸出信息 2. /etc/issue和/etc/issue.net 操作&#xff1a;echo " Authorized users only. All…

tableau使用_使用Tableau升級Kaplan-Meier曲線

tableau使用In a previous article, I showed how we can create the Kaplan-Meier curves using Python. As much as I love Python and writing code, there might be some alternative approaches with their unique set of benefits. Enter Tableau!在上一篇文章中 &#x…

踩坑 net core

webclient 可以替換為 HttpClient 下載獲取url的內容&#xff1a; 證書&#xff1a; https://stackoverflow.com/questions/40014047/add-client-certificate-to-net-core-httpclient 轉載于:https://www.cnblogs.com/zxs-onestar/p/7340386.html

我從參加#PerfMatters會議中學到的東西

by Stacey Tay通過史黛西泰 我從參加#PerfMatters會議中學到的東西 (What I learned from attending the #PerfMatters conference) 從前端的網絡運行情況發布會上的注意事項 (Notes from a front-end web performance conference) This week I had the privilege of attendin…

修改innodb_flush_log_at_trx_commit參數提升insert性能

最近&#xff0c;在一個系統的慢查詢日志里發現有個insert操作很慢&#xff0c;達到秒級&#xff0c;并且是比較簡單的SQL語句&#xff0c;把語句拿出來到mysql中直接執行&#xff0c;速度卻很快。 這種問題一般不是SQL語句本身的問題&#xff0c;而是在具體的應用環境中&#…

leetcode 1178. 猜字謎(位運算)

外國友人仿照中國字謎設計了一個英文版猜字謎小游戲&#xff0c;請你來猜猜看吧。 字謎的迷面 puzzle 按字符串形式給出&#xff0c;如果一個單詞 word 符合下面兩個條件&#xff0c;那么它就可以算作謎底&#xff1a; 單詞 word 中包含謎面 puzzle 的第一個字母。 單詞 word…

Nexus3.x.x上傳第三方jar

exus3.x.x上傳第三方jar&#xff1a; 1. create repository 選擇maven2(hosted)&#xff0c;說明&#xff1a; proxy&#xff1a;即你可以設置代理&#xff0c;設置了代理之后&#xff0c;在你的nexus中找不到的依賴就會去配置的代理的地址中找hosted&#xff1a;你可以上傳你自…

責備的近義詞_考試結果危機:我們應該責備算法嗎?

責備的近義詞I’ve been considering writing on the topic of algorithms for a little while, but with the Exam Results Fiasco dominating the headline news in the UK during the past week, I felt that now is the time to look more closely into the subject.我一直…

電腦如何設置終端設置代理_如何設置一個嚴肅的Kubernetes終端

電腦如何設置終端設置代理by Chris Cooney克里斯庫尼(Chris Cooney) 如何設置一個嚴肅的Kubernetes終端 (How to set up a serious Kubernetes terminal) 所有k8s書呆子需要的CLI工具 (All the CLI tools a growing k8s nerd needs) Kubernetes comes pre-packaged with an ou…