There are N integers in an array A. All but one integer occur in pairs. Your task is to find out the number that occurs only once.

Input Format

The first line of the input contains an integer N indicating number of integers. 
The next line contains N space separated integers that form the array A.

Constraints

1 <= N < 100 
N % 2 = 1 ( N is an odd number ) 
0 <= A[i] <= 100, ∀ i ∈ [1, N]

Output Format

Output S, the number that occurs only once.


常见的题:数组中除了一个数,其他数都是成对出现的。要求找出只出现了一次的这个数。

利用a xor a = 0 和 0 xor a = a这两个公式,设置一个数answer初始化为0,然后依次和数组中每个数异或,最后answer中存储的就是答案了。

代码如下:

 import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*; public class Solution {
static int lonelyinteger(int[] a) {
int answer = 0;
for(int i = 0;i < a.length;i++)
answer = answer ^ a[i];
return answer; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res; int _a_size = Integer.parseInt(in.nextLine());
int[] _a = new int[_a_size];
int _a_item;
String next = in.nextLine();
String[] next_split = next.split(" "); for(int _a_i = 0; _a_i < _a_size; _a_i++) {
_a_item = Integer.parseInt(next_split[_a_i]);
_a[_a_i] = _a_item;
} res = lonelyinteger(_a);
System.out.println(res); }
}

【HackerRank】Lonely Integer的更多相关文章

  1. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  2. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  3. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  4. 【leetcode】Reverse Integer

    题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...

  5. 【LeetCode】273. Integer to English Words

    Integer to English Words Convert a non-negative integer to its english words representation. Given i ...

  6. 【LeetCode】12. Integer to Roman 整型数转罗马数

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  7. 【leetcode】12. Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  8. 【转】【java】论integer是地址传递还是值传递

    转自:http://www.tuicool.com/articles/AraaQbZ 论integer是地址传递还是值传递 Integer 作为传参的时候是地址传递 , 可以参考如下例子,在程序刚启动 ...

  9. 【LeetCode7】Reverse Integer★

    题目描述: 解题思路: 反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法. Java代码: 方法一: 判断溢出方法:在执行完int newResult=result*10+tail语句后, ...

随机推荐

  1. C++之类的静态成员变量和静态成员函数

    static静态成员函数 在类中.static 除了声明静态成员变量,还能够声明静态成员函数. 普通成员函数能够訪问全部成员变量.而静态成员函数仅仅能訪问静态成员变量. 我们知道.当调用一个对象的成员 ...

  2. Sphinx 安装与使用(2)-- 配置Coreseek

    1.必须先关闭守护进程才能做其他的操作(第一次启动不需要这一步) /usr/local/coreseek/bin/searchd --config /usr/local/coreseek/etc/te ...

  3. mySQL 开启事件存储过程

    怎样在Navicat中设置,是数据库按照记录中的日期更新状态字段 其实这个很常用,比如你网站里的某条记录的日期——比如说数据库中某条活动记录的审核日期字段已经过期,亦即当前时间已经超过审核日期,那么定 ...

  4. How tomcat works学习笔记

    最近在看Tomcat的源码, 所以找了一本相关的书籍<How tomcat works>. 博客内容多为 学习该书时所记录的笔记.(如有侵权行为,请联系我:eviltomorrow@163 ...

  5. echarts Y轴刻度保留几位小数

    yAxis: [ { type: 'value', name: '雨量(mm)', nameLocation: 'start', inverse: true, axisLabel: {         ...

  6. knockout Ajax异步无刷新分页 Demo +mvc+bootstrap

    最近工作中web客户端需要用到knockout,在此记录下一些Demo,以后用到的时候查找起来方便.也希望给新入门的knockout使用者一点经验.knockout官方文档.这儿是一个使用knocko ...

  7. yii rule

    https://blog.csdn.net/ljfrocky/article/details/46373691 http://www.yiichina.com/tutorial/997 http:// ...

  8. 提高打开Android本地文档的速度

    非常多Android开发人员在參考Android官方API时,都有一个令人头疼的问题:打开一个index.html平均都须要几分钟甚至更长.尤其是在打开API 8以上的版本号的时候.难道是网速不够好? ...

  9. Windows10环境vagrant+VirtualBox虚拟机无法创建私有网络的解决方案。

    报错信息 ==> default: Clearing any previously set network interfaces...There was an error while execu ...

  10. spoj 10628

    http://www.spoj.com/problems/COT/ 树上第k小元素 LCA + 可持久化线段树 每个新的版本都是由其父亲版本转化而来. #include <cstdio> ...