Next Greater Element I
来源:互联网 发布:生物多样性数据库 编辑:程序博客网 时间:2024/05/17 03:41
It is the first that I have solved a problem without baidu the answer.
problem: You are given two arrays (without duplicates) nums1
and nums2
where nums1
’s elements are subset ofnums2
. Find all the next greater numbers fornums1
's elements in the corresponding places ofnums2
.
The Next Greater Number of a number x in nums1
is the first greater number to its right innums2
. If it does not exist, output -1 for this number.
1,错误一:输出是vector不是一个数
2,错误二:return -1 和return nums[t]不对,
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {返回的是一个整形vector
3,break位置要调式得知】
4,vector赋初值:http://www.cnblogs.com/gtt1025/p/5859585.html
#include<iostream>#include<string>#include<vector>using namespace std;vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {vector<int> rect;for (int i = 0; i<findNums.size(); i++){for (int j = 0; j<nums.size(); j++){if (findNums[i] == nums[j]){for (int t = j; t<nums.size(); t++){if (findNums[i] < nums[t]){rect.push_back(nums[t]);break;}if (t == nums.size() - 1)rect.push_back(-1);}}}}return rect;};int main(){vector<int> ilist, ilist2;ilist.push_back(1);ilist.push_back(3);ilist.push_back(4);ilist.push_back(2);ilist2.push_back(4);ilist2.push_back(1);ilist2.push_back(2);nextGreaterElement(ilist2, ilist);}
这种只能调试,不能输出vector结果,查vector输出:http://blog.csdn.net/lovemysea/article/details/5303897
return rect;
for (std::vector<int>::iterator m = rect.begin(); m != rect.end(); m++) //用迭代器的方式输出容器对象的值
{
cout << *m << endl;
}
这样没错,就是输出不了,过了return rect;后rect就空了;
原来return rect要放在最后;
完整代码:
#include<iostream>#include<string>#include<vector>using namespace std; vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {vector<int> rect;for (int i = 0; i<findNums.size(); i++){for (int j = 0; j<nums.size(); j++){if (findNums[i] == nums[j]){for (int t = j; t<nums.size(); t++){if (findNums[i] < nums[t]){rect.push_back(nums[t]);break;}if (t == nums.size() - 1)rect.push_back(-1);}}}}for (std::vector<int>::iterator m = rect.begin(); m != rect.end(); m++) //用迭代器的方式输出容器对象的值{cout << *m << endl;}return rect;};int main(){vector<int> ilist, ilist2;ilist.push_back(1);ilist.push_back(3);ilist.push_back(4);ilist.push_back(2);ilist2.push_back(4);ilist2.push_back(1);ilist2.push_back(2);nextGreaterElement(ilist2, ilist);}
结果:
第二部实施改写,printf输出
代码:
#include<iostream>#include<string>#include<vector>using namespace std;int nextGreaterElement(vector<int>& findNums, vector<int>& nums) {for (int i = 0; i<findNums.size(); i++){for (int j = 0; j<nums.size(); j++){if (findNums[i] == nums[j]){for (int t = j; t<nums.size(); t++){if (findNums[i] < nums[t]){printf( "%d\n", nums[t] );break;}if (t == nums.size() - 1)printf("%d\n", -1);}}}}return 90;};int main(){vector<int> ilist, ilist2;ilist.push_back(1);ilist.push_back(3);ilist.push_back(4);ilist.push_back(2);ilist2.push_back(4);ilist2.push_back(1);ilist2.push_back(2);nextGreaterElement(ilist2, ilist);}
结果:
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- LEETCODE--Next Greater Element I
- [LeetCode] Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- LeetCode:Next Greater Element I
- 【leetcode】Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- hadoop记录篇2-namenode高可用(HA)之QJM+NFS
- JDBC的DAO模式、ORM关系模型和Template
- 如何用CONSTRUCT 2制作简单的射击游戏
- 判断当前页面是由什么浏览器打开
- Java中处理异常的9个最佳实践
- Next Greater Element I
- 如何使用EL表达式?
- Linux基于mark的策略路由以及nf_conntrack RELATED
- 姚劲波 58同城创始人
- Mac如何查看JAVA_HOME
- 常用算法
- 本地制作yum源
- iOS开发中,代理的就是回调函数的使用
- Anaconda、Opencv和Tensorflow安装