LeetCode - 540. Single Element in a Sorted Array
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.
Example 1:
Input: [1,1,2,3,3,4,4,8,8] Output: 2
Example 2:
Input: [3,3,7,7,10,11,11] Output: 10
class Solution { public int singleNonDuplicate(int[] nums) { if (nums == null) return 0; int i = 0, j = 1; while (i < nums.length-1 && j < nums.length) { if (nums[i] == nums[j]) { i+=2; j+=2; } else { return nums[i]; } } return nums[nums.length-1]; } }
作者:Pickle
声明:对于转载分享我是没有意见的,出于对博客园社区和作者的尊重一定要保留原文地址哈。
致读者:坚持写博客不容易,写高质量博客更难,我也在不断的学习和进步,希望和所有同路人一道用技术来改变生活。觉得有点用就点个赞哈。








浙公网安备 33010602011771号