更新時(shí)間:2022-11-21 09:48:31 來源:動力節(jié)點(diǎn) 瀏覽2071次
給定數(shù)組nums,目標(biāo)值target,在該數(shù)組中找出和為目標(biāo)值的那兩個(gè)整數(shù),并返回?cái)?shù)組的下標(biāo)
public class Test {
public static void main(String[] args) {
int[] arr = {23,34,67,77};
int ret[] = twoSum(arr, 100);
System.out.println(Arrays.toString(ret));
}
public static int[] twoSum(int[] nums,int target){
int flag[] = new int[2];
for(int i = 0;i < nums.length;i++){
for(int j = i + 1;j < nums.length;j++){
if(nums[i] + nums[j] == target){
flag[0] = i;
flag[1] = j;
return flag;
}
}
}
return null;
}
}
執(zhí)行截圖:
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743