leetcode 1720. 解碼異或后的數組(位運算)

未知 整數數組 arr 由 n 個非負整數組成。

經編碼后變為長度為 n - 1 的另一個整數數組 encoded ,其中 encoded[i] = arr[i] XOR arr[i + 1] 。例如,arr = [1,0,2,1] 經編碼后得到 encoded = [1,2,3] 。

給你編碼后的數組 encoded 和原數組 arr 的第一個元素 first(arr[0])。

請解碼返回原數組 arr 。可以證明答案存在并且是唯一的。

示例 1:

輸入:encoded = [1,2,3], first = 1
輸出:[1,0,2,1]
解釋:若 arr = [1,0,2,1] ,那么 first = 1 且 encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]

示例 2:

輸入:encoded = [6,2,7,3], first = 4
輸出:[4,2,0,7,4]

解題思路

設原數組為arr[]:

原數組轉為encoded [],存在encoded[i] = arr[i] XOR arr[i + 1],根據異或的性質可得,arr[i + 1]=arr[i] XOR encoded[i]

因為題目已經給出了arr[0],便可根據公式得出,arr[1]=arr[0] XOR encoded[0],如此類推,就可以得出原數組

代碼

func decode(encoded []int, first int) []int {n:=len(encoded)+1res := make([]int, n)res[0]=firstfor i,j := 1,0; i < n; i++{res[i]=encoded[j]^res[i-1]j++}return res
}

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

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

相關文章

adobe 書簽怎么設置_讓我們設置一些規則…沒有Adobe Analytics處理規則

adobe 書簽怎么設置Originally published at Analyst Admin.最初發布于Analyst Admin 。 In my experience working with Adobe Analytics, I’ve found that Processing Rules help in some cases, but oftentimes they create more work. I try to avoid using Processing R…

詳解linux下安裝python3環境

1、下載python3.5源碼包首先去python官網下載python3的源碼包&#xff0c;網址&#xff1a;https://www.python.org/ 進去之后點擊導航欄的Downloads&#xff0c;也可以鼠標放到Downloads上彈出菜單選擇Source code&#xff0c;表示源碼包&#xff0c;這里選擇最新版本3.5.2&am…

詳解spl_autoload_register()函數

http://blog.csdn.net/panpan639944806/article/details/23192267 轉載于:https://www.cnblogs.com/maidongdong/p/7647163.html

上海區塊鏈會議演講ppt_所以您想參加會議演講嗎? 這是我的建議。

上海區塊鏈會議演講pptYou’ve attended a few conferences, watched a lot of presentations, and decided it’s time to give a talk of your own! As someone who has both given talks at conferences, and sat on the proposal review board for others, I’m here to te…

重學TCP協議(8) TCP的11種狀態

TCP的11種狀態 為了邏輯更加清晰&#xff0c;假設主動打開連接和關閉連接皆為客戶端&#xff0c;被動打開連接和關閉連接皆為服務端 客戶端獨有的&#xff1a;&#xff08;1&#xff09;SYN_SENT &#xff08;2&#xff09;FIN_WAIT1 &#xff08;3&#xff09;FIN_WAIT2 &…

肯尼亞第三方支付_肯尼亞的COVID-19病例正在Swift增加,我們不知道為什么。

肯尼亞第三方支付COVID-19 cases in Kenya are accelerating rapidly. New cases have increased 300% month-over-month since April of this year while global and regional media have reported on the economic toll of stringent lock-down measures and heavy-handed go…

JVM命令

1、jps 查看所有虛擬機進程 -v 虛擬機啟動時JVM參數 -l 執行主類全名 2、jstat 顯示本地或遠程類裝載、內存、垃圾回收、JIT編譯等運行數據&#xff08;性能問題定位工具首選&#xff09; 格式&#xff1a;jstat [-option] vmid ms count &#xff08;示例&a…

Java 集合 List Arrays.asList

2019獨角獸企業重金招聘Python工程師標準>>> 參考鏈接&#xff1a;阿里巴巴Java開發手冊終極版v1.3.0 【強制】使用工具類 Arrays.asList()把數組轉換成集合時&#xff0c;不能使用其修改集合相關的方 法&#xff0c;它的 add/remove/clear 方法會拋出 UnsupportedO…

重學TCP協議(9) 半連接隊列、全連接隊列

