数组//两数之和 II - 输入有序数组

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。

函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2

说明:

  • 返回的下标值(index1 和 index2)不是从零开始的。
  • 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。

示例:

输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int start = 0;
        int end = numbers.length-1;
        int result[] = new int[2];
        while(start < end){
            int temp = numbers[start]+numbers[end];
            if(temp == target){
                result[0] = start+1;
                result[1] = end+1;
                return result;
            }else if(temp < target){
                int t = numbers[start];
                start++;
                while(numbers[start]==t)
                    start++;
            }else{
                int t = numbers[end];
                end--;
                while(numbers[end] == t)
                    end--;
            }
        }
        return result;
    }
}

 

vector<int>与vector<int>::iterator

vector<int>是声明向量容器;
例如 verctor<int> v,就是创建了一个名字叫v的向量容器。


vector<int>::iterator是定义向量迭代器
例如,vector<int>::iterator it  就是定义了一个名字叫it 的向量迭代器
for(it=v.begin();it!=v.end();it++)
     cout<<*it<<endl;
就把vector<int> 向量类型的向量V中的int数据都输出了

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        vector<int>::iterator t = numbers.end()-1;
        vector<int>::iterator i = numbers.begin();
        vector<int> res;
        while(1){
            if(target == *t+*i){
                res.push_back(i-numbers.begin()+1);
                res.push_back(t-numbers.begin()+1);
                return res;
            }
            if(target > *t+*i){
                i++;
            }
            else
                t--;
        }
    }
};

 

posted @ 2018-11-04 10:31  strawqqhat  阅读(89)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }