博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Add Digits(leetcode258)
阅读量:7207 次
发布时间:2019-06-29

本文共 820 字,大约阅读时间需要 2 分钟。

hot3.png

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

Example:

Input: 38

Output: 2 
Explanation: 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?

//常规操作public static int addDigits(int num) {    int result = num;    while(result >=10){         int sum = result;         result = 0;         while(sum>0){             result+=sum%10;             sum = sum/10;         }    }    return result;}

 

//看到别人这么写的  怎么推算的 不想动脑啦public int addDigits2(int num) {    if (num == 0){        return 0;    }    if (num % 9 == 0){        return 9;    }    else {        return num % 9;    }}

git:https://github.com/woshiyexinjie/leetcode-xin

转载于:https://my.oschina.net/u/2277632/blog/2986831

你可能感兴趣的文章
独立云计算服务商的多维实践之道:用户需求驱动变革
查看>>
JavaMail邮件发送不成功的那些坑人情况及分析说明
查看>>
GitHub Checks API帮助应用实现进一步的持续集成
查看>>
庖丁解牛迭代器,聊聊那些藏在幕后的秘密
查看>>
勇敢的交流者在敏捷组织中的重要性
查看>>
Android Pie提供了自适应供电、神经网络API 1.1等新特性
查看>>
蓝云公布2019云生态战略,如何解决企业上云关键问题?
查看>>
FaaS、PaaS和无服务器体系结构的优势
查看>>
Ceylon语言加入Eclipse基金会
查看>>
一文盘点MWC 2019所有5G设备和研发进展
查看>>
【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵
查看>>
网站真分页js代码该怎么写?
查看>>
教你五分钟入门使用html5 svg绘制图形
查看>>
vue-concise-slider vue滑动组件
查看>>
ElectronOCR:基于Electron+React+Tesseract的MACOS下的OCR工具
查看>>
Mysql 架构及优化之-定时计划任务
查看>>
不插即用!配备微信网页授权模块的CodeIgniter应用脚手架
查看>>
HBase存储剖析与数据迁移
查看>>
人工智能高考511分,未来有望考上东京大学!
查看>>
O2O业务都跳不出这五大领域
查看>>