1. 半連接隊列、全連接隊列基本概念 三次握手中&#xff0c;在第一步server收到client的syn后&#xff0c;把相關信息放到半連接隊列中&#xff0c;同時回復synack給client&#xff08;第二步&#xff09;&#xff0c;同時開啟一個定時器&#xff0c;如果超時還未收到 ACK 會進…

分類預測回歸預測_我們應該如何匯總分類預測?

分類預測回歸預測If you are reading this, then you probably tried to predict who will survive the Titanic shipwreck. This Kaggle competition is a canonical example of machine learning, and a right of passage for any aspiring data scientist. What if instead …

【CZY選講·Yjq的棺材】

題目描述? Yjq想要將一個長為寬為的矩形棺材&#xff08;棺材表面絕對光滑&#xff0c;所以棺材可以任意的滑動&#xff09;拖過一個L型墓道。? 如圖所示&#xff0c;L型墓道兩個走廊的寬度分別是和&#xff0c;呈90&#xff0c;并且走廊的長度遠大于。? 現在Hja想知道對于給…

“機器換人”之潮涌向珠三角,藍領工人將何去何從

企業表示很無奈&#xff0c;由于生產需要&#xff0c;并非刻意換人。 隨著傳統產業向更加現代化、自動化的新產業轉型&#xff0c;“機器換人”似乎是歷史上不可逆轉的潮流。 據報道&#xff0c;珠三角經濟圈所在的廣東省要從傳統的制造大省向制造強省轉變&#xff0c;企業轉型…

slack通知本地服務器_通過構建自己的Slack App學習無服務器

slack通知本地服務器Serverless architecture is the industrys latest buzzword and many of the largest tech companies have begun to embrace it. 無服務器架構是業界最新的流行語&#xff0c;許多大型科技公司已開始采用它。 In this article, well learn what it is an…

深入理解InnoDB(6)—獨立表空間

InnoDB的表空間 表空間可以看做是InnoDB存儲引擎邏輯結構的最高層 &#xff0c;所有的數據都是存放在表空間中。 1. Extent 對于16KB的頁來說&#xff0c;連續的64個頁就是一個區&#xff0c;也就是說一個區默認占用1MB空間大小。 每256個區被劃分成一組,第一組的前3個頁面是…

神經網絡推理_分析神經網絡推理性能的新工具

神經網絡推理Measuring the inference time of a trained deep neural model on different hardware devices is a critical task when making deployment decisions. Should you deploy your inference on 8 Nvidia V100s, on 12 P100s, or perhaps you can use 64 CPU cores?…

Eclipse斷點調試

1.1 Eclipse斷點調試概述Eclipse的斷點調試可以查看程序的執行流程和解決程序中的bug1.2 Eclipse斷點調試常用操作:A:什么是斷點&#xff1a;就是一個標記&#xff0c;從哪里開始。B:如何設置斷點&#xff1a;你想看哪里的程序&#xff0c;你就在那個有效程序的左邊雙擊即可。C…

react部署在node_如何在沒有命令行的情況下在3分鐘內將React + Node應用程序部署到Heroku

react部署在nodeIn this tutorial we will be doing a basic React Node app deploy to Heroku. 在本教程中&#xff0c;我們將進行基本的React Node應用程序部署到Heroku。 There are a lot of tutorials that do this only using the command line, so to change things u…

深入理解InnoDB(7)—系統表空間

系統表空間 可以看到&#xff0c;系統表空間和獨立表空間的前三個頁面&#xff08;頁號分別為0、1、2&#xff0c;類型分別是FSP_HDR、IBUF_BITMAP、INODE&#xff09;的類型是一致的&#xff0c;只是頁號為3&#xff5e;7的頁面是系統表空間特有的 頁號3 SYS: Insert Buffer …

CodeForces - 869B The Eternal Immortality

題意&#xff1a;已知a,b&#xff0c;求的最后一位。 分析&#xff1a; 1、若b-a>5&#xff0c;則尾數一定為0&#xff0c;因為連續5個數的尾數要么同時包括一個5和一個偶數&#xff0c;要么包括一個0。 2、若b-a<5&#xff0c;直接暴力求即可。 #include<cstdio>…

如何在24行JavaScript中實現Redux

90% convention, 10% library. 90&#xff05;的慣例&#xff0c;10&#xff05;的圖書館。 Redux is among the most important JavaScript libraries ever created. Inspired by prior art like Flux and Elm, Redux put JavaScript functional programming on the map by i…