Wednesday, April 13, 2016

[Java][Answer] Array-1 > fix23()

Given an int array length 3, if there is a 2 in the array immediately followed by a 3, set the 3 element to 0. Return the changed array.

fix23([1, 2, 3]) → [1, 2, 0]
fix23([2, 3, 5]) → [2, 0, 5]
fix23([1, 2, 1]) → [1, 2, 1]

Answer 1

public int[] fix23(int[] nums) {
  if(nums[0]==2 && nums[1]==3) nums[1] = 0;
  if(nums[1]==2 && nums[2]==3) nums[2] = 0;
  return nums;
}

Answer 2

public int[] fix23(int[] nums) {
  if (nums[0] == 2 && nums[1] == 3)
   nums[1] = 0;
   if (nums[1] == 2 && nums[2] == 3)
   nums[2] = 0;
   return new int[] {nums[0],nums[1],nums[2]};
}

Reference

http://www.javaproblems.com/2012/12/coding-bat-java-array-1-fix23.html

No comments :

Post a Comment