首页 > 代码库 > leetcode mock interview-two sum II

leetcode mock interview-two sum II

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