Skip to content

Commit 8bd8e8c

Browse files
committed
2025-02-05 v. 8.4.3: added "1310. XOR Queries of a Subarray"
1 parent 07302b5 commit 8bd8e8c

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
675675
| 1288. Remove Covered Intervals | [Link](https://leetcode.com/problems/remove-covered-intervals/) | [Link](./lib/medium/1288_remove_covered_intervals.rb) | [Link](./test/medium/test_1288_remove_covered_intervals.rb) |
676676
| 1302. Deepest Leaves Sum | [Link](https://leetcode.com/problems/deepest-leaves-sum/) | [Link](./lib/medium/1302_deepest_leaves_sum.rb) | [Link](./test/medium/test_1302_deepest_leaves_sum.rb) |
677677
| 1305. All Elements in Two Binary Search Trees | [Link](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/) | [Link](./lib/medium/1305_all_elements_in_two_binary_search_trees.rb) | [Link](./test/medium/test_1305_all_elements_in_two_binary_search_trees.rb) |
678+
| 1310. XOR Queries of a Subarray | [Link](https://leetcode.com/problems/xor-queries-of-a-subarray/) | [Link](./lib/medium/1310_xor_queries_of_a_subarray.rb) | [Link](./test/medium/test_1310_xor_queries_of_a_subarray.rb) |
678679
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
679680
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
680681
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.4.2'
8+
s.version = '8.4.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/xor-queries-of-a-subarray/
4+
# @param {Integer[]} arr
5+
# @param {Integer[][]} queries
6+
# @return {Integer[]}
7+
def xor_queries(arr, queries)
8+
(1...arr.size).each { |i| arr[i] ^= arr[i - 1] }
9+
10+
result = ::Array.new(queries.size)
11+
12+
queries.each_with_index do |query, i|
13+
result[i] =
14+
if query[0].zero?
15+
arr[query[1]]
16+
else
17+
arr[query[0] - 1] ^ arr[query[1]]
18+
end
19+
end
20+
21+
result
22+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1310_xor_queries_of_a_subarray'
5+
require 'minitest/autorun'
6+
7+
class XorQueriesOfASubarrayTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[2, 7, 14, 8],
11+
xor_queries(
12+
[1, 3, 4, 8],
13+
[
14+
[0, 1],
15+
[1, 2],
16+
[0, 3],
17+
[3, 3]
18+
]
19+
)
20+
)
21+
end
22+
23+
def test_default_two
24+
assert_equal(
25+
[8, 0, 4, 4],
26+
xor_queries(
27+
[4, 8, 2, 10],
28+
[
29+
[2, 3],
30+
[1, 3],
31+
[0, 0],
32+
[0, 3]
33+
]
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)