• LeetCode 136


    Single Number

    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?

    1 public class Solution {
    2     public int singleNumber(int[] nums) {
    3         int result = 0;
    4         for(int i = 0 ; i<nums.length ; i++ ){
    5             result ^= nums[i];
    6         }
    7         return result;
    8     }
    9 }
     1 /*************************************************************************
     2     > File Name: LeetCode136.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Tue 10 May 2016 07:37:54 PM CST
     6  ************************************************************************/
     7  
     8 /*************************************************************************
     9     
    10     Single Number
    11     
    12     Given an array of integers, every element appears twice except for one. 
    13     Find that single one.
    14 
    15     Note:
    16     Your algorithm should have a linear runtime complexity. 
    17     Could you implement it without using extra memory?
    18 
    19     Subscribe to see which companies asked this question
    20 
    21  ************************************************************************/
    22 
    23 #include <stdio.h>
    24 
    25 int singleNumber( int* nums, int numsSize )
    26 {
    27     int ret = 0;
    28     
    29     int i;
    30     for( i=0; i<numsSize; i++ )
    31     {
    32         ret ^= nums[i];
    33     }
    34     return ret;
    35 }
    36 
    37 int main()
    38 {
    39     int nums[] = { 1, 2, 3, 3, 1 };
    40     int numsSize = 5;
    41     int ret = singleNumber( nums, numsSize );
    42     printf("%d
    ", ret);
    43 
    44     return 0;
    45 }
  • 相关阅读:
    不能执行已释放的Script的代码(ie错误)
    javascript数组
    Jquery遍历方法
    Jquery选择器汇总
    使用xmlHttprequest 发送异步请求(Ajax核心对象)
    不使用局部变量和for循环或其它循环打印出如m=19,n=2結果为2 4 8 16 16 8 4 2形式的串
    解决Js跨域访问的问题
    Oracle 第一天
    计算机图形学1——绪论
    数据库
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5428818.html
Copyright © 2020-2023  润新知