题解 | #IP地址转化#

IP地址转化

https://www.nowcoder.com/practice/67ae7c321ae147049ad569c8fe5c98cd?tpId=196&tqId=40406&ru=/exam/oj

package main

import (
	"strconv"
	"strings"
)

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param ip string字符串
 * @return string字符串
 */
func IPtoNum(ip string) string {
	words := strings.Split(ip, ".")
	res := []string{}
	for i := 0; i < len(words); i++ {
		v, _ := strconv.Atoi(words[i])
		ans := strconv.FormatInt(int64(v), 2)
		for len(ans) < 8 {
			ans = "0" + ans
		}
		res = append(res, ans)
	}
	t := strings.Join(res, "")

	r1, _ := strconv.ParseUint(t, 2, 64)

	return strconv.FormatUint(r1, 10)

}

全部评论

相关推荐

点赞 评论 收藏
分享
机械打工仔:有说的你怀疑一下就行了,直接问也太实诚了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 11:22
怎么这么多逆天求职者,救救我救救我救救我😭
flmz_Kk:哈哈哈哈哈哈,这么多求职者,肯定有那一两个逆天的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务