vector打印出符合X+Y=Z条件的X,Y元素下标-LeetCode1(C++)

在LeetCode编了一些不合格的代码


Problem

Two Num

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:

Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].

给定一组整数,将两个数的返回指数相加,使它们相加成一个特定的目标。您可以假设每个输入都有一个解决方案,您可能不会使用相同的元素两次。

Tips :稍后我们还可以看到LeetCode上关于这个问题推荐的几种方法。

对于这个问题,要在一组数中找到符合X+Y=target条件的X,Y两个值对应的下标,可以随机的在这组数中选取一个值,即尝试遍历这组数中的其它值。看是否符合X+Y=target.
符合即可return.

我最初想的比较简单,要用Vector容器。因为在上面的example 中 显示 return [0,1],可见返回结果是个数组类型.当然我们也可以定义一个大小为2的Array类型。但是我们要尽快的对C++的一些东西熟悉起来,我们可以用Vector容器类型充当它的返回值。

关于Vector容器

#include <iostream>
#include <Vector>

using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> value;

        for(int i=0;i<nums.size();i++){
            for(int j=nums.size()-1;j>i;j--){
            if(target==nums[j]+nums[i]){
                value.push_back(i);
                value.push_back(j);
            }
            }
        }
        return value;
    }
};

int main() {

    vector<int> b{1,2,4,5};
    Solution *sa=new Solution();

    for(auto i : sa->twoSum(b,9)){
        cout<<i<<"--";
    }
    cout<<endl;
    return 0;
}
输出结果:  2--3--

main函数中我们构造的数据,9->6时,输出结果:0--3--1--2--
因为问题中存在假设每个输入都有一个解决方案 所以对输出一个还是两个对我们都没有太大影响,问题的目的已经达到了。

上面是我的做法,在LeetCode上看完一些讨论之后,将他们的结果贴到下面。毕竟要写出好的代码,就要多看多想多对比。

Discuss

vector<int> twoSum(vector<int> &numbers, int target)
{
    //Key is the number and value is its index in the vector.
    unordered_map<int, int> hash;
    vector<int> result;
    for (int i = 0; i < numbers.size(); i++) {
        int numberToFind = target - numbers[i];

            //if numberToFind is found in map, return them
        if (hash.find(numberToFind) != hash.end()) {
                    //+1 because indices are NOT zero based
            result.push_back(hash[numberToFind] + 1);
            result.push_back(i + 1);            
            return result;
        }

            //number was not found. Put it in the map.
        hash[numbers[i]] = i;
    }
    return result;
}
//unordered_map(C++ 11特性)与map的不同之处,就是它的内部元素是无序的。


(java)
public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        map.put(nums[i], i);
    }
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (map.containsKey(complement) && map.get(complement) != i) {
            return new int[] { i, map.get(complement) };
        }
    }
    throw new IllegalArgumentException("No two sum solution");
}


(java)
public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (map.containsKey(complement)) {
            return new int[] { map.get(complement), i };
        }
        map.put(nums[i], i);
    }
    throw new IllegalArgumentException("No two sum solution");
}

OK 到这里我们也看到了不同的实现方法,我编写的方法,也是最容易想到的,但是时间复杂度上确不如他们。
下一篇文章是关于链表的合并,然后介绍一些链表上的操作,如果你还想看,留言吧

最后文章读到这里的你,有什么好的想法,建议,欢迎在评论区留言。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,357评论 0 33
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,494评论 11 349
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,084评论 19 139
  • 前言: 详细介绍: List:元素有放入顺序,元素可重复Map:元素按键值对存储,无放入顺序Set:元素无放入顺序...
    YBshone阅读 12,802评论 0 17
  • 呃……刚刚结束和BOSS隐晦的撕逼模式。 现在觉得累,具体事情不想再说了。大概就是觉得,发现自己是一个欲迎还拒的人...
    嫏嬛素素阅读 1,288评论 2 1

友情链接更多精彩内容