leetcode 1310. 子數組異或查詢(位運算)

有一個正整數數組 arr,現給你一個對應的查詢數組 queries,其中 queries[i] = [Li, Ri]。

對于每個查詢 i,請你計算從 Li 到 Ri 的 XOR 值(即 arr[Li] xor arr[Li+1] xor … xor arr[Ri])作為本次查詢的結果。

并返回一個包含給定查詢 queries 所有結果的數組。

示例 1:

輸入:arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
輸出:[2,7,14,8]
解釋:
數組中元素的二進制表示形式是:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
查詢的 XOR 值為:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
示例 2:

輸入:arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
輸出:[8,0,4,4]

解題思路

維護一個前綴數組sum,sum[i]代表原數組中arr[0] xor arr[1]…arr[i]的結果

因此如果要查詢某個區間[i…j]的異或的結果,那么只需要計算sum[i-1]^sum[j]

因為

  • sum[j]=arr[0] xor arr[1]…xor arr[i-1]…xor arr[j]
  • sum[i-1]=arr[0] xor arr[1]…xor arr[i-1]
  • sum[i-1] xor arr[i]…xor arr[j]=sum[j]
    所以可得 arr[i]…xor arr[j]=sum[i-1]^sum[j]

代碼

func xorQueries(arr []int, queries [][]int) []int {n,m := len(arr),len(queries)sum := make([]int, n)res:=make([]int,m)sum[0]=arr[0]for i := 1; i <n ; i++ {sum[i]=arr[i]^sum[i-1]}for i, query := range queries {if query[0]==0{res[i]=sum[query[1]]}else {res[i]=sum[query[0]-1]^sum[query[1]]}}return res	
}

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

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

相關文章

大樣品隨機雙盲測試_訓練和測試樣品生成

大樣品隨機雙盲測試This post aims to explore a step-by-step approach to create a K-Nearest Neighbors Algorithm without the help of any third-party library. In practice, this Algorithm should be useful enough for us to classify our data whenever we have alre…

vue組件命名指南,不為取名而糾結

前言 自古中國取名文化博大進深,往往取一個好的名字而絞盡腦汁.那么一個好名字能夠帶來什么呢? 名字的內涵必需和使用者固有的本性相配套不和名人重名、不易重名、創意新穎&#xff0c;真正體現通過名字以區分人的作用響亮上口讀起來流暢好聽&#xff0c;協音美好&#xff0c;…

JavaScript 基礎,登錄驗證

<script></script>的三種用法&#xff1a;放在<body>中放在<head>中放在外部JS文件中三種輸出數據的方式&#xff1a;使用 document.write() 方法將內容寫到 HTML 文檔中。使用 window.alert() 彈出警告框。使用 innerHTML 寫入到 HTML 元素。使用 &qu…

使用final類的作用是什么?

問題&#xff1a;使用final類的作用是什么&#xff1f; 我在看一本關于Java的書&#xff0c;它里面說你可以定義一個類為final。我搞不明白有什么地方會被用到這樣。 我是一個編程萌新。我想知道程序員在他們的程序里面都是怎么用fianl類的。如果知道他們是什么時候使用的話&…

photoshop cc_如何使用Photoshop CC將圖片變成卡通

photoshop ccA fun photo effect is to make a photo look like a cartoon. In this tutorial you will learn how to use Photoshop CC to make a photo look like a cartoon drawing.有趣的照片效果是使照片看起來像卡通漫畫。 在本教程中&#xff0c;您將學習如何使用Photos…

從數據角度探索在新加坡的非法毒品

All things are poisons, for there is nothing without poisonous qualities. It is only the dose which makes a thing poison.” ― Paracelsus萬物都是毒藥&#xff0c;因為沒有毒藥就沒有什么。 只是使事物中毒的劑量。” ― 寄生蟲 執行摘要(又名TL&#xff1b; DR) (Ex…

Android 自定義View實現QQ運動積分抽獎轉盤

因為偶爾關注QQ運動&#xff0c; 看到QQ運動的積分抽獎界面比較有意思&#xff0c;所以就嘗試用自定義View實現了下&#xff0c;原本想通過開發者選項查看下界面的一些信息&#xff0c;后來發現積分抽獎界面是在WebView中展示的&#xff0c;應該是在H5頁面中用js代碼實現的&…

瑞立視:厚積薄發且具有“工匠精神”的中國品牌

一家成立兩年的公司&#xff1a;是如何在VR行業趨于穩定的情況下首次融資就獲得如此大額的金額呢&#xff1f; 2017年VR行業內宣布融資的公司寥寥無幾&#xff0c;無論是投資人還是消費者對這個 “寵兒”都開始紛紛投以懷疑的目光。但就在2017年7月27日&#xff0c;深圳市一家…

蘋果系統使用svg 動畫_為什么要使用SVG圖像:如何為SVG設置動畫并使其快速閃電化

蘋果系統使用svg 動畫我們為什么要使用SVG&#xff1f; (Why Are We Using SVG?) The web development sector is growing at a rapid pace, and SVG (scalable vector graphics) images are becoming more popular. As vector images, SVGs are composed of mathematical equ…

CSV模塊的使用

CSV模塊的使用 1、csv簡介 CSV (Comma Separated Values)&#xff0c;即逗號分隔值&#xff08;也稱字符分隔值&#xff0c;因為分隔符可以不是逗號&#xff09;&#xff0c;是一種常用的文本 格式&#xff0c;用以存儲表格數據&#xff0c;包括數字或者字符。很多程序在處理數…

Java里面遍歷list的方式

問題&#xff1a;Java里面遍歷list的方式 對于Java語言有點陌生&#xff0c;我嘗試熟悉遍歷list&#xff08;或者其他集合&#xff09;的所有方法&#xff08;或者是其他正確的語法&#xff09;和它們每一個方法的優缺點 給定 List的list對象&#xff0c;我知道有下列方法去循…

python 重啟內核_Python從零開始的內核回歸

python 重啟內核Every beginner in Machine Learning starts by studying what regression means and how the linear regression algorithm works. In fact, the ease of understanding, explainability and the vast effective real-world use cases of linear regression is…

bzoj千題計劃282:bzoj4517: [Sdoi2016]排列計數

http://www.lydsy.com/JudgeOnline/problem.php?id4517 組合數錯排公式 #include<cstdio> #include<iostream>using namespace std;#define N 1000001const int mod1e97;long long fac[N],inv[N],f[N];void read(int &x) {x0; char cgetchar();while(!isdigit…

chrome啟用flash_如何在Google Chrome中啟用Adobe Flash Player

chrome啟用flashRemember Adobe Flash player? Its that nifty software that lets websites embed videos and web games. Whole websites can even be powered by Flash.還記得Adobe Flash Player嗎&#xff1f; 正是這些漂亮的軟件使網站可以嵌入視頻和網絡游戲。 整個網站…

怎么樣把Java的字符串轉化為字節數組?

問題&#xff1a;怎么樣把Java的字符串轉化為字節數組 有沒有任何方法把Java的字符串轉化為字節數組 我嘗試這樣: System.out.println(response.split("\r\n\r\n")[1]); System.out.println("******"); System.out.println(response.split("\r\n\r\…

Forward團隊-爬蟲豆瓣top250項目-模塊開發過程

項目托管平臺地址:https://github.com/xyhcq/top250 開發模塊功能: 寫入文件功能 開發時間:3小時 實現將爬取到的信息寫入到文件中的功能 實現過程&#xff1a; # 打開文件 fopen("top250.txt","w") 在別的隊員寫的代碼基礎上&#xff0c;加入功能代碼 de…

CSS3 outline-offset 屬性 項目中input會遇到

outline在一個聲明中設置所有的輪廓屬性。outline:顏色&#xff08;outline-line&#xff09;樣式&#xff08;outline-style&#xff09;寬度&#xff08;outline-width&#xff09; outline-offset 屬性對輪廓進行偏移&#xff0c;并在邊框邊緣進行繪制。 輪廓在兩方面與邊框…

回歸分析中自變量共線性_具有大特征空間的回歸分析中的變量選擇

回歸分析中自變量共線性介紹 (Introduction) Performing multiple regression analysis from a large set of independent variables can be a challenging task. Identifying the best subset of regressors for a model involves optimizing against things like bias, multi…

winform窗體模板_如何驗證角模板驅動的窗體

winform窗體模板介紹 (Introduction) In this article, we will learn about validations in Angular template-driven forms. We will create a simple user registration form and implement some inbuilt validations on it. Along with the inbuilt validations, we will a…

【loj6191】「美團 CodeM 復賽」配對游戲 概率期望dp

題目描述 n次向一個棧中加入0或1中隨機1個&#xff0c;如果一次加入0時棧頂元素為1&#xff0c;則將這兩個元素彈棧。問最終棧中元素個數的期望是多少。 輸入 一行一個正整數 n 。 輸出 一行一個實數&#xff0c;表示期望剩下的人數&#xff0c;四舍五入保留三位小數。 樣例輸入…