Skip to content

Commit 0efd32d

Browse files
add 1290
1 parent 436a34e commit 0efd32d

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
1010
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
11+
|1290|[Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1290.java) | | | |Easy||
1112
|1287|[Element Appearing More Than 25% In Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1287.java) | | | |Easy||
1213
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | | | |Medium||
1314
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | | | |Easy||
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
5+
/**
6+
* 1290. Convert Binary Number in a Linked List to Integer
7+
*
8+
* Given head which is a reference node to a singly-linked list.
9+
* The value of each node in the linked list is either 0 or 1.
10+
* The linked list holds the binary representation of a number.
11+
*
12+
* Return the decimal value of the number in the linked list.
13+
*
14+
* Example 1:
15+
* Input: head = [1,0,1]
16+
* Output: 5
17+
* Explanation: (101) in base 2 = (5) in base 10
18+
*
19+
* Example 2:
20+
* Input: head = [0]
21+
* Output: 0
22+
*
23+
* Example 3:
24+
* Input: head = [1]
25+
* Output: 1
26+
*
27+
* Example 4:
28+
* Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
29+
* Output: 18880
30+
*
31+
* Example 5:
32+
* Input: head = [0,0]
33+
* Output: 0
34+
*
35+
* Constraints:
36+
*
37+
* The Linked List is not empty.
38+
* Number of nodes will not exceed 30.
39+
* Each node's value is either 0 or 1.
40+
* */
41+
public class _1290 {
42+
public static class Solution1 {
43+
public int getDecimalValue(ListNode head) {
44+
StringBuilder sb = new StringBuilder();
45+
while (head != null) {
46+
sb.append(head.val);
47+
head = head.next;
48+
}
49+
return Integer.parseInt(sb.toString(), 2);
50+
}
51+
}
52+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
import com.fishercoder.solutions._1290;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static junit.framework.TestCase.assertEquals;
11+
12+
public class _1290Test {
13+
private static _1290.Solution1 solution1;
14+
private static ListNode head;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _1290.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
head = ListNode.createSinglyLinkedList(Arrays.asList(1, 0, 1));
24+
assertEquals(5, solution1.getDecimalValue(head));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)