Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- greedy
- Binary Tree
- java
- 구현
- Number Theory
- geometry
- Method
- Tree
- hash table
- two pointers
- simulation
- bit manipulation
- 파이썬
- Math
- 코테
- Stack
- array
- database
- Binary Search
- Data Structure
- Class
- SQL
- string
- implement
- sorting
- Counting
- Matrix
- 코딩테스트
- dynamic programming
- 자바
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1290. Convert Binary Number in a Linked List to Integer 본문
코딩테스트 풀이/JAVA
[LeetCode/Easy] 1290. Convert Binary Number in a Linked List to Integer
무지맘 2022. 12. 24. 01:231. Input
1) 1 또는 0으로 구성된 값을 linked list로 구현한 ListNode head
2. Output
1) head가 뜻하는 2진수를 10진수로 변환한 결과
3. Constraint
1) linked list는 비어있지않다.
2) 노드의 개수는 30개를 초과하지 않는다.
3) 각 노드의 값을 1 또는 0이다.
4. Example
Input: head = [1,0,1] -> Output: 5
설명: 101(2) = 5이다.
5. Code
1) 첫 코드(2022/06/05)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
String s = "";
ListNode n = head;
while(n!=null){
s += n.val;
n = n.next;
}
int result = 0;
int len = s.length();
for(int i=0 ; i<len ; i++)
result += (s.charAt(i)-'0') * Math.pow(2,len-i-1);
return result;