LintCode之各位相加
题目描述:


我的代码
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之各位相加的更多相关文章
- [LintCode]各位相加
描述: 给出一个非负整数 num,反复的将所有位上的数字相加,直到得到一个一位的整数. 给出 num = 38. 相加的过程如下:3 + 8 = 11,1 + 1 = 2.因为 2 只剩下一个数字,所 ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- lintcode 落单的数(位操作)
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- T-SQL字符串相加之后被截断的那点事
本文出处:http://www.cnblogs.com/wy123/p/6217772.html 字符串自身相加, 虽然赋值给了varchar(max)类型的变量,在某些特殊情况下仍然会被“截断”,这 ...
随机推荐
- 图解Http阅读笔记(一)
1.网络基础 TCP/IP 1.1TCP /IP 协议族 计算机与网络设备要相互通信,双方就必须基于相同的方法.比如,如何探测到通信目标.由哪一边先发起通信.使用哪种语言进行通信.怎样结束通信等规 ...
- vsphere虚拟化之 NTP服务的创建(三)
1.先修改windows 2012的注册表. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\ 设置 Annou ...
- [Linux] 009 链接命令
链接命令:ln 命令名称:ln 命令英文原意:link 命令所在路径:/bin/ln 执行权限:所有用户 语法:ln -s [原文件] [目标文件] 功能描述:生成链接文件 范例: 创建文件 /etc ...
- [Linux] 008 文件处理命令
1. 文件处理命令:touch 命令名称:touch 命令所在路径:/bin/touch 执行权限:所有用户 语法:touch [文件名] 功能描述:创建空文件 范例: 文件名不包含空格 touch ...
- [Bzoj1008][HNOI2008]越狱(组合计数)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 组合计数的简单题,可能越狱的方案数等于总方案数-不可能越狱的方案数,则: 总方案数 ...
- php实现字符串翻转,使字符串的单词正序,单词的字符倒序
如字符串'I love you'变成'I evol uoy',只能使用strlen(),不能使用其他内置函数. function strturn($str){ $pstr=''; $sstr=''; ...
- Go语言_包、变量和函数
包.变量和函数 学习 Go 程序的基本结构. Go 作者组编写,Go-zh 小组翻译. https://go-zh.org 包 每个 Go 程序都是由包构成的. 程序从 main 包开始运行. 本程序 ...
- vue 当前页跳转并强制刷新
watch: { '$route'(to, from) { this.$router.go(0); } }, this.$router.push({ path: '/dashboard/XXZX?' ...
- python 类的私有属性和方法 (转载)
转载:http://www.runoob.com/python/python-object.html 类属性与方法 类的私有属性 __private_attrs:两个下划线开头,声明该属性为私有,不能 ...
- 为什么集合类没有实现Cloneable和Serializable接口?
为什么集合类没有实现Cloneable和Serializable接口? 克隆(cloning)或者是序列化(serialization)的语义和含义是跟具体的实现相关的.因此,应该由集合类的具体实现来 ...