1.Two Sum

Given an array of integers, returnindicesof the two numbers such that they add up to a specific target.
给定一个integers类型的数组,找出相加等于目标值的两个数,返回他们的索引

You may assume that each input would haveexactly one solution, and you may not use thesame element twice.
默认所有的input输入有且只有一个解,且不可以用同一个元素自加

Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

举例:
给定数组为[2, 7, 11, 15],目标数字为9
因为数组第0项+数组第1项=9,所以返回[0, 1]

拟采用方法:
将数组各项与数组索引组合成结构体。
按照各项值对结构体数组快速排序
从结构体数组第0项往后遍历,直到value>target/2停止,对target-value用二分做查找,若找到则返回索引值
时间复杂度为O(nlogn)

如下为C语言初始版本

#include

#define MAX_SIZE 20480

struct num_infor{
    int value;
    int location;
}struct_array[MAX_SIZE];

void swap_struct(struct num_infor *array, int left, int right){
    int value_t = array[left].value;
    int locat_t = array[left].location;
    array[left].value = array[right].value;
    array[left].location = array[right].location;
    array[right].value = value_t;
    array[right].location = locat_t;
    return;
}

void qsort_struct(struct num_infor *array, int left, int right){
    if(left >= right){
        return;
    }
    int i = left;
    int j = right;
    int key_value = struct_array[left].value;
    int key_locat = struct_array[left].location;

    while(i < j){
        while(i < j && key_value <= struct_array[j].value){
            j--;
        }
        swap_struct(struct_array, i, j);
        while(i < j && key_value >= struct_array[i].value){
            i++;
        }
        swap_struct(struct_array, i, j);
    }
    struct_array[i].value = key_value;
    struct_array[i].location = key_locat;
    qsort_struct(array, left, i-1);
    qsort_struct(array, i+1, right);
    return;
}

int* twoSum(int* nums, int numsSize, int target){
    //init the struct array
    int i;
    int *res = (int *)malloc(sizeof(int) * 2);
    for(i = 0; i < numsSize; i++){
        struct_array[i].value = nums[i];
        struct_array[i].location = i;
    }
    
    //  sort the struct array
    qsort_struct(struct_array, 0, numsSize-1);
/* 
    for(i = 0; i < numsSize; i++){
        printf("%d---%d\n",struct_array[i].value, struct_array[i].location);
    } 
 */ 
    int min, max, mid;
    for(i = 0; i < numsSize; i++){
        if(struct_array[i].value > target/2){
            break;
        }
        min = i+1;
        max = numsSize - 1;
        while(min <= max){
            mid = (min + max) / 2;
            if(struct_array[i].value + struct_array[mid].value == target){
                res[0] = struct_array[i].location;
                res[1] = struct_array[mid].location;
                if(res[0] > res[1]){
                    i = res[0];
                    res[0] = res[1];
                    res[1] = i;
                }
                i = numsSize;
                break;
            }
            else if(struct_array[i].value + struct_array[mid].value > target){
                max = mid - 1;
            }
            else{
                min = mid + 1;
            }
        }
    }
    return res;
}

int main(){
    int nums[10] = {-1, -2, -3, -4, -5};
    int *answer = NULL;
    answer = twoSum(nums, 5, -8);
    printf("[%d, %d]\n", answer[0], answer[1]);
}
//参考自http://blog.csdn.net/gatieme/article/details/50596965

个人写的较为繁琐,后来在网络上看到了一个更好理解的解法
即定义一个n对n的map,遍历所有的数字,在map中寻找对应的target-nums[i]

class Solution {
public:
    vector twoSum(vector& nums, int target) {
        map mapping;
        vector result;
        for (int i = 0; i < nums.size(); i++)
        {
            mapping[nums[i]] = i;
            //将数组元素->角标的关系存入map中
        }
        for (int i = 0; i < nums.size(); i++)
        {
            int searched = target - nums[i];
            //查找对应的searched值
            if (mapping.find(searched) != mapping.end()
                && mapping.at(searched) != i)
            {
                result.push_back(i + 1);
                result.push_back(mapping[searched] + 1);
                break;
            }
        }
        return result;     
    }
};
选自 http://wiki.jikexueyuan.com/project/leetcode-book/00.html

你可能感兴趣的:(1.Two Sum)