面试题 17.04:消失的数字
This commit is contained in:
parent
29ec6d9681
commit
81844b8b6e
40
src/main/java/leetcode/editor/cn/MissingNumberLcci.java
Normal file
40
src/main/java/leetcode/editor/cn/MissingNumberLcci.java
Normal file
@ -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)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
<p>数组<code>nums</code>包含从<code>0</code>到<code>n</code>的所有整数,但其中缺了一个。请编写代码找出那个缺失的整数。你有办法在O(n)时间内完成吗?</p>
|
||||||
|
|
||||||
|
<p><strong>注意:</strong>本题相对书上原题稍作改动</p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>[3,0,1]
|
||||||
|
<strong>输出:</strong>2</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>[9,6,4,2,3,5,7,0,1]
|
||||||
|
<strong>输出:</strong>8
|
||||||
|
</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>位运算</li><li>数组</li><li>哈希表</li><li>数学</li><li>排序</li></div></div><br><div><li>👍 57</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user