[LintCode]各位相加
描述:
给出一个非负整数 num,反复的将所有位上的数字相加,直到得到一个一位的整数。
给出 num = 38。
相加的过程如下:3 + 8 = 11,1 + 1 = 2。因为 2 只剩下一个数字,所以返回 2。
分析:
这道题并不难,只能说用好递归吧。
方法一:
public int addDigits(int num) {
// write your code here
if(num / 10 == 0){
return num;
}else{
return addDigits(sum(num));
}
}
public int sum(int num){
if(num == 0){
return 0;
}else{
return num % 10 + sum(num / 10);
}
}
方法二:
public int addDigits2(int num) {
// write your code here
String numStr = num + "";
if (numStr.length() > 1) {
int sum = 0;
for (int i = 0; i < numStr.length(); i++) {
sum += Integer.parseInt(numStr.charAt(i) + "");
num = sum;
}
return addDigits2(num);
}
return num;
}
[LintCode]各位相加的更多相关文章
- LintCode之各位相加
题目描述: 我的代码 public class Solution { /* * @param num: a non-negative integer * @return: one digit */ p ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- lintcode 落单的数(位操作)
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- T-SQL字符串相加之后被截断的那点事
本文出处:http://www.cnblogs.com/wy123/p/6217772.html 字符串自身相加, 虽然赋值给了varchar(max)类型的变量,在某些特殊情况下仍然会被“截断”,这 ...
随机推荐
- JNI入门
JNI是Java Native Interface的缩写,Native指C/C++. JNI内容涉及两个方面: Java调用C,这种情况是最主要的 C调用Java,这种情况不常见 第一步:编写Java ...
- SQL语句面试题
一条SQL查询,一个表中按日期的累加数据如表: tmp_pay_amount pay_time amount 2013-11-1 10 2013-11-2 5 2013-11-3 4 2013-11- ...
- java 移位
java中没有2进制的数据类型,对二进制的操作,需要使用三种操作符 << 左移位操作符,算数左移 用来将一个数的二进制位序列左移若干位,高位左移后溢出,舍弃不用,右补0 ...
- C#搞工控的一些代码
首先工控项目都会用到: using System.Runtime.InteropServices; 1.字节转化为单精度浮点数 2.单精度转成字节 3.使用结构体 4.使用动态链接库 5.ASCCII ...
- DataTable.Compute()
一.DataTable.Compute()方法說明如下 作用: 计算用来传递筛选条件的当前行上的给定表达式. 格式為: Object Compute (string ...
- RHCE7 -- systemctl命令
查询所有单元的状态: [root@rhce7 ~]# systemctl UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automo ...
- 采用dlopen、dlsym、dlclose加载动态链接库
1.前言 为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将各个业务已动态链接库的形式加载进来,这就是所谓的插件.linux提供了加载和处理动态链接库的系统 ...
- PHP安装Redis扩展教程
安装redis 下载软件包phpredis https://github.com/nicolasff/phpredis uzip master #解压得到 phpredis-master c ...
- jenkins构建自动执行jmeter 发送http请求,中间有替换参数路径
#在构建目录下创建jmeter目录,在这个目录下面执行jmeter性能测试mkdir -p $WORKSPACE/target/apache-jmeter-3.1/#复制jmeter文件到执行测试目录 ...
- Latex中定义、定理、引理、证明 设置方法总结
Latex中定义.定理.引理.证明 设置方法总结 在LaTex中需要有关定理.公理.命题.引理.定义等时,常用如下命令 \newtheorem{定理环境名}{标题}[主计数器名] \newtheore ...