This question has a tricky part. It asks you to determine whether a string is an IP address, but does not specify whether it is IPv4 or IPv6. If you only consider IPv4 when writing the algorithm but ignore IPv6, the interviewer may ask you to write out the IPv6 solution process. Before writing an algorithm, you need to understand the concepts of IPv4 and IPv6. If the interviewer does not tell you the definitions of these two, it may be to test your basic computer knowledge and the definition of IP addresses. Let's take a look at the definitions of IPv4 and IPv6 on LeetCode.
IPv4 IPv4 addresses are represented by decimal numbers and points. Each address contains 4 decimal numbers ranging from 0 to 255, separated by ("."). For example, 172.16.254.1; Also, numbers in IPv4 addresses do not start with 0. For example, the address 172.16.254.01 is illegal. IPv6 An IPv6 address is represented by 8 groups of hexadecimal numbers, each group represents 16 bits. These groups of numbers are separated by (":"). For example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid address. Moreover, we can add some numbers starting with 0, and letters can be uppercase or lowercase. So, 2001:db8:85a3:0:0:8A2E:0370:7334 It is also a valid IPv6 address (ie, ignoring the leading 0 and ignoring case). However, we cannot use an empty group just because the value of a group is 0, which will result in ::. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address. Code implementation: Determine whether it is an IPv4 address. It is easy to make mistakes when using JS to solve this problem. Convert the string directly to a number and then determine whether it is between 0 and 255. However, parseInt('1e1', 10) is not the result you expect. For a detailed explanation, refer to Converting a string to an integer. I was taught a lesson. IPv4 problem-solving ideas: The length of the split must be 4; The value of each node can only be an integer between 0 and 255:
Determine whether it is an IPv6 address. Solution:
Key points When implementing this problem with JavaScript, you need to pay attention to the "pitfall" when converting strings to integers. Characters can be compared based on the value of the character encoding. |
<<: “Double Eleven” flash sale strategy: Which is faster, 5G or Wi-Fi 6?
>>: Hard-core dry goods: HTTP timeout, repeated requests must see the pitfalls and solutions
Friendhosting sent a promotional email yesterday ...
edgeNAT is a Chinese host provider established in...
1. Network card model There are mainly two types ...
【51CTO.com Quick Translation】With the industry...
1. Management and configuration of routers and sw...
1. What is your understanding of the TCP/IP four-...
By the end of 2021, nearly 8% of the world's ...
September 22, 2021 – Aruba, a Hewlett Packard Ent...
[[181278]] On January 6, the Ministry of Science ...
In the wilderness, two nimble figures move left a...
[51CTO.com original article] Technology has been ...
It is very necessary to understand the user licen...
In modern network architecture, the health of net...
DogYun also officially released the promotional a...
In today's networking world, Wifi and Etherne...