Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use leetcode to represent numeric strings in golang

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Golang how to use leetcode to represent numeric strings, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Implement a function to determine whether a string represents a numeric value (including integers and decimals). For example, the strings "+ 100"," 5e2 ","-123", "3.1416", "0123" and "- 1E-16" all represent numeric values, but "12e", "1a3.14", "1.2.3", "+-5" and "12e+5.4" are not.

Ideas for solving the problem:

Remove the leading and trailing spaces from the string

Unified symbol, replace E with e

Split the string with e as the delimiter

When the length of the split array is 1, the number can be a signed floating point number or a signed integer. This part must contain a number symbol, otherwise it is illegal because of the existence of *.

When the length of the split array is 2, the number of the first part can be a signed floating point number or a signed integer, and the second part can only be a signed integer, and each part must contain a number symbol, otherwise it is illegal.

When the length of the split array is greater than 2, it is illegal

To determine whether a number is a signed floating point number or a signed integer:

Divide the string with the decimal point as the delimiter

When the length of the split array is 1, it must be a signed integer

When the length of the split array is 2, it must be a signed floating point number

Signed floating point numbers the first part is a signed integer

The second part of a signed floating point number is an unsigned integer

Code implementation

Import "strings" func isNumber (s string) bool {s0:=strings.Trim (s, ") s1:=strings.Replace (s0," E "," e ",-1) sa:=strings.Split (S1) "e") if len (sa) 2 {return false} if len (sa) = = 2 {if! isRealNumber (sa [1]) {return false} if len (sa [0]) = = 0 | len (sa [1]) = 0 {return false}} return isFloat (sa [0]) )} func isRealNumber (s string) bool {sc:=strings.Split (s ".") If len (sc)! = 1 | | len (sc [0]) 0 & &! isDigit (sb [1]) {return false}} l:=len (sb [0]) fmt.Println (lmemsb Sb [0]) sb0:=sb [0] if l > 1 & & (sb [0] [0] = ='+'| | sb [0] [0] = ='-') {sb [0] = sb [0] [1:]} fmt.Println (sb [0]), isDigit (sb [0]) Sb [0]) return (len (sb) = = 2 & & len (sb [1]) > 0 & & (sb [1]) > 0 & (lumbago = "+" | sb0== "-")) | | isDigit (sb [0])}

Func isDigit (s string) bool {if len (s) = = 0 {return false}

For iPUR.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report