class Solution {
public static int[] twoSum(int[] nums, int target) {
int left = 0;
int[] arr = new int [2];
for(;left < nums.length;++left){
int right = left+1;
while(right<nums.length){
if(nums[left] + nums[right] != target){
right++;
}else{
return new int[]{left,right};
}
}
}
return new int[0];
}
public static void main(String[] args) {
int[] testArr = {3,2,4};
int[] toArr = new int [2];
Solution Test = new Solution();
int target = 6;
toArr = Test.twoSum(testArr,target);
for(int i:toArr){
System.out.printf("%d\t", i);
}
}
}
两数之和的思路
最新推荐文章于 2024-10-08 20:32:22 发布