描述:

给出一个非负整数 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. Http Analyzer Std V3.3.1.140 汉化补丁

    http://www.cnblogs.com/nicch/archive/2008/08/30/ha_httpanalyzerstdv3.html Http Analyzer Std V3.3.1.1 ...

  2. 阿里云ECS服务器Linux环境下配置php服务器(二)--phpMyAdmin篇

    上一篇讲了PHP服务器的基本配置,我们安装了apache,php,还有MySQL,最后还跑通了一个非常简单的php页面,有兴趣的朋友可以看我的这篇博客: 阿里云ECS服务器Linux环境下配置php服 ...

  3. Google Map 学习过程中的代码

    <!DOCTYPE html><html> <head> <title>Simple click event</title> <met ...

  4. Debian/Ubuntu架设nginx+bugzilla

    题注 最近需要一个简单快速的bug追踪工具,考虑到系统环境以及学习成本,决定采用bugzilla.不试不知道,原来这里面还有这么多的坑需要一个个踩平~,遂随笔一下以备后用. 我采用的系统组成是debi ...

  5. OAF_OAF组件系列1 - Item Style汇总(概念)

    20150506 Created By BaoXinjian

  6. Linux时间子系统(六) POSIX timer

    一.前言 在用户空间接口函数文档中,我们描述了和POSIX timer相关的操作,主要包括创建一个timer.设定timer.获取timer的状态.获取timer overrun的信息.删除timer ...

  7. tomcat支持https的历程

    tomcat真是业界良心啊,文档写的详细无比. 一.https是什么? 简单的说,就是http+SSL/TLS 协议还是http,但是在传输层过程中使用了加密(涉及握手.秘钥分发.加密.解密等过程). ...

  8. MyBatis generator 使用方式 小结

    1.  配置到 maven 中pom.xml 中 2.  手动运行 脚本文件 3. 基于 web 项目  http://git.oschina.net/redArmy/springboot-gener ...

  9. mysql查询-不存在记录时赋对应的数据

    使用mysql数据库,执行查询的时候,有时候就不存在记录,但是正好在不存在记录的时候又需要给赋予相应的查询结果字段,代码实现如下: select IFNULL(('), '1970-01-01 00: ...

  10. mysqld_safe与mysqld区别详解

    mysqld_safe与mysqld区别,直接运行mysqld程序来启动MySQL服务的方法很少见,mysqld_safe脚本会在启动MySQL服务器后继续监控其运行情况,并在其死机时重新启动它. 用 ...