two sum in java code example
Example: two sum java
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] sol = new int[2];
if(nums.length == 2) {
sol[0] = 0;
sol[1] = 1;
}
else {
for(int i = 0; i < nums.length-1; i++) {
for(int j = 1; j <= nums.length-1; j++) {
if((nums[i] + nums[j]) == target && i != j) {
sol[0] = i;
sol[1] = j;
break;
}
}
}
}
return sol;
}
}