zoukankan      html  css  js  c++  java
  • [LeetCode]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

    解题方案:

    定义一个结构体,用来存储原数组中的值和位置,然后对原数组按值进行排序,得到新数组。对新数组按首尾向中间遍历。下面是该题的代码:

     1 struct number_and_location{
     2     int number;
     3     int location;
     4     bool operator < (const number_and_location& lhs)const {
     5         return number < lhs.number;
     6     }
     7 };
     8 
     9 class Solution {
    10 public:
    11     vector<int> twoSum(vector<int> &numbers, int target) {
    12         int len_of_numbers = numbers.size();
    13         vector<int> index_of_result(2, 0);
    14         vector<number_and_location> change_of_source;
    15         for(int i = 0; i < len_of_numbers; ++i){
    16             change_of_source.push_back((number_and_location){numbers[i],i});
    17         }
    18         sort(change_of_source.begin(),change_of_source.end());
    19         int j = len_of_numbers - 1;
    20         int i = 0;
    21         while ( j != i){
    22             if(change_of_source[i].number + change_of_source[j].number == target){
    23                 if(change_of_source[i].location < change_of_source[j].location){
    24                     index_of_result[0] = change_of_source[i].location + 1;
    25                     index_of_result[1] = change_of_source[j].location + 1;
    26                 }else {
    27                     index_of_result[0] = change_of_source[j].location + 1;
    28                     index_of_result[1] = change_of_source[i].location + 1;
    29                 }
    30                 break;
    31             }else if(change_of_source[i].number + change_of_source[j].number < target){
    32                 ++i;
    33             }else {
    34                 --j;
    35             }
    36         }
    37         return index_of_result;
    38     }
    39 };

     修改2015-10-02

    用哈希表

    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            unordered_map<int, int> index;
            vector<int> result;
            for (size_t i = 0; i < nums.size(); ++i) {
                if (index.find(target - nums[i]) == index.end()) {
                    index[nums[i]] = i;
                } else {
                    result.push_back(index[target - nums[i]] + 1);
                    result.push_back(i + 1);
                    
                    return result;
                }
            }
        }
    };
    

      

  • 相关阅读:
    第0课
    学前班-怎么看原理图
    LCD-裸机韦东山
    学前班
    专题8-Linux系统调用
    专题4-嵌入式文件系统
    网络编程 之 软件开发架构,OSI七层协议
    反射、元类,和项目生命周期
    多态、魔法函数、和一些方法的实现原理
    封装,接口,抽象
  • 原文地址:https://www.cnblogs.com/skycore/p/4001103.html
Copyright © 2011-2022 走看看