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. figure margins too large错误解决

    使用Rstudio,遇到下面这个错误: figure margins too large 这是因为界面右下角的“plot”窗口太小,显示不了,将右下角的窗口调大就能解决

  2. sed & awk & grep 专题( 鸟哥 )

    grep, sed 与 awk 相当有用 ! gerp 查找, sed 编辑, awk 根据内容分析并处理. awk(关键字:分析&处理) 一行一行的分析处理 awk '条件类型1{动作1}条 ...

  3. Servlet 打包部署

    默认情况下,Servlet 应用程序位于路径 <Tomcat-installation-directory>/webapps/ROOT 下,且类文件放在 <Tomcat-instal ...

  4. JavaScript 函数语法

    函数就是包裹在花括号中的代码块,前面使用了关键词 function: function functionname() { 这里是要执行的代码 } 当调用该函数时,会执行函数内的代码. 可以在某事件发生 ...

  5. Tomcat (7.0)数据源配置

    在Tomcat这个Java Web容器下通过配置DataSource(数据源)对象能够解决上面所述的问题. JDBC中的javx.sql.DataSource接口负责建立于数据库的连接.程序中直接从数 ...

  6. JS异步笔记

    Promise 最早接触异步是在.net中,当时还是比较流行使用基于控件的BackgroundWorker,其自身通过子线程的方式来异步处理一些情况,并且封装了一些功能与主线程通信.后来,开始使用Th ...

  7. SQL2008删除大量数据

    常见问题:工作中数据库难免产生大量的日志,而用户可能关心的只有最近一个月左右的,这些日志占用了服务器磁盘,还可能影响了服务运行效率.甚至在数据库迁移时更因为体积而带来巨大麻烦. 那么,在需要时,删除不 ...

  8. Yii GridView::widget

    GridView::widget文档 http://demos.krajee.com/grid GridView::widget([ 'dataProvider' => $dataProvide ...

  9. PHP 代码简洁之道 ( PHP Clean Code)

    https://laravel-china.org/topics/7774/the-conciseness-of-the-php-code-php-clean-code

  10. awk合并两个文件并显示

    问题: 答案: