給定一個整數 N,找出 1 到 N 範圍內的異或
1 ^ 2 ^ 3 ^4 ^.....N 的異或;
暴力方法:
Tc:O(n)
Sc:O(1)
public int findExor(int N){ //naive/brute force approach: int val = 0; for(int i=1;i <p>最佳方法:<br> Tc:O(1)<br> Sc:O(1)<br> </p> <pre class="brush:php;toolbar:false"> public int getExor(int N){ //better approach /** * one thing to observe is * 1 = 001 = 1 * 1 ^2 = 001 ^ 010 = 011= 3 * 1^2^3 = 011 ^ 011 = 0= 0 * 1^2^3^4 = 000^100 = 100= 4 * 1^2^3^4^5 = 100^101 = 001= 1 * 1^2^3^4^5^6 = 001^110 =111= 7 * 1^2^3^4^5^6^7 = 111^111=000= 0 * * what we can observer is : * * N%4==0 then result is: N * N%4 ==1 then result is: 1 * N%4 ==2 then result is: N+1 * N%4==3 then result is: 0 * * */ if(N%4==0) return N; else if(N%4 ==1) return 1; else if(N%4==2) return N+1; else return 0; }
如果我們必須找到 L 和 R 等範圍之間的 異或怎麼辦
例如找出數字 4 和 7 之間的異或,即 4^5^6^7。
為了解決這個問題,我們可以利用與 getExor() 相同的最佳解決方案
首先我們將得到exor直到L-1,即getExor(L-1) = 1 ^ 2 ^ 3(因為L-1 = 3)…方程式(1)
然後我們會發現 getExor(R) = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ----equation(2)
最後,
Result = equation(1) ^ equation(2) = (1 ^ 2 ^ 3) ^ (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7) = (4^5^6^7)
public int findExorOfRange(int L, int R){ return getExor(L-1) ^ getExor(R); } public int getExor(int N){ //better approach /** * one thing to observe is * 1 = 001 = 1 * 1 ^2 = 001 ^ 010 = 011= 3 * 1^2^3 = 011 ^ 011 = 0= 0 * 1^2^3^4 = 000^100 = 100= 4 * 1^2^3^4^5 = 100^101 = 001= 1 * 1^2^3^4^5^6 = 001^110 =111= 7 * 1^2^3^4^5^6^7 = 111^111=000= 0 * * what we can observer is : * * N%4==0 then result is: N * N%4 ==1 then result is: 1 * N%4 ==2 then result is: N+1 * N%4==3 then result is: 0 * * */ if(N%4==0) return N; else if(N%4 ==1) return 1; else if(N%4==2) return N+1; else return 0; }
以上是N 個數字的異或的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Java'splatFormIndependecemeansDeveloperScanWriteCeandeCeandOnanyDeviceWithouTrecompOlding.thisAcachivedThroughThroughTheroughThejavavirtualmachine(JVM),WhaterslatesbyTecodeDecodeOdeIntComenthendions,允許univerniverSaliversalComplatibilityAcrossplatss.allospplats.s.howevss.howev

要設置JVM,需按以下步驟進行:1)下載並安裝JDK,2)設置環境變量,3)驗證安裝,4)設置IDE,5)測試運行程序。設置JVM不僅僅是讓其工作,還包括優化內存分配、垃圾收集、性能調優和錯誤處理,以確保最佳運行效果。

toensurejavaplatFormIntence,lofterTheSeSteps:1)compileAndRunyOpplicationOnmultPlatFormSusiseDifferenToSandjvmversions.2)upureizeci/cdppipipelinelikeinkinslikejenkinsorgithikejenkinsorgithikejenkinsorgithikejenkinsorgithike forautomatecross-plateftestesteftestesting.3)

javastandsoutsoutinmoderndevelopmentduetoitsrobustfeatureslikelambdaexpressions,streams,andenhanced concurrencysupport.1)lambdaexpressionssimplifyfunctional promprogientsmangional programmanging,makencodemoreconciseandable.2)

Java的核心特點包括平台獨立性、面向對象設計和豐富的標準庫。 1)面向對象設計通過多態等特性使得代碼更加靈活和可維護。 2)垃圾回收機制解放了開發者的內存管理負擔,但需要優化以避免性能問題。 3)標準庫提供了從集合到網絡的強大工具,但應謹慎選擇數據結構以保持代碼簡潔。

Yes,Javacanruneverywhereduetoits"WriteOnce,RunAnywhere"philosophy.1)Javacodeiscompiledintoplatform-independentbytecode.2)TheJavaVirtualMachine(JVM)interpretsorcompilesthisbytecodeintomachine-specificinstructionsatruntime,allowingthesameJava

jdkincludestoolsfordEveloping and compilingjavacode,whilejvmrunsthecompiledbytecode.1)jdkcontainsjre,編譯器,andutilities.2)

Java的關鍵特性包括:1)面向對象設計,2)平台獨立性,3)垃圾回收機制,4)豐富的庫和框架,5)並發支持,6)異常處理,7)持續演進。 Java的這些特性使其成為開發高效、可維護軟件的強大工具。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具