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 < ...
随机推荐
- ABP框架EF6链接Oracle数据库手动迁移
环境:VS2017 + ABP官方模板(不含Zero) +Oracle 11Gx64DB + Oracle 11Gx32Client(PLSQL工具访问) 一.Abp项目的下载以及运行 1.创建ab ...
- java jdk 环境变量设置
我的电脑点右键,选择“属性”,选择“高级”标签,进入环境变量设置,分别设置如下三个环境变量: 设置JAVA_HOME: 一是为了方便引用,比如,JDK安装在C:\jdk1.6.0目录里,则设置JAVA ...
- java 实现统计某段文字在内容中出现的次数
http://outofmemory.cn/code-snippet/815/java-zishutongji 一个api,位于apache.commons.lang.StringUtils类下的一个 ...
- java替换文本中所有的正则符号 Java问题通用解决代码
开发时遇到一个需求,需要对一段文本中的所有正则符号进行转义,不然使用split分割方法分割文本的话无效,想到用替换来做,全部替换正则符号为转义后的符号 贴java实现代码: 1.测试版 ...
- preloadjs实现网页资源预加载
<!doctype html> <html lang="zh"> <head> <title>PreloadJS的基础使用</ ...
- cmd命令速查手册
CMD命令速查手册ASSOC显示或修改文件扩展名关联AT 计划在计算机上运行的命令和程序ATTRIB 显示或更改文件属性BREAK 设置或清除扩展式 CTRL+C检查CACLS显示或修改文件的访问控制 ...
- 当半年不碰的webpack + vuejs项目重见天日时遇到的神坑!
半年前的Vuejs项目,由于webpack环境一直是1.0的(现在是webpack4.0),倒不是说不会升级或者不敢升级. 总而言之.在我再次启动这个项目时: npm run dev / npm ru ...
- hdu 4601 Letter Tree
不easy啊.. 一个小错误让我wa死了.找了一个晚上,怎么都找不到 最后是对拍代码找到的错误.发现当步数比較小的时候答案就是对的,比較大的时候就不正确了 想到一定是什么地方越界了.. . power ...
- [译]GLUT教程 - 移动镜头2
Lighthouse3d.com >> GLUT Tutorial >> Input >> Move the Camera II 本节的最后一个示例是回顾.现在我们 ...
- HDU 5273 区间DP
输入一组数,m次询问 问每一个询问区间的逆序数有多少 区间DP简单题 #include "stdio.h" #include "string.h" int dp ...