Tuesday, March 19, 2013

[Java][Answer] Array-1 > sameFirstLast

Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal.


sameFirstLast([1, 2, 3]) → false
sameFirstLast([1, 2, 3, 1]) → true
sameFirstLast([1, 2, 1]) → tru

Answer 1:
public boolean sameFirstLast(int[] nums) {
  return nums.length>=1 && nums[0]==nums[nums.length-1];
}

Answer 2:
public boolean sameFirstLast(int[] nums) {
  if ( nums.length > 0 && nums[0]
                  == nums[nums.length-1] )
  return true;
  else
  return false;
}

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

No comments :

Post a Comment