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. Spring4 MVC+Hibernate4+MySQL+Maven使用注解集成实例

    在本教程中,我们将使用基于注解的配置集成Spring和Hibernate. 我们将开发包含表单要求用户输入一个简单的CRUD为导向Web应用程序,使用Hibernate保存输入的数据到 MySQL 数 ...

  2. 这篇文章主要为大家详细介绍了jQuery密码强度验证控件使用详解的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    本文实例为大家分享了jQuery密码强度验证控件,供大家参考,具体内容如下 <html>   <head>     <meta http-equiv="Cont ...

  3. firefox(火狐)怎么关闭鼠标拖拽搜索

    工具-附加组件-卸载<附加组件管理器> 即可. 这玩意真心坑爹,起这个名字的人绝对是吃屎了,这名字怎么和鼠标拖拽混到一起的 !!!   关键字:火狐:鼠标:鼠标拖拽:鼠标手势:关闭

  4. webpack添加node_path不是('webpack' 不是内部或外部命令,也不是可运行的程序或批处理文件?)

    安装webpack 先决条件 开始之前,请确保安装了新的Node.js版本.目前的LTS是理想的起点.您可能会遇到与旧版本的各种问题,因为它们可能缺少webpack或相关软件包可能需要的功能. 请注意 ...

  5. 《TP5.0学习笔记---配置篇》

    参考博客:http://blog.csdn.net/self_realian/article/details/75045541

  6. node.js中的路由(url)初步

    1.建立n4_root.js var http = require('http'); var url = require('url'); //这是node.js中自带的var router = req ...

  7. resize和reserve

    resize改变的实际的大小,reserve是容量即capacity 如果先指定capacity的大小,可以防止内存的重新分配,我感觉在分配实际的内存的时候会餐口capacity的大小,如果事先指定容 ...

  8. using 关键字的使用

    using 关键字的使用主要分为两种类型:using declaration(using 声明)和using directive(using 命令): using 声明:引入特定名称空间中的一个成员. ...

  9. 【BZOJ5055】膜法师 树状数组

    [BZOJ5055]膜法师 Description 题目描述 在给定的维度序列a中, 求出所有满足i<j<k且ai<aj<ak的ai*aj*ak的和 即 ∑ (a_i*a_j* ...

  10. Python 编码(一)— Python3

    Unicode 什么是 Unicode 标准 unicode 标准 Unicode 为每个字符提供了一个独特的数字,并且跨平台.设备.应用或者编程语言都是通用的. -- 来自 http://unico ...