287find-the-duplicate-number
某视面试官问了一道这样的题,1到N(N为正整数)共N个正整数,其中有一个数重复一次覆盖了另外一个数,比如:9,3,7,5,1,8,2,4,5,那么其中5重复一次,相当于覆盖了6,那么,请找出这个重复的数。
当时提供了HASH法,但人家不满意,直接pass了。
如果能想到循环链表式的数据结构,那么就很容易有思路了。// 9,3,7,5,1,8,2,4,5 error
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>//reverse
#include <vector>
using namespace std;
#define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
void f(int *s,int n){
, f=; // should n-1
do{
w = s[w]-;
f = s[s[f]-]-;
}while(w != f);
printf();
}
int main() {
;
,,,,,,};
f(arr, N);
}
class Solution {
public int findDuplicate(int[] nums) {
// Find the intersection point of the two runners.
];
];
do {
tortoise = nums[tortoise];
hare = nums[nums[hare]];
} while (tortoise != hare);
// Find the "entrance" to the cycle.
];
int ptr2 = tortoise;
while (ptr1 != ptr2) {
ptr1 = nums[ptr1];
ptr2 = nums[ptr2];
}
return ptr1;
}
}
287find-the-duplicate-number的更多相关文章
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 287. Find the Duplicate Number hard
287. Find the Duplicate Number hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...
- Leetcode Find the Duplicate Number
最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...
- Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode——Find the Duplicate Number
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
- 287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [LeetCode] 287. Find the Duplicate Number 解题思路
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- Find the Duplicate Number 解答
Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [Swift]LeetCode287. 寻找重复数 | Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
随机推荐
- 3.控制hive map reduce个数
参考: https://blog.csdn.net/wuliusir/article/details/45010129 https://blog.csdn.net/zhong_han_jun/arti ...
- servlet基础学习总结
Servlet的任务 1. 读取客户端发送的显示的数据,包括HTML表单和一些客户端程序的表单 2. 读取客户端发送的隐式的数据,包括cookies.媒体类型等 3. 处理数据并产生结果 4. ...
- 20162328蔡文琛 大二week07
20162328 2017-2018-1 <程序设计与数据结构>第7周学习总结 教材学习内容总结 树是非线性结构,其元素组织为一个层次结构. 树的度表示树种任意节点的最大子节点数. 有m个 ...
- 第二阶段每日站立会议Second Day
昨天我在手机端安装cpp后进行界面效果测试以及进一步完善 今天对图片显示的大小进行调整 遇到的问题:当图片太小时,显示一块灰色区域,不美观
- sprint站立会议
索引卡: 工作认领: 时间 ...
- 【CS231N】1、图像分类
一.知识点 1. 计算机识别物体面临的困难 视角变化(Viewpoint variation):同一个物体,摄像机可以从多个角度来展现. 大小变化(Scale variation):物体可视的大小通常 ...
- python learning IO.py
f = open('test.txt', 'r') # 'r' 表示只读 s = f.read() # 调用read()方法可以一次读取文件的全部内容,Python把内容读到内存,用一个str对象表示 ...
- 调整Linux的最大文件打开数
要调整一下Linux的最大文件打开数,否则squid在高负载时执行性能将会很低.另外,在Linux下面部署应用时,有时候会遇上 Socket/File:Can’t open so many files ...
- Android 布局类控件
Android提供6种布局类的控件:LinearLayout.TableLayout.GridLayout.FrameLayout.RalativeLayout.AbsoluteLayout 网上搜到 ...
- linux 负载均衡配置 keepalive lvs 使用nginx转发 CentOS7 搭建LVS+keepalived负载均衡
最近希望能够配置一下负载均衡,在虚拟机上面,但是网上找了很多资料很零散,对于不了解的人,很多不够详细,最近终于做好了,把具体的步骤写下来,方便各位网友查阅学习 这个实验需要安装nginx如果没有安装过 ...