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)类型的变量,在某些特殊情况下仍然会被“截断”,这 ...
随机推荐
- python实现获取文件的绝对路径
实现代码如下: #获取文件的绝对路径import osclass GetPath: def get_path(self,path): r=os.path.abspath(path) return ri ...
- python实现压缩文件成zip格式
实现代码如下: #压缩文件 import time,zipfile class zip: def get_zip(self,files,zip_name): zp=zipfile.ZipFile(zi ...
- [LeetCode] 137. Single Number II (位操作)
传送门 Description Given an array of integers, every element appears three times except for one, which ...
- 《STL源码剖析》——第七、八章:仿函数与接配器
第七章:仿函数 7.1.仿函数(函数对象)概观 STL仿函数的分类,若以操作数(operand)的个数划分,可分为一元和二元仿函数,若以功能划分,可分为算术运算(Arithmetic).关系运算(R ...
- vuejs基础-事件修饰符
事件修饰符: .stop 阻止冒泡 .prevent 阻止默认事件 .capture 添加事件侦听器时使用事件捕获模式 .self 只当事件在该元素本身(比如不是子元素)触发时触发回调 .once 事 ...
- flex布局相关用法
/* pages/classic/classic.wxss */ .chunk { /* 行内元素可设置但是设置了flex,无效了 *//* display: inline-block; */ wid ...
- 通过JS,用a标签代替form中的submit
---恢复内容开始--- 有时候在使用表单的时候,不一定会用到表单中的input_submit来提交表单数据,可能会用a.button等来代替 然后自然而然地想到了用JS中的提交表单数据的动作 < ...
- vue 使用 computed 结合 filter 实现数据的的过滤和排序
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- java_第一年_JavaWeb(6)
会话 会话:浏览器从打开一个进程访问服务器到该浏览器关闭,我们称之为一个会话: 在浏览器和服务器交互期间,会不可避免地产生一些数据,而为了为每个用户保存其对应的数据,可使用两种技术:Cookie和Se ...
- 洛谷 P1589 泥泞路 & 2019青岛市竞赛(贪心)
题目链接 https://www.luogu.org/problemnew/show/P1589 解题思路 用结构体存下每一段泥泞路的左端点和右端点,然后用sort根据左端点排序,采用贪心的思想,从左 ...