描述:

给出一个非负整数 num,反复的将所有位上的数字相加,直到得到一个一位的整数。

给出 num = 38。

相加的过程如下:3 + 8 = 111 + 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]各位相加的更多相关文章

  1. LintCode之各位相加

    题目描述: 我的代码 public class Solution { /* * @param num: a non-negative integer * @return: one digit */ p ...

  2. [LintCode] Add Two Numbers 两个数字相加

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  3. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  4. lintcode 落单的数(位操作)

    题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...

  5. [Lintcode two-sum]两数之和(python,双指针)

    题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. T-SQL字符串相加之后被截断的那点事

    本文出处:http://www.cnblogs.com/wy123/p/6217772.html 字符串自身相加, 虽然赋值给了varchar(max)类型的变量,在某些特殊情况下仍然会被“截断”,这 ...

随机推荐

  1. XP和Win 7双系统安装说明和注意事项

    安装前说明: 1.先装XP,再装Windows 7,最好不要反过来,不然XP不会把Windows 7的启动管理器给覆盖掉,会麻烦些.总之遵循“旧版本到新版本”安装原则. 2.如果分区不够大,请用以下软 ...

  2. window下删除所有带.svn文件夹及文件,删除所有的.svn文件夹

    (一)------------------------------------------------------------------------------------------------- ...

  3. Mac - 使用php环境

    按下shift + Command + G  ,输入以下php(www)路径 /Library/WebServer/Documents 打开终端,输入以下命令打开环境 sudo apachectl s ...

  4. AngularJS 使用 UI Router 实现表单向导

    Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...

  5. linux安装rzsz(lrzsz)

    lrzsz是一个unix通信套件提供的,X,Y和ZModem文件传输协议,可以用在Windows与linux系统之间的文件传输,体积小速度快,可以与xshell工具配合使用. (1)在线安装 yum ...

  6. PYNQ = Python + ZYNQ —— ZYNQ部分功能的Python化

    PYNQ优点:1.    Python用于ZYNQ开发,Python库和FPGA硬件库可以直接调用,极大加快开发进程.缩短开发周期.降低开发难度,更方便.快捷:2.    用PYNQ开发,当Pytho ...

  7. 【Android】8.1 主题基本用法

    分类:C#.Android.VS2015: 创建日期:2016-02-17 一.创建本章示例主界面 1.界面截图 2.MainActivity.cs文件中对应的代码 在CreateChItems()方 ...

  8. Untrusted 游戏 通关攻略

    这个游戏还不错,用了两个晚上的时间通过并写下解法.这个游戏通过修改JS代码来通关的游戏.很考验玩家的解决问题能力,同时也有一定的可玩性. 游戏地址 http://alexnisnevich.githu ...

  9. Spark使用总结与分享【转】

    背景 使用spark开发已有几个月.相比于python/hive,scala/spark学习门槛较高.尤其记得刚开时,举步维艰,进展十分缓慢.不过谢天谢地,这段苦涩(bi)的日子过去了.忆苦思甜,为了 ...

  10. windows 简单api应用

    //调用系统函数 将鼠标移动到相应位置 [DllImport("user32.dll", EntryPoint = "SetCursorPos")] publi ...