leetcode287. Find the Duplicate Number
来源:互联网 发布:台州五轴编程工资 编辑:程序博客网 时间:2024/06/05 11:51
287. 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 the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.
解法
数组中的元素从1-n,适合做0-1位置的映射,改变对应映射位置的值,检测是否重复。
public class Solution { public int findDuplicate(int[] nums) { if (nums == null || nums.length == 0) { return -1; } int len = nums.length; for (int i = 0; i < len; i++) { int index = (nums[i] - 1) % len; if (nums[index] > len) { return index + 1; } else { nums[index] += len; } } return -1; }}
阅读全文
0 0
- leetcode287 : Find the Duplicate Number
- [leetcode287] Find the Duplicate Number
- leetcode287 Find the Duplicate Number
- leetcode287: Find the Duplicate Number
- leetcode287. Find the Duplicate Number
- leetcode287. Find the Duplicate Number
- leetcode287. Find the Duplicate Number
- leetcode287. Find the Duplicate Number
- Leetcode287 - Find the Duplicate Number(二分)
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Angular项目目录介绍
- 图像识别
- iOS应用审核
- Thymeleaf系列四 生成URL地址和表达式工具对象
- 海量数据高并发的访问技术解决方案
- leetcode287. Find the Duplicate Number
- WebSocket + Spring消息推送服务的快速实现
- 使用GPU
- Failover与Failback的区别
- 微服务架构的两大解耦利器与最佳实践
- 计算机相关岗位要求
- Nginx状态信息(status)配置及信息详解
- Tree(1)--树的基本概念
- Linux学习笔记二