leetcode-258.add digits/Digital root

解决升级go版本遇到的Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.问题 推荐两个比较值得参考的go开源项目列表这里主要提供两个链接AWESOME-GOAWESOME-GO,一个很全的go语言框架/库/开源软件合集https://github.com/avelino/awesome-go中文版awesome-go相较于AWESOME-GO,主要优点是: 中文 相较于AWESOME-GO,虽然项目少,但是每个项目有建议介绍,方便新人快速入门 地址:https://link. 阅读详情

leetcode-258.Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

如果纯粹按照上面的想法,显然是想不出有什么优化方式。但是本题要求我们使用O(1)的时间复杂度,那么应该换一种表达方式才能够解决。

数根

在数学中,数根(又称数字根Digital root)是自然数的一种性质,换句话说,每个自然数都有一个数根。

数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于10的话,则继续将各位数进行横向相加直到其值小于十为止[1],或是,将一数字重复做数字和,直到其值小于十为止,则所得的值为该数的数根。

例如54817的数根为7,因为5+4+8+1+7=25,25大于10则再加一次,2+5=7,7小于十,则7为54817的数根。

参考计算地址

https://en.wikipedia.org/wiki/Digital_root

用途

数根可以计算模运算的同余,对于非常大的数字的情况下可以节省很多时间。

数字根可作为一种检验计算正确性的方法。例如,两数字的和的数根等于两数字分别的数根的和。

另外,数根也可以用来判断数字的整除性,如果数根能被3或9整除,则原来的数也能被3或9整除。

实现

dr(n) = 0 if n = 0,

dr(n) = 9 if n%9 = 0,

dr(n) = n mod 9 if n%9 != 0

func addDigits(num int) int {
    if (num == 0){
        return 0
    }

    finalDigit := num%9
    if (finalDigit == 0){
        return 9
    } else{
        return finalDigit
    }
}
go简易udp客户端和服务器 go简易udp socket客户端和服务器1.Socket编程以前使用Socket编程时,一般是如下步骤 建立socket,socket 绑定socket,bind 监听,listen 接受连接,accept 接受/发送,recv/send Go语言对其进行了抽象和封装,刚开始接触有可能不太适应(譬如我第一天用的时候觉得API好难找……建议参考文档),后来发现用起来很爽简单来说,客户端省去了很多!客 阅读详情

相关推荐

golang使用protobuf简易教程

golang使用protobuf简易教程google公司发布的一套开源编码规则,基于二进制流的序列化传输,可以转换成多种编程语言,几乎涵盖了市面上所有的主流编程语言,当然也包括Go1、安装protobuf 安装下载protoc,很多种安装方法,下载地址https://github.com/google/protobuf/releases 安装下载proto的go插件,命令是go get github

绯浅yousa的笔记 1万+

leetcode.258.Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

RaymondWong的博客 252

go简易tcp socket客户端和服务器

go简易tcp socket客户端和服务器1.Socket编程以前使用Socket编程时,一般是如下步骤 建立socket,socket 绑定socket,bind 监听,listen 接受连接,accept 接受/发送,recv/send go tcp版真的很省事 服务端: 就是Listen、Accept、Read/Write 客户端 就是Dial、Read/Write直接上代码2.Ser

绯浅yousa的笔记 1万+

leetcode258. Add Digits

