zoukankan      html  css  js  c++  java
  • LeetCode0523

    http://leetcode.com/onlinejudge#question_1

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int> &numbers, int target) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         
     7         vector<int> result;
     8         map<int, int> mymap;
     9         
    10         typedef vector<int>::iterator iter_type;
    11         typedef map<int, int>::iterator map_iter_type;
    12 
    13         map_iter_type map_iter;
    14         for(iter_type it = numbers.begin(); it != numbers.end(); ++it) {
    15             map_iter = mymap.find(target - *it);
    16             if (mymap.end() != map_iter) {
    17                 result.push_back(map_iter->second);
    18                 result.push_back(it-numbers.begin()+1);
    19                 return result;
    20             }
    21             mymap[*it] = it-numbers.begin()+1;
    22         }
    23         
    24         /*
    25         vector<int> result;
    26         typedef vector<int>::iterator iter_type;
    27         for(iter_type it1 = numbers.begin(); it1 != numbers.end(); ++it1) {
    28             for(iter_type it2 = it1+1; it2 != numbers.end(); ++it2) {
    29                 if(target == *it1 + *it2) {
    30                     result.push_back(it1-numbers.begin()+1);
    31                     result.push_back(it2-numbers.begin()+1);
    32                     break;
    33                 }
    34             }        
    35         }
    36         
    37         return result;
    38         */
    39     }
    40 };
    View Code
  • 相关阅读:
    初始JSON
    JS异步加载的三种方式
    JS之事件
    关于null == 0?返回false的问题
    JS之类型转换
    金融(一)
    使用var声明的变量 和 直接赋值并未声明的变量的区别
    POJ2594 Treasure Exploration
    POJ1422 Air Raid
    Codevs1922 骑士共存问题
  • 原文地址:https://www.cnblogs.com/tangr206/p/3095279.html
Copyright © 2011-2022 走看看