首页 > 其他 > 详细

LeetCode 137. Single Number II

时间:2016-02-04 02:06:41      阅读:180      评论:0      收藏:0      [点我收藏+]

再来一道,刷简单题。

?

137. Single Number II

?

Given an array of integers, every element appears?three?times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

?

题目思路和136一致,只需要将步长设为3即可,分析过程见我blog

http://leonard1853.iteye.com/blog/2275626
??
public class Solution {
    public int singleNumber(int[] nums) {
        Arrays.sort(nums);
        
        int index = 0;
        while(index < nums.length - 2) {
            if (nums[index] - nums[index + 1] == 0 && nums[index] - nums[index + 2] == 0) {
                index += 3;
            } else {
                return nums[index];
            }
        }
        return nums[index];
    }
}

?

?

LeetCode 137. Single Number II

原文:http://leonard1853.iteye.com/blog/2275633

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!