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)类型的变量,在某些特殊情况下仍然会被“截断”,这 ...
随机推荐
- MongoDB ODM
安装 pip3 install mongoengine 连接MongoDB 方法一:简写 connect('students) 方法二:指定端口和地址 connect('students',host= ...
- mooc-IDEA alter enter--008
十四.IntelliJ IDEA -alter enter 1.提示一:自动创建函数 2.提示二.list replace 等于 3.提示三.字符串formate或build 等于 等于 等于 4.实 ...
- 关于VMware中的几个网络模式
直接参考别人的: 写的已经很细致了: http://blog.csdn.net/yaoyaowugui/article/details/7422388 关键是看别人的几张图
- Oracle基本操作练习(一)
--创建表空间 create tablespace test datafile 'c:\test.dbf' size 100m autoextend on next 10m; --删除表空间 drop ...
- Windows下通过GitHub+Hexo搭建个人博客的步骤
Windows下通过GitHub+Hexo搭建个人博客的步骤 https://blog.csdn.net/namechenfl/article/details/90442312 https://bl ...
- indexOf与includes的区别
indexOf与includes的区别:https://blog.csdn.net/gtLBTNq9mr3/article/details/78700118 includes和indexOf相比较:相 ...
- CSS的置换和非置换元素
一个来自面试的坑. 面试的时候考官先问了行内元素和块级元素的区别,这个不难理解.然后一脚就踩进了,置换元素的坑.例如img就是行内置换元素,这种行内元素是可以设置宽高的. 什么是置换元素 一个内容不受 ...
- 【JMeter4.0】一、JAVA环境-JDK1.10安装与配置
环境变量的作用: 它是操作系统用来指定运行环境的一些参数.比如临时文件夹位置和系统文件夹位置等.当你运行某些程序时,除了在当前文件夹中寻找外,还会到这些环境变量中去查找,比如“Path”就是一个变量, ...
- 攻防世界--dmd-50
测试文件:https://adworld.xctf.org.cn/media/task/attachments/7ef7678559ea46cbb535c0b6835f2f4d 1.准备 获取信息 6 ...
- 抓包工具Charles简单使用介绍(可抓取Android中app的请求)
摘自: 作者:Roy_Liang链接:http://www.jianshu.com/p/5539599c7a25 Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下 ...