描述:

给出一个非负整数 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. iOS archive(归档)

    归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存(实际上是一种文件保存的形式),浏览网上的一些资料后,并结合自己的一些经验,总结成此文. 一.使用archiveRootObject进 ...

  2. RabbitMQ与.net core(二)Producer与Exchange

    Producer:消息的生产者,也就是创建消息的对象 Exchange:消息的接受者,也就是用来接收消息的对象,Exchange接收到消息后将消息按照规则发送到与他绑定的Queue中.下面我们来定义一 ...

  3. Android应用如何适配不同分辨率的手机

    主要分三块考虑   1 )界面配置   根据不同的分辨率,创建手机界面文件   例子: 在res下创建 layout-800x480            layout-480x320   并在各自不 ...

  4. 定时器quartz工具类

    一.gradle配置 // https://mvnrepository.com/artifact/org.quartz-scheduler/quartz compile group: 'org.qua ...

  5. mpu6050 DMP库的移植

    https://www.amobbs.com/thread-5528472-1-1.html 官方的运动库,必须通过这个才能启用MPU6050的DMP引擎(数据手册里完全不提这个东西,必须在官网注册登 ...

  6. VLOG丨树莓派Raspberry Pi 3安装PLEX并挂载USB硬盘打造最牛的微型家庭影音服务器2018

    视频介绍 树莓派3安装目前最流行的PLEX服务器,实现既能最大限度降低功耗,也能随时随地观看分享影片. 一.在树莓派下安装PLEX媒体服务器 1.在终端,将你的树莓派更新至最新 sudo apt up ...

  7. TreeMap升序|降序排列和按照value进行排序

    TreeMap 升序|降序排列 import java.util.Comparator; import java.util.TreeMap; public class Main { public st ...

  8. 在C#中使用WMI查询进程的用户信息

    这是一个使用WMI查询信息的例子.看之前请对WMI有一个简单的了解,可以百度,或者查看我上一篇:WMI测试器 主要代码:(需要添加对System.Management的引用) //创建Win32_Pr ...

  9. chkconfig命令具体介绍

    命令介绍: chkconfig命令用来更新.查询.改动不同执行级上的系统服务.比方安装了httpd服务,而且把启动的脚本放在了/etc/rc.d/init.d文件夹下,有时候须要开机自己主动启动它,而 ...

  10. 【Android】5.3 单选和复选

    分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 1.CheckBox 复选 [Checked]属性:是否选中. 2.RadioButton 单选 [Checked ...