leetcode 1442. 形成兩個異或相等數組的三元組數目(位運算)

給你一個整數數組 arr 。

現需要從數組中取三個下標 i、j 和 k ,其中 (0 <= i < j <= k < arr.length) 。

a 和 b 定義如下:

a = arr[i] ^ arr[i + 1] ^ … ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ … ^ arr[k]
注意:^ 表示 按位異或 操作。

請返回能夠令 a == b 成立的三元組 (i, j , k) 的數目。

示例 1:

輸入:arr = [2,3,1,6,7]
輸出:4
解釋:滿足題意的三元組分別是 (0,1,2), (0,2,2), (2,3,4) 以及 (2,4,4)

示例 2:

輸入:arr = [1,1,1,1,1]
輸出:10
示例 3:

輸入:arr = [2,3]
輸出:0
示例 4:

輸入:arr = [1,3,5,7,9]
輸出:3
示例 5:

輸入:arr = [7,11,12,9,5,2,7,17,22]
輸出:8

解題思路

因為題目中
a 和 b 定義如下:
a = arr[i] ^ arr[i + 1] ^ … ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ … ^ arr[k]

前綴異或數組s[j],代表前i個數字的異或結果

  • s[j]=arr[0]^…arr[j-1]
  • s[i]=arr[0]^…arr[i-1]
  • s[k+1]=arr[0]^…arr[k]
  • s[i…j]=arr[i]^… arr[j-1]=a=s[i] ^s[j]
  • s[j…k+1]=arr[j]^…arr[k]=b=s[j] ^ s[k+1]
    要使得a=b,則s[i]^s[j]=s[j] ^s[k+1],即s[i]=s[k+1]
  1. 因為i<k+1恒成立,所以我們可以在構造前綴異或數組的同時,用map記錄下前面出現的s[i],在計算s[k+1]以后,檢查前面出現過的s[i].
  2. 又因為使得a=b成立的子數組,只需要滿足s[i]=s[k+1],與j的值無關,所以在i < j <= k的范圍內,所有j的取值均滿足條件。所以滿足s[i]=s[k+1]后,滿足條件的子數組增加為k-i,因為在前面的與s[k+1]相等的s[i]值,不止一個,所以有(k-i1)+(k-i22)…+(k-im)=m*k-(i1+i2…+im)
    所以我們只需要用map記錄下s[i]值對應的
  • m:s[i]這個值出現的次數
  • (i1+i2…+im):s[i]這個值出現的下標之和

代碼

func countTriplets(arr []int) int {res,pre:=0,0cnt := map[int]int{}sum:=map[int]int{}for k, value := range arr {cur,exis := cnt[pre^value]if exis{res+=cur*k-sum[pre^value]}cnt[pre]++sum[pre]+=kpre^=value}return res
}

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

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

相關文章

數據科學與大數據技術的案例_作為數據科學家解決問題的案例研究

數據科學與大數據技術的案例There are two myths about how data scientists solve problems: one is that the problem naturally exists, hence the challenge for a data scientist is to use an algorithm and put it into production. Another myth considers data scient…

AJAX, callback,promise and generator

