Created
July 16, 2016 18:48
-
-
Save jianminchen/ae6ab2d83a090b79e161cb4c870d6aa8 to your computer and use it in GitHub Desktop.
bulbs - facebook code lab - stack overflow, need to work on iterative solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int bulbs(List<Integer> a) { | |
if(a == null || a.size() == 0) | |
return 0; | |
Integer val = a.get(0); | |
a.remove(0); | |
if(val.intValue() ==0) | |
{ | |
List<Integer> opposite = getOpposite(a); | |
return 1 + bulbs(opposite); | |
} | |
else | |
return bulbs(a); | |
} | |
public List<Integer> getOpposite(List<Integer> a) | |
{ | |
List<Integer> list = new ArrayList<Integer>(); | |
if(a== null) | |
return list; | |
for(int i=0; i< a.size(); i++) | |
{ | |
int val = Integer.valueOf(a.get(i)); | |
Integer tmp = val == 0? Integer.valueOf(1):Integer.valueOf(0); | |
list.add(tmp); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment