소프트웨어에 대한 모든 것

LeetCode 풀기 - 1290. Convert Binary Number in a Linked List to Integer 본문

알고리즘/LeetCode

LeetCode 풀기 - 1290. Convert Binary Number in a Linked List to Integer

앤테바 2021. 12. 4. 15:58
반응형

1290. Convert Binary Number in a Linked List to Integer

https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/

 

Convert Binary Number in a Linked List to Integer - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def getDecimalValue(self, head: ListNode) -> int:
        str_bins = deque()
        node = head
        while node:
            str_bins.appendleft(node.val)
            node = node.next
            
        res = 0
        for i, v in enumerate(str_bins):
            if v == 1:
                res += pow(2, i)
        return res

 

반응형
Comments