Sunday, April 10, 2016

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

Start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element.

start1([1, 2, 3], [1, 3]) → 2
start1([7, 2, 3], [1]) → 1
start1([1, 2], []) → 1

Answer 1:

public int start1(int[] a, int[] b) {
  int sum = 0;
  if(a.length>0 && a[0] == 1) sum++;
  if(b.length>0 && b[0] == 1) sum++;
  return sum;
}

Answer 2:

public int start1(int[] a, int[] b) {
int count = 0;
  if (a.length != 0) {
     if (a[0]== 1) count++; }
  if (b.length != 0) {
     if (b[0]== 1) count++; }
    
     return count;
}

Reference

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

No comments :

Post a Comment