-
-
Save jonsie/a10fdbc1adff15321ab0fab1e2065115 to your computer and use it in GitHub Desktop.
binary list thingy
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class BinaryListProcessor { | |
| public static List<Integer> processBinaryList(List<Integer> binaryList) { | |
| List<Integer> addList = new ArrayList<>(); | |
| List<Integer> resultList = new ArrayList<>(); | |
| for (int index = 0; index < binaryList.size(); index++) { | |
| int value = binaryList.get(index); | |
| // ignore the last index since we do a look ahead below, avoid out of bounds error | |
| if (index != binaryList.size() - 1) { | |
| addList.add(value); | |
| // check the next value | |
| if (binaryList.get(index + 1) == 1) { | |
| resultList.add(addList.size()); | |
| addList.remove(0); | |
| if (!addList.isEmpty()) { | |
| resultList.addAll(addList); | |
| addList.clear(); | |
| } | |
| } | |
| } | |
| } | |
| return resultList; | |
| } | |
| public static void main(String[] args) { | |
| List<Integer> binaryList = new ArrayList<>(); | |
| // Example input | |
| binaryList.add(1); | |
| binaryList.add(1); | |
| binaryList.add(0); | |
| binaryList.add(0); | |
| binaryList.add(1); | |
| binaryList.add(0); | |
| binaryList.add(0); | |
| binaryList.add(0); | |
| binaryList.add(1); | |
| binaryList.add(0); | |
| binaryList.add(0); | |
| binaryList.add(0); | |
| binaryList.add(0); | |
| binaryList.add(1); | |
| binaryList.add(1); | |
| List<Integer> result = processBinaryList(binaryList); | |
| System.out.println(result); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment