zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1310. 子数组异或查询 | XOR Queries of a Subarray

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/12151960.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    Given the array arr of positive integers and the array queries where queries[i] = [Li, Ri], for each query i compute the XOR of elements from Li to Ri (that is, arr[Li] xor arr[Li+1] xor ... xor arr[Ri] ). Return an array containing the result for the given queries.

    Example 1:

    Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
    Output: [2,7,14,8] 
    Explanation: 
    The binary representation of the elements in the array are:
    1 = 0001 
    3 = 0011 
    4 = 0100 
    8 = 1000 
    The XOR values for queries are:
    [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
    

    Example 2:

    Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
    Output: [8,0,4,4]
    

    Constraints:

    • 1 <= arr.length <= 3 * 10^4
    • 1 <= arr[i] <= 10^9
    • 1 <= queries.length <= 3 * 10^4
    • queries[i].length == 2
    • 0 <= queries[i][0] <= queries[i][1] < arr.length

    有一个正整数数组 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]
    

    提示:

    • 1 <= arr.length <= 3 * 10^4
    • 1 <= arr[i] <= 10^9
    • 1 <= queries.length <= 3 * 10^4
    • queries[i].length == 2
    • 0 <= queries[i][0] <= queries[i][1] < arr.length

    Runtime: 556 ms
    Memory Usage: 25.7 MB
     1 class Solution {
     2     func xorQueries(_ arr: [Int], _ queries: [[Int]]) -> [Int] {
     3         var arr = arr
     4         var res:[Int] = [Int](repeating:0,count:queries.count)
     5         var q:[Int] = [Int]()
     6         for i in 1..<arr.count
     7         {
     8             arr[i] ^= arr[i - 1]
     9         }
    10         for i in 0..<queries.count
    11         {
    12             q = queries[i]
    13             res[i] = q[0] > 0 ? arr[q[0] - 1] ^ arr[q[1]] : arr[q[1]]
    14         }
    15         return res
    16     }
    17 }

    Runtime: 540 ms
    Memory Usage: 26.2 MB
    1  class Solution {
    2     func xorQueries(_ arr: [Int], _ queries: [[Int]]) -> [Int] {
    3         var prefix = Array<Int>(repeating: 0, count: arr.count + 1)
    4         for index in 0..<arr.count {
    5             prefix[index + 1] = prefix[index] ^ arr[index]
    6         }
    7         return queries.map {prefix[$0[0]] ^ prefix[$0[1] + 1]}
    8     }
    9  }
  • 相关阅读:
    vijos p1782——借教室(noip2012提高组第2题)
    vijos p1781——同余方程(noip2012提高组第1题)
    vijos p1905——生活大爆炸版 石头剪刀布(noip2014提高组第一题)
    URAL_1018 二叉苹果树
    b_lc_统计同构子字符串的数目(找规律 / dp)
    a_lc_完成所有工作的最短时间(暴搜 / 状压)
    lc_b_栈和队列设计(都需要不断踢出非法元素的过程)
    a_lc_缺失的第一个整数 I~II(暴力 / 不断放到正确位置)
    b_lc_最短无序连续子数组(暴力 / )
    b_lc_把二叉搜索树转换为累加树(逆中序遍历 / 迭代)
  • 原文地址:https://www.cnblogs.com/strengthen/p/12151960.html
Copyright © 2011-2022 走看看