Re: [閒聊] 每日leetcode

看板Marginalman作者 (神楽めあ的錢包)時間10月前 (2025/01/17 00:36), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1289/1552 (看更多)
2425. Bitwise XOR of All Pairings 思路: 應該可以直接硬幹 不過根據xor的特性 兩個相同的數xor結果會變成0 所以我們只要思考同個數出現奇數次的情況 當nums1所含的數個數為奇數時 nums2裡的數會出現奇數次,反之亦然 所以去判斷nums1、nums2的長度是不是奇數 如果是的話就分別把答案xor上nums2、nums1裡的數就好 golang code: func xorAllNums(nums1 []int, nums2 []int) int { ans := 0 if len(nums1) & 1 == 1{ for _,val := range nums2{ ans ^= val } } if len(nums2) & 1 == 1 { for _,val := range nums1{ ans ^= val } } return ans } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.73.231.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1737045376.A.79B.html
文章代碼(AID): #1dYJM0UR (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dYJM0UR (Marginalman)