Skip to content

Commit

Permalink
力扣每日一刷第 1442 题
Browse files Browse the repository at this point in the history
  • Loading branch information
smltq committed May 18, 2021
1 parent 0da20ad commit ff4bd7a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion leetcode/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LeetCode 题解目录(共117题)
# LeetCode 题解目录(共118题)

## 二叉树

Expand Down Expand Up @@ -49,6 +49,7 @@
|21|[34 在排序数组中查找元素的第一个和最后一个位置](https://github.com/smltq/spring-boot-demo/blob/master/leetcode/src/main/java/com/easy/leetcode/Sub34.java)|中等|数组、二分查找
|22|[56 合并区间](https://github.com/smltq/spring-boot-demo/blob/master/leetcode/src/main/java/com/easy/leetcode/Sub56.java)|中等|数组、排序
|23|[421 数组中两个数的最大异或值](https://github.com/smltq/spring-boot-demo/blob/master/leetcode/src/main/java/com/easy/leetcode/Sub421.java)|中等|数组、位运算、字典树
|24|[1442 形成两个异或相等数组的三元组数目](https://github.com/smltq/spring-boot-demo/blob/master/leetcode/src/main/java/com/easy/leetcode/Sub1442.java)|中等|数组、数学

## 动态规划

Expand Down
75 changes: 75 additions & 0 deletions leetcode/src/main/java/com/easy/leetcode/Sub1442.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.easy.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
提示:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8
*/
public class Sub1442 {
public static void main(String[] args) {
int[] nums = {2, 3, 1, 6, 7};
Solution_1442 solution = new Solution_1442();
System.out.println("输出:" + solution.countTriplets(nums));
}
}

class Solution_1442 {
public int countTriplets(int[] arr) {
int len = arr.length;
int[] s = new int[len + 1];
for (int i = 0; i < len; i++) {
s[i + 1] = s[i] ^ arr[i];
}

int ans = 0;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
for (int k = j; k < len; k++) {
if (s[i] == s[k + 1]) {
++ans;
}
}
}
}
return ans;
}
}

0 comments on commit ff4bd7a

Please sign in to comment.