From 81844b8b6e787d67c2b64d602dc53e415bb29b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Mon, 21 Mar 2022 22:42:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=A2=E8=AF=95=E9=A2=98=2017.04:=E6=B6=88?= =?UTF-8?q?=E5=A4=B1=E7=9A=84=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/MissingNumberLcci.java | 40 +++++++++++++++++++ .../cn/doc/content/MissingNumberLcci.md | 17 ++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/MissingNumberLcci.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/MissingNumberLcci.md diff --git a/src/main/java/leetcode/editor/cn/MissingNumberLcci.java b/src/main/java/leetcode/editor/cn/MissingNumberLcci.java new file mode 100644 index 0000000..c0b53c0 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/MissingNumberLcci.java @@ -0,0 +1,40 @@ +//数组nums包含从0到n的所有整数,但其中缺了一个。请编写代码找出那个缺失的整数。你有办法在O(n)时间内完成吗? +// +// 注意:本题相对书上原题稍作改动 +// +// 示例 1: +// +// 输入:[3,0,1] +//输出:2 +// +// +// +// 示例 2: +// +// 输入:[9,6,4,2,3,5,7,0,1] +//输出:8 +// +// Related Topics 位运算 数组 哈希表 数学 排序 👍 57 👎 0 + +package leetcode.editor.cn; + +//面试题 17.04:消失的数字 +public class MissingNumberLcci { + public static void main(String[] args) { + Solution solution = new MissingNumberLcci().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int missingNumber(int[] nums) { + int xor = nums.length; + for (int i = 0; i < nums.length; i++) { + xor ^= i ^ nums[i]; + } + return xor; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/MissingNumberLcci.md b/src/main/java/leetcode/editor/cn/doc/content/MissingNumberLcci.md new file mode 100644 index 0000000..cb820ca --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/MissingNumberLcci.md @@ -0,0 +1,17 @@ +

数组nums包含从0n的所有整数,但其中缺了一个。请编写代码找出那个缺失的整数。你有办法在O(n)时间内完成吗?

+ +

注意:本题相对书上原题稍作改动

+ +

示例 1:

+ +
输入:[3,0,1]
+输出:2
+ +

 

+ +

示例 2:

+ +
输入:[9,6,4,2,3,5,7,0,1]
+输出:8
+
+
Related Topics
  • 位运算
  • 数组
  • 哈希表
  • 数学
  • 排序

  • 👍 57
  • 👎 0
  • \ No newline at end of file