Lettcode_217_Contains Duplicate

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46271159

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears
at least twice in the array, and it should return false if every element is distinct.

思路:

(1)题意为判断给定的整数数组中是否包含相同的元素,如果包含则返回false,否则返回true。

(2)该题比较简单。通过构造一个Map来存放遍历得到的数值,如果在后续的遍历中从Map能够获取到value,则说明有重复的,返回false;否则继续遍历直到结束,返回true。

(3)详情见下方代码。希望对你有所帮助。

算法代码实现如下:

/**
 * @author liqqc
 */
import java.util.HashMap;
import java.util.Map;

public class ContainsDuplicate {
	public boolean containsDuplicate(int[] nums) {
		if (nums == null || nums.length == 0) {
			return false;
		}

		Map<Integer, Integer> maps = new HashMap<Integer, Integer>();
		for (int i = 0; i < nums.length; i++) {
			if (maps.get(nums[i]) == null) {
				maps.put(nums[i], nums[i]);
			} else if (maps.get(nums[i]) != null) {
				return true;
			}
		}
		return false;
	}
}
时间: 2024-08-09 23:48:11

Lettcode_217_Contains Duplicate的相关文章

Oracle 11gR2使用RMAN duplicate复制数据库

11g的RMAN duplicate 个人感觉比10g的先进了很多,10g需在rman备份的基础上进行复制,使用RMAN duplicate创建一个数据完全相同但DBID不同的数据库.而11g的RMAN duplicate 可通过Active database duplicate和Backup-based duplicate两种方法实现.Active database duplicate方式不需要先把目标数据库进行rman备份,只要目标数据库处于归档模式下即可直接通过网络对数据库进行copy,且

Find the Duplicate Number

 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify

LeetCode:Contains Duplicate - 判断数组内是否有重复元素

1.题目名称 Contains Duplicate(判断数组内是否有重复元素) 2.题目地址 https://leetcode.com/problems/contains-duplicate/ 3.题目内容 英文:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in

duplicate from active dg 的 run 脚本 sfile 为完整配置可能导致的一些错误

oracle 11g RMAN:Active Database Duplication for standby database 创建dg 命令解读 继上篇如果没有正确配置,理解duplicate from active  dg 的 run 脚本,就会出现以下错误: 1:路径不存在: Oracle instance shut down connected to auxiliary database (not started) RMAN-00571: =======================

oracle 11g 基于备份的rman duplicate

基于备份的rman duplicate 命令来创建standby database 前提条件: 保证原库数据库的备份,归档对于standby 端是完全可见的, 这里假设原库和目标端数据文件,日志文件等所有文件存放结构完全相同: 配置主库,备库静态监听,主库处于归档模式,和force logging 1.对主数据库进行必要的更改. a. 启用 force logging. b. 如果没有密码文件,则创建密码文件. c. 创建备用 redo 日志. d. 修改参数文件,使其适用于 Dataguard

ios 中 使用自制framework导致 Duplicate symbol 的问题解决方法

使用第三方静态库的时候有时候要求在编译选项linker 中 other linker flag中加入 -ObjC 但如果自制的framework库工程中加入了-ObjC,在Demo工程中如果也加入-ObjC选项时,可能导致 duplicate sysbol的问题 duplicate symbol _OBJC_CLASS_$_ZHAlixPayResult in: /Users/zizhu/Library/Developer/Xcode/DerivedData/NtUniSdkHaiMaDemo-

RMAN DUPLICATE ADG DEMO

RMAN DUPLICATE ADG DEMO 生产环境谨慎使用,建议生产环境采用RMAN备份恢复的方式. 本演示案例所用环境:   primary standby OS Hostname pry std OS Version RHEL6.5 RHEL6.5 DB Version 11.2.0.4 11.2.0.4 db_name stephen stephen db_unique_name stephen standby service_names stephen standby instan

(leetcode题解)Contains Duplicate

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 题意给定一个数组,查找数组是否存在重复项

on duplicate key update简单使用

1.最近在做项目的时候,遇到这样的一个问题,就是我每做完一件事情,都要更新一下统计表,然而要更新统计表,就要根据主键去统计表里面去查询是否已经有这样的一条记录,如果有那么就更新,如果没有那么就插入一条记录,开始我就是这么干的,结果被老大给否决了,他说可以用on duplicate key update去做.下面就实际操作一下吧: 表结构: +-----------+---------+------+-----+---------+-------+| Field     | Type    | N