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的更多相关文章

  1. 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 ...

  2. Data Structure Array: Move all zeroes to end of array

    http://www.geeksforgeeks.org/move-zeroes-end-array/ #include <iostream> #include <vector> ...

  3. 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 ...

  4. 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 ...

  5. Data Structure Array: Sort elements by frequency

    http://www.geeksforgeeks.org/sort-elements-by-frequency-set-2/ #include <iostream> #include &l ...

  6. Data Structure Array: Maximum circular subarray sum

    http://www.geeksforgeeks.org/maximum-contiguous-circular-sum/ #include <iostream> #include < ...

  7. 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& ...

  8. 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 ...

  9. Data Structure Array: Longest Monotonically Increasing Subsequence Size

    http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/ #include < ...

随机推荐

  1. 获取web应用路径 // "/" 表示class 根目录

    /** * 获取web应用路径 * @Description : 方法描述 * @Method_Name : getRootPath * @return * @return : String * @C ...

  2. Solution to Triangle by Codility

    question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...

  3. ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同

    说来惭愧,接触WPF这么长时间了,今天在写自定义控件时遇到一个问题:运行界面中并没有显示自定义控件,经调试发现原来没有加载Themes中的Generic.xaml. 可是为什么在其他solution中 ...

  4. ASP.NET常见命名空间及其功能描述

    命名空间 |  功能描述 System |  包含CLR的基本类型和基类,定义了常用的值类型和引用类型,事件.接口.属性和异常处理等 System.Text | 包含用于文本处理的类,实现了不同编码方 ...

  5. 改变listview中item选中时文字的颜色

    摘要 当listview的某个item选中时,默认有个选中的高亮显示,如果你要自定义选中时的高亮显示效果,可以在listview中设置属性 android:listSelector="@dr ...

  6. 涛哥的Python工具箱之批量删除含指定字符串行

    我们在软件研发中不可避免的要用到大量的反复性的繁琐的工作,比如批量改动代码中接口的字符串.批量下载文件并又一次按规则命名.这些工作人工做特别累,尤其是对我这样的懒人来说. 对于一个出色的程序猿来说,反 ...

  7. Javascript属性constructor/prototype的底层原理

    在Javascript语言中,constructor属性是专门为function而设计的,它存在于每个function的prototype属性中. 这个constructor保存了指向function ...

  8. Nginx日志配置与切割

    访问日志主要记录客户端访问Nginx的每一个请求,格式可以自定义.通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息. Nginx中访问日志相关指令主要有两条,一条是 ...

  9. 403/you don't have the permission to access on this server

    Localhost/index.php出现 错误403 you don't have the permission to access on this server 现在已经解决,特将方法与大家分享. ...

  10. LeetCode_Minimum Depth of Binary Tree

    一.题目 Minimum Depth of Binary Tree My Submissions Given a binary tree, find its minimum depth. The mi ...