LeetCode136 Single Number


详细见:leetcode.com/problems/single-number


Java Solution: github

package leetcode;

/*
 * 	Given an array of integers, every element appears twice except for one.
 *  Find that single one.

	Note:
	Your algorithm should have a linear runtime complexity. 
		Could you implement it without using extra memory?
 */
public class P136_SingleNumber {
	public static void main(String[] args) {
		int[] arr = tools.Random随机生成器.A_生成一个随机数据(100, 0, 900);
		System.out.println(tools.Utils.LEETCODE_int_array_序列化_(arr));
//		int[] new_arr = new int[] {
//			315, 140, 472, 637, 280, 741, 416, 105, 754, 206, 75, 729, 496, 61, 444, 865, 707, 813, 686, 243, 531, 771, 463, 890, 133, 623, 873, 381, 422, 218, 9, 395, 807, 693, 23, 159, 736, 816, 154, 17, 138, 466, 339, 493, 586, 896, 798, 136, 690, 170, 367, 625, 801, 408, 827, 569, 5, 24, 57, 733, 400, 275, 533, 166, 230, 342, 672, 503, 750, 517, 529, 616, 818, 590, 213, 726, 608, 691, 645, 37, 747, 134, 516, 872, 505, 129, 661, 896, 70, 81, 137, 148, 83, 157, 520, 312, 701, 417, 446, 830,
//			315, 140, 472, 637, 280, 741, 416, 105, 754, 206, 75, 729, 496, 61, 444, 865, 707, 813, 686, 243, 531, 771, 463, 890, 133, 623, 873, 381, 422, 218, 9, 395, 807, 693, 23, 159, 736, 816, 154, 17, 138, 466, 339, 493, 586, 896, 798, 136, 690, 170, 367, 625, 801, 408, 827, 569, 5, 24, 57, 733, 400, 275, 533, 166, 230, 342, 672, 503, 750, 517, 529, 616, 818, 590, 213, 726, 608, 691, 645, 37, 747, 134, 516, 872, 505, 129, 661, 896, 70, 81, 137, 148, 83, 157, 520, 312, 701, 417, 446, 830
//		};
//		Solution s = new Solution();
//		System.out.println(s.singleNumber(new_arr));
	}
	/*
	 * 	1 ms
	 * 	41.62%
	 */
	static class Solution {
	    public int singleNumber(int[] nums) {
	    	if (nums == null || nums.length == 0) {
	    		return 0;
	    	}
	    	int ans = 0;
	    	for (int val : nums) {
	    		ans ^= val;
	    	}
	        return ans;
	    }
	}
}


C Solution: github

/*
    url: leetcode.com/problems/single-number
    AC 6ms 15.50%
*/

#include <stdio.h>
#include <stdlib.h>

int singleNumber(int* n, int nn) {
    int ans = 0, i = 0;
    for (i = 0; i < nn; i ++) {
        ans ^= n[i];
    }
    return ans;
}

int main() {
    int n[] = {1, 2, 2, 1, 3 ,4, 4};
    int nn = 7;
    printf("answer is %d\n", singleNumber(n, nn));
}



Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/single-number
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年5月18日
    @details:    Solution:  89ms 68.38%
'''
from _operator import xor

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        ans = 0
        for n in nums:
            ans = xor(ans, n)
        return ans;
    
if __name__ == "__main__":
    nums = [1, 2, 2, 1 ,4, 3, 3]
    print(Solution().singleNumber(nums))





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值