码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode mock interview-two sum II

时间:2016-10-07 23:38:46      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:

package com.company;


import java.util.LinkedList;
import java.util.List;

public class Main {

    public int[] twoSum(int[] numbers, int target) {
        int i = 0, j = numbers.length - 1;
        int []ret = new int[2];
        while (i < j) {
            if (numbers[i] + numbers[j] < target) {
                i++;
            }
            else if (numbers[i] + numbers[j] > target) {
                j--;
            }
            else {
                ret[0] = i+1;
                ret[1] = j+1;
                break;
            }
        }
        return ret;
    }



    public static void main(String[] args) {
        // write your code here
        System.out.println("Hello");

        int []numbers = {2, 7, 11, 15};
        int target = 9;
        Main mn = new Main();
        int[] ret = mn.twoSum(numbers, target);
        System.out.printf("ret: %d, %d\n", ret[0], ret[1]);

    }
}

 

leetcode mock interview-two sum II

标签:

原文地址:http://www.cnblogs.com/charlesblc/p/5936948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!