540:有序数组中的单一元素
This commit is contained in:
parent
c69a7f02a6
commit
b92d989a2a
@ -0,0 +1,59 @@
|
||||
//给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入: nums = [1,1,2,3,3,4,4,8,8]
|
||||
//输出: 2
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入: nums = [3,3,7,7,10,11,11]
|
||||
//输出: 10
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= nums.length <= 105
|
||||
// 0 <= nums[i] <= 105
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶: 采用的方案可以在 O(log n) 时间复杂度和 O(1) 空间复杂度中运行吗?
|
||||
// Related Topics 数组 二分查找
|
||||
// 👍 256 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//540:有序数组中的单一元素
|
||||
class SingleElementInASortedArray {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new SingleElementInASortedArray().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int singleNonDuplicate(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i = i + 2) {
|
||||
if (i + 1 == nums.length || nums[i] != nums[i + 1]) {
|
||||
return nums[i];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<p>给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [1,1,2,3,3,4,4,8,8]
|
||||
<strong>输出:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [3,3,7,7,10,11,11]
|
||||
<strong>输出:</strong> 10
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><meta charset="UTF-8" /></p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong> 采用的方案可以在 <code>O(log n)</code> 时间复杂度和 <code>O(1)</code> 空间复杂度中运行吗?</p>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li></div></div>\n<div><li>👍 256</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user