水王问题及扩展
来源:互联网 发布:瓷砖销售软件破解版 编辑:程序博客网 时间:2024/05/22 13:01
int findEle(int a[],int len){ int candidate; int nTimes=0; for(int i=0;i<len;i++) { if(nTimes==0) { candidate=a[i]; nTimes=1; } else { if(candidate==a[i]) nTimes++; else nTimes--; } }return candidate;}
用candidate[3]记录三个候选ID,用count[3]记录它们的累积次数,然后遍历整个ID列表,每处理一个ID,若与candidate[i]中的某一个相同,则count[i]++,若与三个都不同,则说明找到了四个互不相同的ID,将三个count[i]--,也就相当于“删除了四个不同ID”,若某一个count[i]==0,则更新之。
int nTimes[3]={1};int candidate[3]; candidate[0]=a[0]; candidate[1]=a[1]; candidate[2]=a[2]; for(int i=1;i<len;i++) { if(candidate[0]==a[i]) nTimes[0]++; else if(candidate[1]==a[i]) nTimes[1]++; else if(candidate[2]==a[i]) nTimes[2]++; else if(nTimes[0]==0) { candidate[0]=a[i]; nTimes[0]=1; } else if(nTimes[1]==0) { candidate[1]=a[i]; nTimes[1]=1; } else if(nTimes[2]==0) { candidate[2]=a[i]; nTimes[2]=1; } else { nTimes[0]--; nTimes[1]--; nTimes[2]--; } }
0 0
- 水王问题及扩展
- 编程之美--发帖水王及扩展问题
- 编程之美:水王问题及扩展
- LIS问题及扩展
- 数塔问题及扩展
- jmeter版本及扩展问题
- 编程之美 - 寻找灌水王及扩展问题
- 寻找发帖水王 扩展问题
- 次球称重的问题及扩展
- 扩展CListCtrl及需要注意的问题
- 最长公共子序列问题及扩展
- 八数码问题可解性及扩展
- 字符串是否包含问题及扩展
- 编程之美 寻找发帖水王 扩展问题
- 编程之美2.3寻找发帖水王扩展问题
- 编程之美 2.3 寻找发帖水王扩展问题
- 编程之美-程序改错及扩展问题
- 第二章、字符串是否包含及相关问题扩展
- Office 2010: outlook中设置邮件格式为HTML、RTF、纯文本
- ActiveMQ的持久化
- javascript指南-函数
- Spring Security学习与配置
- MySQL之索引的使用
- 水王问题及扩展
- select,poll,epoll区别
- select、poll、epoll的比较
- 产品学习:智能生产调度管理系统
- 诺基亚 N1:芬兰大厂打响重返消费者市场的第一枪
- hibernate.cfg.xml配置文件详解
- select poll epoll的区别
- 选择结构和循环结构
- ngxin的Module开发(2)