博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:136. Single Number
阅读量:4040 次
发布时间:2019-05-24

本文共 1018 字,大约阅读时间需要 3 分钟。

LeetCode刷题:136. Single Number

原题链接:

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,1]

Output: 1
Example 2:

Input: [4,1,2,1,2]

Output: 4


算法设计

package com.bean.algorithm.basic;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.Map;public class SingleNumber {	public static int singleNumber(int[] nums) {		Map
map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { Integer num = map.get(nums[i]); map.put(nums[i], num != null ? num + 1 : 1); } return Collections.min(map.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey(); } public static void main(String[] args) { // TODO Auto-generated method stub int[] arrays=new int[] {4,1,2,1,2}; int ANSWER = singleNumber(arrays); System.out.println("ANSWER = "+ANSWER); }}

程序运行结果:

ANSWER = 4

 

转载地址:http://yitdi.baihongyu.com/

你可能感兴趣的文章
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>
没有路由器的情况下,开发板,虚拟机Ubuntu,win10主机,三者也可以ping通
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>
忽略图片透明区域的事件(Flex)
查看>>
忽略图片透明区域的事件(Flex)
查看>>
AS3 Flex基础知识100条
查看>>
Flex动态获取flash资源库文件
查看>>
flex中设置Label标签文字的自动换行
查看>>
Flex 中的元数据标签
查看>>
flex4 中创建自定义弹出窗口
查看>>