1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
找出数组中的单身狗;
1. OddOccurrencesInArray
Find value that occurs in odd number of elements. A non-empty zero-indexed array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the elements at indexes 0 and 2 have value 9,
the elements at indexes 1 and 3 have value 3,
the elements at indexes 4 and 6 have value 9,
the element at index 5 has value 7 and is unpaired.
Write a function: class Solution { public int solution(int[] A); } that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element. For example, given array A such that: A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the function should return 7, as explained in the example above. Assume that: N is an odd integer within the range [1..1,000,000];
each element of array A is an integer within the range [1..1,000,000,000];
all but one of the values in A occur an even number of times.
Complexity: expected worst-case time complexity is O(N);
expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
package com.code; import java.util.Arrays; public class Test02 { public int solution(int[] A) {
// write your code in Java SE 8
int size = A.length;
if(size%2==0){
return -1;
}
//System.out.println(Arrays.toString(A));
Arrays.sort(A);
//System.out.println(Arrays.toString(A));
if(size==1){
return A[0];
}
if(A[0]!=A[1]){
return A[0];
}
if(A[size-1] != A[size-2]){
return A[size-1];
}
for(int i=2;i<size-1;i=i+2){
if(A[i]!=A[i+1]){
return A[i];
}
}
return 0;
} public static void main(String[] args) {
Test02 t02 = new Test02();
int[] a = {9,3,9,3,5}; System.out.println(t02.solution(a));
int [] b = {1,1,2,2,3,4,4}; System.out.println(t02.solution(b)); }
}
1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.的更多相关文章
- [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- 剑指Offer:找出数组中出现次数超过一半的元素
题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...
- 找出数组中出现奇数次的元素<异或的应用>
点击打开链接:百度面试题之找出数组中之出现一次的两个数(异或的巧妙应用) 题目描述|:给定一个包含n个整数的数组a,其中只有一个整数出现奇数次,其他整数都出现偶数次,请找出这个整数 使用异或操作,因为 ...
- 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...
- 找出数组中最大值and索引
找出数组中的最大值和和最大值的索引位置..... 第一中方法: /** * 找出数组中最大值和最大值的索引 * @param args */ public static void main(Strin ...
- 剑指offer:1.找出数组中重复的数(java版)
数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...
- 【Java】 剑指offer(1) 找出数组中重复的数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...
- python找出数组中第二大的数
#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出数组中第2大的数字 ''' def find_Second_large_ ...
- 剑指offer.找出数组中重复的数字
题目: 给定一个长度为 n 的整数数组 nums,数组中所有的数字都在 0∼n−1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...
随机推荐
- 19 C#循环语句的跳过和中断 continue和break
在C#的循环语句中,有的时候我们希望跳过其中某个循环,有时我们希望当某个条件满足时,直接终止整个循环.C#为我们提供了 continue;和break;语句. continue和break的用法一样, ...
- RS485通信和Modbus协议(转)
转自:http://www.51hei.com/bbs/dpj-23230-1.html 在工业控制.电力通讯.智能仪表等领域,通常情况下是采用串口通信的方式进行数据交换.最初采用的方式是RS232接 ...
- Spring:(三) --常见数据源及声明式事务配置
Spring自带了一组数据访问框架,集成了多种数据访问技术.无论我们是直接通过 JDBC 还是像Hibernate或Mybatis那样的框架实现数据持久化,Spring都可以为我们消除持久化代码中那些 ...
- Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解
步骤1.破解包下载地址:https://gitee.com/pengzhile/MyBatisCodeHelper-Pro-Crack/releases 步骤2.下载:Intellij IDEA p ...
- SugarCRM安装过程——PHP文件上传限制问题
找到D:\xampp\php目录下,php文件中的php.ini文件,用写字板打开: 1.查找post_max_size,指通过表单POST给PHP的所能接收的最大值,包括表单里的所有值,默认为8M, ...
- error C2143: syntax error : missing ';' before '}'
今天弄Tab控件,干了一件非常愚蠢的事,没有去声明头文件.这也是今天要记录的问题,提示如下各种 前面一个符号是错误的.如果初学者遇到,算作一个提示,记得声明新类的.h 头文件 标签空间再进一步.cpp ...
- 03Oracle Database 物理结构,逻辑结构
Oracle Database 物理结构,逻辑结构 Oracle Database 物理结构 数据文件 Oracle数据库有一个或多个物理的数据文件.数据库的数据文件包含全部数据库数据.逻辑数据物理地 ...
- ansible结合playbook批量部署war包项目上线
批量部署jenkins.war包实现上线 用于测试war包上线 [root~localhost]~#vim /etc/ansible/test.yml - hosts: test vars: ...
- 制作一个最小Linux系统
使用的是itop4412开发板(仅记录个人的学习回顾,如有不当之处欢迎指出) ---------致谢 准备:busybox软件.uboot(一般和开发板配套).zImage(kernel内核).ram ...
- 将登录代码模块化,然后用add address接口来调用它,success!
登录模块,单独写成mylogin()方法,保存为modeltry119.py,然后接口的文件只写接口, 在post请求时,引用mylogin()返回的cookies就好了. requests.post ...