package chen1121;import java.util.Arrays;/*** @author: ChenforCode* @date: 2018年11月21日 13:58* @description: 传入一个数组和一个target,计算出哪两个元素相加等于target,返回一个index数组*/public class TwoSum {public int[] twoSum(int[] nums, int target) {int[] result = new int[2];for (int i = 0; i < nums.length - 1; i++) {for (int j = i + 1; j < nums.length; j++) {if (nums[i] + nums[j] == target) {result[0] = i;result[1] = j;}}}return result;}public static void main(String[] args) {int[] prob = {2, 7, 11, 5};TwoSum twoSum = new TwoSum();System.out.println(Arrays.toString(twoSum.twoSum(prob, 9)));}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。