Saturday, March 19, 2016

[Java][Answer] CodingBat AArray-1 > no23()

Given an int array length 2, return true if it does not contain a 2 or 3.

no23([4, 5]) → true
no23([4, 2]) → false
no23([3, 5]) → false

Answer 1:
public boolean no23(int[] nums) {
  return !(nums[0] == 2 || nums[0] == 3 || nums[1] == 2 || nums[1] == 3);
}
Answer 2:
public boolean no23(int[] nums) {
  if (nums[0] != 2 && nums[0] != 3 && nums[1] != 2 && nums[1] != 3)
    return true;
  else
    return false;
}

Reference
http://www.javaproblems.com/2013/11/java-array-1-no23-codingbat-solution.html

No comments :

Post a Comment