Data Structure Array: Find the minimum distance between two numbers
http://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; int mindis(int arr[], int n, int x, int y) {
int pre = ;
while (arr[pre] != x && arr[pre] != y) pre++;
int i = pre + ;
int ans = n;
for (; i < n; i++) {
if (arr[i] != x && arr[i] != y) continue;
if (arr[i] != arr[pre]) ans = min(ans, i - pre);
pre = i;
}
return ans;
} int main() {
int arr[] = {, , , , , , , , , , , };
cout << mindis(arr, , , ) << endl;
return ;
}
Data Structure Array: Find the minimum distance between two numbers的更多相关文章
- Data Structure Array: Find the Missing Number
http://www.geeksforgeeks.org/find-the-missing-number/ 1. use sum formula, O(n), O(1) 2. use XOR, O(n ...
- Data Structure Array: Move all zeroes to end of array
http://www.geeksforgeeks.org/move-zeroes-end-array/ #include <iostream> #include <vector> ...
- Data Structure Array: Find if there is a subarray with 0 sum
http://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/ #include <iostream> #incl ...
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-tha ...
- Data Structure Array: Sort elements by frequency
http://www.geeksforgeeks.org/sort-elements-by-frequency-set-2/ #include <iostream> #include &l ...
- Data Structure Array: Maximum circular subarray sum
http://www.geeksforgeeks.org/maximum-contiguous-circular-sum/ #include <iostream> #include < ...
- Data Structure Array: Largest subarray with equal number of 0s and 1s
http://www.geeksforgeeks.org/largest-subarray-with-equal-number-of-0s-and-1s/ #include <iostream& ...
- Data Structure Array: Find the two numbers with odd occurrences in an unsorted array
http://www.geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/ #include ...
- Data Structure Array: Longest Monotonically Increasing Subsequence Size
http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/ #include < ...
随机推荐
- C#操作消息列队
首先安装消息队列MSMQ,在“计算机管理-服务和应用程序-消息队列-专用队列”中新建列队名称Demo: static void SendAndReceiveMsg() { MessageQueue m ...
- 【SpringMVC学习09】SpringMVC与前台的json数据交互
json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...
- PHP框架认识初步
PHP框架比較 CodeIgniter Codeigniter 相当轻量级.下载下来就能用, CI 的最大特点就是可扩展性非常强 你能够通过不改动源代码的方式 优雅的扩展差点儿全部的东西. think ...
- nginx实现某个页面http访问,其余全部跳转到https
全站https实现某个页面可以http访问,其余全部跳转到https,注意下面的location,如果不加root 配置 找不到这个文件的server { listen ; server_name w ...
- MySQL加入服务、设置password、改动password
修正:加入MySQL服务时,能够不带版本.也就说以下的全部MySQL57能够直接写成MySQL!希望大家注意. MySQL安装好之后,往往还须要再做一些设置! 1.加入MySQL服务: 输入命令cmd ...
- css 温故而知新 select-option 文字方向居右
对select-option使用text-align:right;是无效的. 正确的姿势是:direction: ltr; 另外值得一提的是,通常还需要配合一点padding来美化.
- [Hibernate开发之路](4)ID生成策略
一 对象关系数据库映射之Id 被映射的类必须定义相应数据库表主键字段.大多数类有一个JavaBeans风格的属性, 为每个实例包括唯一的标识. <id> 元素定义了该属性到数据库表主键字段 ...
- MySQL中in(常量列表)的执行计划
我们在写sql的时候,经常用到in,in后面跟一堆常量列表,如id.有人说in的效率很高,而有人说很低:有人说in能使用索引,还有人说in不能使用索引... 到底是一个怎样的情况呢?我们分析以下几种情 ...
- 跟我一起写 Makefile(三)[转]
原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) make 的运行—————— 一 ...
- Oracle集群和灾备解决方案介绍
Oracle本身有各种各样的解决方案,本文主要是对以下几种解决方案做一下简单的梳理. 1. Oracle Rac解决方案. Oracle Rac主要通过两台Oracle服务器来组件集群,提高Or ...