905. Sort Array By Parity (Easy)#

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

Example 1:

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. Note: 1 <= A.length <= 5000
0 <= A[i] <= 5000

solution##

我的解法

class Solution {
public int[] sortArrayByParity(int[] A) {
int i = 0, j = A.length - 1; int temp;
while (i < j)
{
if (A[i] % 2 != 0)
{
while (A[j] % 2 != 0 & i < j)
j--;
if (i < j)
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
j--;
}
}
i++;
}
return A;
}
}

大佬的解法

    public int[] sortArrayByParity(int[] A) {
for (int i = 0, j = 0; j < A.length; j++)
if (A[j] % 2 == 0) {
int tmp = A[i];
A[i++] = A[j];
A[j] = tmp;;
}
return A;
}

reference

https://leetcode.com/problems/sort-array-by-parity/discuss/170734/C%2B%2BJava-In-Place-Swap

总结##

此题主要考察数组相关的知识点。我的思路借鉴了快速排序的思想。首先设置两个计数器i与j分别记录数组的开头下标和结尾下标,然后用一个while循环从数组左端开始遍历数组,如果A[i]为奇数,则用一个while循环从数组右端开始遍历数组,直到A[j]为偶数(注意i<j)则跳出循环。假如此时j处的元素为偶数,如果i<j,就交换A[i]和A[j],并将j++,否则什么都不做。最后,当外层循环结束后,将A返回。

大佬的解法是将数组中的偶数与前面的奇数交换,即当遍历数组时,遇到奇数就跳过,遇到偶数就与前面的奇数相交换。

Notes:

1.此类数组问题最好只用题目给定的数组解决问题,如无必要,不要用额外的数组来存储中间结果。比如此题,如果新建一个int数组来存储结果,虽然也可以做出来,但是就失去了题目想要考察的技巧了。

961. N-Repeated Element in Size 2N Array (Easy)

In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.

Return the element repeated N times.

Example 1:

Input: [1,2,3,3]
Output: 3 Example 2: Input: [2,1,2,5,3,2]
Output: 2 Example 3: Input: [5,1,5,2,5,3,5,4]
Output: 5 Note: 4 <= A.length <= 10000
0 <= A[i] < 10000
A.length is even

solution##

我的解法

class Solution {
public int repeatedNTimes(int[] A) {
Set<Integer> s = new HashSet<Integer>();
for (int i = 0; i < A.length / 2 + 2; i++)
{
if (!s.add(new Integer(A[i])))
return A[i];
}
return -1;
}
}

大佬的解法,虽然有缺陷,但思想值得学习

//法一,利用数组数字充当数组下标,有意思。
//O(N) time, O(N) space
public int repeatedNTimes(int[] A) {
int[] count = new int[10000];
for (int a : A)
if (count[a]++ == 1)
return a;
return -1;
} //法二,将数组中的某一个数与其前面的两个数作比较,看是否相等。是否可以增加比较的数字个数,或许最好前后比较。
//O(N) time,O(1) space
public int repeatedNTimes(int[] A) {
for (int i = 2; i < A.length; ++i)
if (A[i] == A[i - 1] || A[i] == A[i - 2])
return A[i];
return A[0];
} //法三,随机挑选数组中的两个数字作比较
//O(4) time, O(1) space
public int repeatedNTimes(int[] A) {
int i = 0, j = 0, n = A.length;
while (i == j || A[i] != A[j]) {
i = (int)(Math.random() * n);
j = (int)(Math.random() * n);
}
return A[i];
}

reference:

https://leetcode.com/problems/n-repeated-element-in-size-2n-array/discuss/208563/JavaC%2B%2BPython-O(1)-Solution

总结##

此题思路很多。我的思路是构建一个HashSet,然后遍历数组,将数组里面的int值转为Integer后加到HashSet集合里面,如果值已经加到HashSet里面了,则就会返回false,此时直接返回当前的int数值。若遍历完毕,还没找到,则返回-1。

Notes:

1.java集合类只能存放引用类型,所以此题得将int转化为Integer后再放入集合。比如,Integer a = new Integer(A[i]);

2.数组里面的值(int)也可以当新的数组的下标,这种做法可以用来对数字计数;

