题目描述:

我的代码

 public class Solution {
/*
* @param num: a non-negative integer
* @return: one digit
*/
public int addDigits(int num) {
// write your code here
int[] n = new int[10];
int f = -1,sum=0;
//当num是个位数的时候退出循环
while(num/10 != 0) {
//循环取得num的各位数
while(num != 0) {
n[++f] = num%10;
num /= 10;
}
//数组出栈获得各位数之和
while(f > -1) {
sum = sum + n[f--];
}
num = sum;
sum = 0;
} return num;
}
}

总结:这道题是LintCode中的一道容易题,我用了一个栈的思想,把每次取得的num的各位数依次入栈,在依次出栈获得总和,把总和的值赋给num,当num为个位数时退出循环返回num。

LintCode之各位相加的更多相关文章

  1. [LintCode]各位相加

    描述: 给出一个非负整数 num,反复的将所有位上的数字相加,直到得到一个一位的整数. 给出 num = 38. 相加的过程如下:3 + 8 = 11,1 + 1 = 2.因为 2 只剩下一个数字,所 ...

  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. HTML --JS 选择框

    <html> <head> <title>选择框</title> <script language="JavaScript"& ...

  2. 继承Process类,另一种方法计算累加和以及阶乘

    #定义一个类 继承Process类 from multiprocessing import Process import os import time class jiecheng(Process): ...

  3. Vuejs中关于computed、methods、watch,mounted的区别

    1.computed是在HTML DOM加载后马上执行的,如赋值: 2.methods则必须要有一定的触发条件才能执行,如点击事件: 3.watch呢?它用于观察Vue实例上的数据变动.对应一个对象, ...

  4. bzoj2396 神奇的矩阵(随机化)

    Time Limit: 5 Sec  Memory Limit: 512 MB 给出三个行数和列数均为N的矩阵A.B.C,判断A*B=C是否成立. 题目可能包含若干组数据.    对于每组数据,第一行 ...

  5. 使用form提交到搜狗浏览器示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Mac下安装nodejs,然后安装Vue-devtools工具

    一.安装nodejs 1.这一步简单,只要上官网下载下来,直接按照提示安装就可以,mac版本的安装方法很简单. 下载nodejs的官方网址是:  nodejs.org    ,浏览器输入就可以跳转到了 ...

  7. elasticsearch 基础 —— ReIndex

    Reindex会将一个索引的数据复制到另一个已存在的索引,但是并不会复制原索引的mapping(映射).shard(分片).replicas(副本)等配置信息. 一.reindex的常用操作 1.re ...

  8. 2018-2-13-WPF-绑定密码

    title author date CreateTime categories WPF 绑定密码 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 + ...

  9. Swoole 的运行模式

    Swoole 做了什么 Swoole 是 php 的一个扩展,但是他又不是普通的扩展,其最明显的特点就是:一但运行后就会接管PHP的控制权,进入事件循环. 当某种IO事件发生时, Swoole 会回调 ...

  10. smbumount - 为普通用户卸载smb文件系统

    总览 smbumount 装载点 描述 普通用户使用这个程序可以卸载smb文件系统.它在工作时会suid到root身份,并且向普通linux用户提供了对资源更多的控制能力.在suid方面,它拥有足够的 ...