sum3([1, 2, 3]) → 6
sum3([5, 11, 2]) → 18
sum3([7, 0, 0]) → 7
Answer 1:
public int sum3(int[] nums) {
return nums[0] + nums[1] + nums[2];
}
Answer 2:
public int sum3(int[] nums) {
int sum = 0;
for(int i=0;i<nums.length; i++){
sum += nums[i];
}
return sum;
}
No comments :
Post a Comment