problem 258. Add Digits solution1: class Solution { public: int addDigits(int num) { int sum = 0; int n = num; while(n/10) { sum = n/10 + n...

weixin_30481087的博客 87

LeetCode 258-Add Digits

Given a non-negative integernum, repeatedly add all its digits until the result has only one digit. For example: Givennum = 38, the process is like:3 + 8 = 11,1 + 1 = 2. Since2has only one dig...

weixin_34064653的博客 83

LeetCode258. Add Digits 解题小结

题目: Given a non-negative integernum, repeatedly add all its digits until the result has only one digit. For example: Givennum = 38, the process is like:3 + 8 = 11,1 + 1 = 2. Since2has only on...

weixin_30567471的博客 84

LeetCode258. Add Digits

题目:         Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.              For example:         Given num = 38, the process is like: 3 + 8

糖梦梦是女侠 394

LeetCode258. Add Digits (2 solutions)

Add Digits Given a non-negative integernum, repeatedly add all its digits until the result has only one digit. For example: Givennum = 38, the process is like:3 + 8 = 11,1 + 1 = 2. Since2has ...

weixin_30325071的博客 75

LeetCode -- 258. Add Digits

题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one di...

weixin_30609331的博客 105

[2016/06/24] LeetCode / Java - Day 02 -

258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 +

Eclipse是我男朋友 462

Go语言:解决数据库中null值的问题

Go语言:解决数据库中null值的问题 本文主要介绍如何使用go语言database/sql库从数据库中读取null值的问题,以及如何向数据库中插入null值。本文在这里使用的是sql.NullString, sql.NullInt64, sql.NullFloat64等结构体,为了方便书写,它们的泛指我会使用sql.Null***来表示 要点 从数据库读取可能为null值得值时,可以选择使用s

绯浅yousa的笔记 5万+

Go获取两个时间时间

获取当前时间time.Now() 两个时间作差,第一个时间是t1,第二个时间是t2,那么时间差是t2.Sub(t1) 具体显示时间可以通过Format(time.ANSIC)打印时间格式,以及In接口+Location设置时区 例如: TimeLocation, err := time.LoadLocation("Asia/Shanghai") //err处理 t1.Now().In(Ti...

绯浅yousa的笔记 5万+

linux设置环境变量GOPATH

vim /etc/profile export GOROOT=/usr/local/go #设置为go安装的路径,有些安装包会自动设置默认的goroot export GOPATH=$HOME/gocode #默认安装包的路径 export PATH=$PATH:$GOROOT/bin:$GOPATH/bin source /etc/profile...

绯浅yousa的笔记 4万+

go语言获取数组长度

go语言获取数组长度示例package mainimport "fmt"func main() { myArray := [3][4]int{{1,2,3,4},{1,2,3,4},{1,2,3,4}} //打印一维数组长度 fmt.Println(len(myArray)) //打印二维数组长度 fmt.Println(len(myArray[1]))

绯浅yousa的笔记 4万+

float:double类型数据在内存中中存储格式

float/double类型数据在内存中中存储格式 float/double类型数据在计算机是如何存储的呢? 它们是ieee standard 754的存储方式。 譬如float数,第一位是符号位,然后是8位指数位,然后是23位尾数;double双精度格式为8字节64位,由三个字段组成:52位小数f,11位偏置指数e,以及1位符号s,这些字段连续存储在两个32位字中。存储结构 类型 符号

绯浅yousa的笔记 1万+

跨域资源共享CORS学习笔记

跨域资源共享CORS学习笔记1、源政策含义1995年,源政策由 Netscape 公司引入浏览器。目前,所有浏览器都实行这个政策。最初,它的含义是指,A网页设置的 Cookie,B网页不能打开,除非这两个网页”源”。所谓”源”指的是”三个相”。协议相 域名相 端口相举例来说,http://www.example.com/dir/page.html这个网址,协议是http://,域名是

绯浅yousa的笔记 8590

从nginx热更新聊一聊Golang中的热更新(下)

从nginx热更新聊一聊Golang中的热更新(下) 静态语言在服务器编程时都会遇到这样的问题:如何保证已有的连接服务不中断时又升级版本? 在上一篇介绍热升级的时候时候,讲到了通过信号通知nginx进行热升级。我们在这一篇中介绍下平滑重启go http server。 目录结构 热更新 热更新目标: 1、正在处理中的连接/服务/请求不能立即中断,需要继续提供服务 2、socket对用户来说...

绯浅yousa的笔记 8074
上一篇: leetcode-292.Nim Game
下一篇: cjson源码剖析(1)
绯浅yousa
博客等级 码龄12年 152粉丝 241原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值