From bc935f19217f307ddec1fce6c5c00d66d8244e07 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Sun, 16 May 2021 23:16:22 +0800 Subject: [PATCH] =?UTF-8?q?421:=E6=95=B0=E7=BB=84=E4=B8=AD=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=9A=84=E6=9C=80=E5=A4=A7=E5=BC=82=E6=88=96?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/MaximumXorOfTwoNumbersInAnArray.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/leetcode/editor/cn/MaximumXorOfTwoNumbersInAnArray.java b/src/main/java/leetcode/editor/cn/MaximumXorOfTwoNumbersInAnArray.java index 4e2e744..9bc5bf3 100644 --- a/src/main/java/leetcode/editor/cn/MaximumXorOfTwoNumbersInAnArray.java +++ b/src/main/java/leetcode/editor/cn/MaximumXorOfTwoNumbersInAnArray.java @@ -71,25 +71,23 @@ public class MaximumXorOfTwoNumbersInAnArray { //leetcode submit region begin(Prohibit modification and deletion) class Solution { public int findMaximumXOR(int[] nums) { - int x = 0; - for (int k = 30; k >= 0; k--) { + int result = 0; + for (int i = 30; i >= 0; i--) { Set set = new HashSet<>(); - boolean found = false; - + boolean isHave = false; for (int num : nums) { - set.add(num >> k); - if (set.contains(((x << 1) + 1) ^ (num >> k))) { - found = true; + set.add(num >> i); + if (set.contains(((result << 1) + 1) ^ (num >> i))) { + isHave = true; break; } } - - x <<= 1; - if (found) { - x++; + result <<= 1; + if (isHave) { + result++; } } - return x; + return result; } } //leetcode submit region end(Prohibit modification and deletion)