首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。