描述:

给出一个非负整数 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. 【LeetCode】64. Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  2. 理解over()函数

    1.1.两个order by的执行时机分析函数(以及与其配合的开窗函数over())是在整个sql查询结束后(sql语句中的order by的执行比较特殊)再进行的操作, 也就是说sql语句中的ord ...

  3. go 学习笔记(1)--package

    引入包有以下几种方式: 1. 最简单的方式引入一个包的方式是直接引入包,例如: import "fmt" import "os" 2. 也可以通过下面的方式将包 ...

  4. epoll 系列函数简介、与select、poll 的区别

    一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...

  5. elk中文教程

    https://kibana.logstash.es/content/elasticsearch/monitor/logging.html ELK 实战之Elasticsearch ELK 地址:ht ...

  6. C 常量指针和指针常量

    * (指针)和 const(常量) 谁在前先读谁 :*象征着地址,const象征着内容:谁在前面谁就不允许改变. 例子: ; ; ; int const *p1 = &b;//const 在前 ...

  7. extjs中组件监听器里面的回调函数说明

    近期在看项目源代码的时候发现了例如以下代码,当中_searchSupplierStore是JsonStore对象 _searchSupplierStore.on('beforeload',functi ...

  8. 【Android】21.1 画板资源

    分类:C#.Android.VS2015: 创建日期:2016-03-19 一.简介 画板资源(Drawable Resources)是用XML描述/Resources/drawable中的2D图形文 ...

  9. Progressive Web App

    下一代 Web 应用? 近年来,Web 应用在整个软件与互联网行业承载的责任越来越重,软件复杂度和维护成本越来越高,Web 技术,尤其是 Web 客户端技术,迎来了爆发式的发展. 包括但不限于基于 N ...

  10. iptables允许一个ip访问本机的某个端口

    需求是redis允许特定客服端连接: -A INPUT -s .xx.xx.xxx/ -p tcp --dport -j ACCEPT