zoukankan      html  css  js  c++  java
  • leetcode 2SUM

         struct vp{
            int value;
            int place;
        };
        bool comp(const struct vp a, const struct vp b){
            return a.value<b.value;
        }

    class Solution {
    public:

        

        
        vector<int> twoSum(vector<int> &numbers, int target) {
            vector<struct vp> v ;
            for(int i = 0; i < numbers.size(); ++i){
                struct vp tmp;
                tmp.value = numbers[i];
                tmp.place = i;
                v.push_back(tmp);
            }
            sort(v.begin(), v.end(),comp);
            for(int i = 0; i < v.size(); i++){
                for(int j = i+1; j < v.size(); j++){
                    if(v[i].value + v[j].value > target){
                        break;
                    }
                    if(v[i].value + v[j].value < target){
                        continue;
                    }
                    if(v[i].value + v[j].value == target){
                        vector<int> t ;
                        t.push_back(v[i].place+1);
                        t.push_back(v[j].place+1);
                        sort(t.begin(),t.end());
                        return t;
                    }
                }
            }
            return numbers;
        }
    };
  • 相关阅读:
    3、选择排序(最小值/最大值)
    2、冒泡排序
    1、快速排序
    Stream操作
    1. 两数之和
    Mysql修改字段类型修改
    获取节假日
    mysql 前缀 + 编号 补0
    一口气说出 6种@Transactional注解的失效场景
    Activiti最全入门教程(基于Eclipse插件开发)
  • 原文地址:https://www.cnblogs.com/jilichuan/p/3997932.html
Copyright © 2011-2022 走看看