AJAX with jQuery $.ajax({url:??,type:??,data:??,success: function(){??} //callback,error:function(jqXHR,textStatus,error){??} })think about what AJAX wants from human , AJAX asks questions : tell Me By Which Way You Want To Do Things : —— GET …

Spring-Boot + AOP實現多數據源動態切換

2019獨角獸企業重金招聘Python工程師標準>>> 最近在做保證金余額查詢優化&#xff0c;在項目啟動時候需要把余額全量加載到本地緩存&#xff0c;因為需要全量查詢所有騎手的保證金余額&#xff0c;為了不影響主數據庫的性能&#xff0c;考慮把這個查詢走從庫。所以涉…

css 幻燈片_如何使用HTML,CSS和JavaScript創建幻燈片

css 幻燈片A web slideshow is a sequence of images or text that consists of showing one element of the sequence in a certain time interval.網絡幻燈片是一系列圖像或文本&#xff0c;包括在一定時間間隔內顯示序列中的一個元素。 For this tutorial you can create a…

leetcode 1738. 找出第 K 大的異或坐標值

本文正在參加「Java主題月 - Java 刷題打卡」&#xff0c;詳情查看 活動鏈接 題目 給你一個二維矩陣 matrix 和一個整數 k &#xff0c;矩陣大小為 m x n 由非負整數組成。 矩陣中坐標 (a, b) 的 值 可由對所有滿足 0 < i < a < m 且 0 < j < b < n 的元素…

【數據庫】Oracle用戶、授權、角色管理

創建和刪除用戶是Oracle用戶管理中的常見操作&#xff0c;但這其中隱含了Oracle數據庫系統的系統權限與對象權限方面的知識。掌握還Oracle用戶的授權操作和原理&#xff0c;可以有效提升我們的工作效率。 Oracle數據庫的權限系統分為系統權限與對象權限。系統權限( Database Sy…

商業數據科學

數據科學 &#xff0c; 意見 (Data Science, Opinion) “There is a saying, ‘A jack of all trades and a master of none.’ When it comes to being a data scientist you need to be a bit like this, but perhaps a better saying would be, ‘A jack of all trades and …

為什么游戲開發者不玩游戲_什么是游戲開發?

為什么游戲開發者不玩游戲Game Development is the art of creating games and describes the design, development and release of a game. It may involve concept generation, design, build, test and release. While you create a game, it is important to think about t…

leetcode 692. 前K個高頻單詞

題目 給一非空的單詞列表&#xff0c;返回前 k 個出現次數最多的單詞。 返回的答案應該按單詞出現頻率由高到低排序。如果不同的單詞有相同出現頻率&#xff0c;按字母順序排序。 示例 1&#xff1a; 輸入: ["i", "love", "leetcode", "…

數據顯示,中國近一半的獨角獸企業由“BATJ”四巨頭投資

中國的互聯網行業越來越有被巨頭壟斷的趨勢。百度、阿里巴巴、騰訊、京東&#xff0c;這四大巨頭支撐起了中國近一半的獨角獸企業。CB Insights日前發表了題為“Nearly Half Of China’s Unicorns Backed By Baidu, Alibaba, Tencent, Or JD.com”的數據分析文章&#xff0c;列…

Java的Servlet、Filter、Interceptor、Listener

寫在前面&#xff1a; 使用Spring-Boot時&#xff0c;嵌入式Servlet容器可以通過掃描注解&#xff08;ServletComponentScan&#xff09;的方式注冊Servlet、Filter和Servlet規范的所有監聽器&#xff08;如HttpSessionListener監聽器&#xff09;。 Spring boot 的主 Servlet…

html5教程_最好HTML和HTML5教程

html5教程HyperText Markup Language (HTML) is a markup language used to construct online documents and is the foundation of most websites today. A markup language like HTML allows us to超文本標記語言(HTML)是用于構造在線文檔的標記語言&#xff0c;并且是當今大…

leetcode 1035. 不相交的線(dp)

在兩條獨立的水平線上按給定的順序寫下 nums1 和 nums2 中的整數。 現在&#xff0c;可以繪制一些連接兩個數字 nums1[i] 和 nums2[j] 的直線&#xff0c;這些直線需要同時滿足滿足&#xff1a; nums1[i] nums2[j] 且繪制的直線不與任何其他連線&#xff08;非水平線&#x…

SPI和RAM IP核

學習目的&#xff1a; &#xff08;1&#xff09; 熟悉SPI接口和它的讀寫時序&#xff1b; &#xff08;2&#xff09; 復習Verilog仿真語句中的$readmemb命令和$display命令&#xff1b; &#xff08;3&#xff09; 掌握SPI接口寫時序操作的硬件語言描述流程&#xff08;本例僅…

個人技術博客Alpha----Android Studio UI學習

項目聯系 這次的項目我在前端組&#xff0c;負責UI&#xff0c;下面簡略講下學到的內容和使用AS過程中遇到的一些問題及其解決方法。 常見UI控件的使用 1.TextView 在TextView中&#xff0c;首先用android:id給當前控件定義一個唯一標識符。在活動中通過這個標識符對控件進行事…

數據科學家數據分析師_站出來! 分析人員,數據科學家和其他所有人的領導和溝通技巧...

數據科學家數據分析師這一切如何發生&#xff1f; (How did this All Happen?) As I reflect on my life over the past few years, even though I worked my butt off to get into Data Science as a Product Analyst, I sometimes still find myself begging the question, …

leetcode 810. 黑板異或游戲

黑板上寫著一個非負整數數組 nums[i] 。Alice 和 Bob 輪流從黑板上擦掉一個數字&#xff0c;Alice 先手。如果擦除一個數字后&#xff0c;剩余的所有數字按位異或運算得出的結果等于 0 的話&#xff0c;當前玩家游戲失敗。 (另外&#xff0c;如果只剩一個數字&#xff0c;按位異…

react-hooks_在5分鐘內學習React Hooks-初學者教程

react-hooksSometimes 5 minutes is all youve got. So in this article, were just going to touch on two of the most used hooks in React: useState and useEffect. 有時只有5分鐘。 因此&#xff0c;在本文中&#xff0c;我們僅涉及React中兩個最常用的鉤子&#xff1a; …

分析工作試用期收獲_免費使用零編碼技能探索數據分析

分析工作試用期收獲Have you been hearing the new industry buzzword — Data Analytics(it was AI-ML earlier) a lot lately? Does it sound complicated and yet simple enough? Understand the logic behind models but dont know how to code? Apprehensive of spendi…

select的一些問題。

這個要怎么統計類別數呢&#xff1f; 哇哇哇 解決了。 之前怎么沒想到呢&#xff1f;感謝一樓。轉載于:https://www.cnblogs.com/AbsolutelyPerfect/p/7818701.html