It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.
Apparently, many engineers take measures on the site called LetCode.
It is a site that trains the algorithmic power that can withstand the coding test that is being done in the early story, and it is an inevitable path for those who want to build a career at an overseas tech company.
I wrote it in a big way, but I have no plans to have such an interview at the moment.
However, as an IT engineer, it would be better to have the same level of algorithm power as a person, so I would like to solve the problem irregularly and write down the method I thought at that time as a memo.
I'm solving it with Python3.
Leet Code Table of Contents Starting from Zero
Last time Leet Code Day53 starting from zero "1365. How Many Numbers Are Smaller Than the Current Number"
Right now, I'm prioritizing the Medium of the Top 100 Liked Questions. I solved all Easy, so if you are interested, please go to the table of contents.
Twitter I'm doing it.
1290. Convert Binary Number in a Linked List to Integer The difficulty level is Easy.
The problem is given a unidirectional linked list head
.
Each node has 0
or 1
as values and treats them as binary values. Here, convert those binary notation values to decimal and design an algorithm that returns numbers.
Input: head = [1,0,1] Output: 5 Explanation: (101) in base 2 = (5) in base 10
Input: head = [0] Output: 0
Input: head = [1] Output: 1
Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0] Output: 18880
Input: head = [0,0] Output: 0
I feel that the base conversion is a bit of a story. It may be annoying at first, but if you take a closer look, it is often one pattern, so it is recommended to solve the problem and get used to it.
The important thing about this problem is
--Cover all the elements of the linked list --Convert from binary to decimal
So it may be easy if you can understand it.
It seems that it is enough to prepare an array, add it to the array, move to the next element, and write it with a while statement.
It's a base conversion, but in Python, for example,
int('10',2)
If you write it in the form of, it will convert the binary number to the decimal number, so if you use it, the conversion itself is easy.
For more information [Official Document] We recommend that you refer to (https://docs.python.org/ja/3/library/stdtypes.html?highlight=find).
Let's write the flow up to this point in Python.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getDecimalValue(self, head: ListNode) -> int:
ans = []
while head:
ans.append(str(head.val))
head = head.next
return int(''.join(ans),2)
# Runtime: 16 ms, faster than 99.88% of Python3 online submissions for Convert Binary Number in a Linked List to Integer.
# Memory Usage: 13.7 MB, less than 85.59% of Python3 online submissions for Convert Binary Number in a Linked List to Integer.
join function is a convenient function that concatenates and returns as a character string if you specify the delimiter element.
For example, if you consider the example, when the while statement is finished, head = [1,0,1]
is added to the array ʻans with
str, so ʻans = ['. It becomes 1', '0', '1']
. If you write after return to concatenate here, it will be ʻans ['101', 2] `, so it will be finally converted to a decimal number and returned.
Up to here for this time. Thank you for your hard work.
Recommended Posts