• Two Sum


    Given an array of integers, find two numbers such that they add up to a specific target number.

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

    You may assume that each input would have exactly one solution.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

    Subscribe to see which companies asked this question

    Show Tags
    Show Similar Problems
     这种题目就是注意转换下标和值
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class IndexData {
    public:
        IndexData(){}
        int index;
        int value;
    };
    /*
     * 1.生成一个vector,并对vector的 value 进行排序
     * 2.两个指针,left, right, left < right
     * 3.如果 vector[left][1] + vector[right][1] > value, right--;
     * 4.如果 vector[left][1] + vector[right][1] < value, left--;
     * 5.如果 == , 返回, 注意返回index 的大小
     *
     */
    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            vector<IndexData*> v_data;
            vector<int> res;
            size_t num_size = nums.size();
            size_t i = 0;
    
            for (; i<num_size; i++) {
                IndexData* d = new IndexData();
                d->index = i;
                d->value = nums[i];
                v_data.push_back(d);
            }
            sort(v_data.begin(), v_data.end(), cmp);
    
            int left = 0;
            int right = num_size - 1;
            while (left < right) {
                while ((left < right) && ((v_data[left]->value + v_data[right]->value) > target)) {
                    right--;
                }
    
                while ((left < right) && ((v_data[left]->value + v_data[right]->value) < target)) {
                    left++;
                }
    
                if ((v_data[left]->value + v_data[right]->value) == target) {
                    if(v_data[left]->index < v_data[right]->index) {
                        res.push_back(v_data[left]->index + 1);
                        res.push_back(v_data[right]->index + 1);
                    } else {
                        res.push_back(v_data[right]->index + 1);
                        res.push_back(v_data[left]->index + 1);
                    }
                    break;
                }
    
            }
            
            for (i=0; i<num_size; i++) {
                if (NULL != v_data[i]) {
                    delete v_data[i];
                    v_data[i] = NULL;
                }
            }
            
            return res;
    
        }
    
    private:
        static bool cmp(const IndexData* id1, const IndexData* id2) {
            return id1->value < id2->value;
        }
    
    };
    
    
    
    int main() {
    
        vector<int> v;
        v.push_back(3);
        v.push_back(2);
        v.push_back(4);
        Solution s;
    
        s.twoSum(v, 6);
        return 0;
    }
    class Solution(object):
    
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            res_list = []
            new_nums = list(enumerate(nums))
            new_nums.sort(key = lambda x:x[1])
            left = 0
            right = len(new_nums) - 1
            while left < right:
                while new_nums[left][1] + new_nums[right][1] > target and left < right:
                    right = right - 1
                while new_nums[left][1] + new_nums[right][1] < target and left < right:
                    left = left + 1
                if new_nums[left][1] + new_nums[right][1] == target and left < right:
                    index_1 = new_nums[left][0] + 1
                    index_2 = new_nums[right][0] + 1
                    if index_1 < index_2:
                        res_list.append(index_1)
                        res_list.append(index_2)
    
                    else:
                        res_list.append(index_2)
                        res_list.append(index_1)
    
                    left = left + 1
                    right = right - 1
    
            return res_list
  • 相关阅读:
    DB2原因码7,解决方法
    打包项目成war包并部署到服务器上,项目运行一直显示加载中
    解决js中对象中属性是数组中对应元素,不能使用点数组元素(.数组[i])来获取value值来循环,属性不能是数组元素array[i]的问题
    javaweb项目中jsp的from表单提交action内容与web.xml的servlet-mapping对应
    Collection迭代器Iterator的使用
    使用switch计算出某年某月某日是今年的第几天,输出一直是当月天数
    DB2添加联合主键
    您的主机中的软件中止了一个已建立的连接
    java中在构造方法中修改线程名,修改失败原因(现已修改成功)
    String字符串加号的作用与基本数据类型加号的作用的区别
  • 原文地址:https://www.cnblogs.com/SpeakSoftlyLove/p/5090326.html
Copyright © 2020-2023  润新知