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;
                }
            }
        }
    };
    

      

  • 相关阅读:
    Jmeter压测
    .NET .Core 选择日志框架
    边缘控制器在边缘计算中的作用
    为何选择NB-IOT,NB-IOT的特点是什么
    RS232协议是什么
    物联网在生活中的应用场景
    透传模块是什么 为何透传
    4G DTU相对于GPRS/3G DTU有什么优势
    MQTT协议和Modbus之间的区别是什么
    4G模块应该怎么选择
  • 原文地址:https://www.cnblogs.com/skycore/p/4001103.html
Copyright © 2011-2022 走看看