LeetCode--Sort Array By Parity && N-Repeated Element in Size 2N Array (Easy)的更多相关文章

  1. 【Leetcode_easy】961. N-Repeated Element in Size 2N Array

    problem 961. N-Repeated Element in Size 2N Array solution: class Solution { public: int repeatedNTim ...

  2. 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  3. LeetCode 961. N-Repeated Element in Size 2N Array

    In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...

  4. LeetCode 961 N-Repeated Element in Size 2N Array 解题报告

    题目要求 In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is re ...

  5. 116th LeetCode Weekly Contest N-Repeated Element in Size 2N Array

    In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...

  6. 【leetcode】961. N-Repeated Element in Size 2N Array

    题目如下: In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is r ...

  7. [Swift]LeetCode961. 重复 N 次的元素 | N-Repeated Element in Size 2N Array

    In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...

  8. LC 961. N-Repeated Element in Size 2N Array【签到题】

    In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...

  9. LeetCode 961. N-Repeated Element in Size 2N Array (重复 N 次的元素)

    题目标签:HashMap 题目给了我们一个size 为 2N 的int array,其中有 N + 1 个唯一的 数字,让我们找出那个重复的数字. 利用hashset,把每一个数字存入,一旦发现有重复 ...

随机推荐

  1. lua使用笔记2:Linux 中安装php的lua扩展

    安装lua扩展的前提是lua已经安装好,如果没有安装,参照 1.http://pecl.php.net/package/lua 下载lua扩展 或者Linux下直接输入 wget http://pec ...

  2. 快速搭建网站信息库(小型Zoomeye)

    前言:本来是不想重复造车轮的,网上资料有开源的fofa,和一些设计.有的架设太复杂了,好用东西不会用,整个毛线.还有的没有完整代码. 设计方案:    测试平台:windows    测试环境:php ...

  3. python 工具链 包管理工具 pip

    Installation mac下可以采用 brew,easy_install(python自带)等方式安装. centos下可以采用yum,easy_install等方式安装. 但是上面两种方式在系 ...

  4. 基于 HTML WebGL 的会展中心智能监控系统

    前言 随着近几年物联网.万物互联等诸多概念的大行其道,智慧城市的概念也早已经被人们耳熟能详,而作为城市的组成部分,智慧建筑也是重中之重,智慧园区,智慧小区等也如雨后春笋般的相继出现. 智慧建筑是指通过 ...

  5. 进阶 Linux基本命令-1

    vmware三种网络模式1,桥接虚拟机直接连接外网,局域网.宿主机电脑不提供路由. 2,NAT网络地址转换,家庭网 3,host only 只能和宿主电脑打交道 Linux命令形式 命令 +[参数]+ ...

  6. Jmeter:运行报:Error occurred starting thread group :线程组, error message:Invalid duration 0 set in Thread Group:线程组, see log file for more details

    最近在用jmeter做压测,上周五压测的脚本,今天早晨结束后. 点击同样的脚本,运行就报Error occurred starting thread group :线程组, error message ...

  7. PHP中的数据库操作

    PDO project data object 连接到数据库 $db=new PDO("mysql:dbname=database;host=sever","userna ...

  8. NC使用练习之通达OA-2017版本漏洞复现后续

    利用上一篇通达OA的漏洞环境,练习NC工具的使用. 步骤: 1.本机启动nc.exe监听端口: 确认端口是否成功监听成功: 2.用冰蝎将nc.exe上传至目标机: 3.用命令行在目标机启动nc.exe ...

  9. Linux磁盘修复命令----fsck

    使用fsck命令修复磁盘时 一定要进入单用户模式去修复 语 法fsck.ext4[必要参数][选择参数][设备代号] 功 能fsck.ext4 命令: 针对ext4型文件系统进行检测 参数  -a 非 ...

  10. Linux系统管理第一二三四章 系统管理 目录和文件管理 安装及管理程序 账号管理

    命令 功能 序号 第一章   cd 切换目录 1 stat 查看文件状态信息 2 cp 复制   -f -i -p -r 3 du 统计磁盘的大小 4 find 精细查找文件和目录 5 help 帮助 ...