`
songlj
  • 浏览: 15824 次
社区版块
存档分类
最新评论

Add Digits

 
阅读更多

解题思路:传统方法,按位相加,循环,直至只有一位为止。

Java代码实现:

public class Solution {
    public int addDigits(int num) {
        int sum=0;
		//System.out.println(num);
		while(num/10>0){
			String strNum=String.valueOf(num);
			//System.out.println(strNum.length());
			for(int i=0;i<strNum.length();i++){
				sum+=Integer.valueOf(strNum.charAt(i))-48;
				//System.out.println(sum);
			}
			System.out.println(sum);
			num=sum;
			sum=0;
		}
		sum=num;
        return sum;
    }
}
原题地址:https://leetcode.com/problems/add-digits/







版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

    leetcode题目之Add Digits(非负整数各位相加)

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

    给定一个非负整数num,反复将各个位上的数字相加,直到结果为一位数 .pdf

    ⾃⼰当时想的解法(但⽤了循环和判断) class Solution(object): def addDigits(self, num): while True: if num&gt;9: s=0 for i in range (len(str(num))): s=int(str(num)[i])+s num=s else: break return num 下⾯...

    LeetCode最全代码

    ...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...

    cyxpdc#Algorithm#(简单)各位相加1

    示例:输入: 38输出: 2解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。public int addDigits(int num) {

    leetcode338-MyLeetCode:我对LeetCode问题的解答

    (./solutions/AddDigits.cc) | 104 | | [MaxDepthBinaryTree.cc] (./solutions/MaxDepthBinaryTree.cc) | 第226话| [InvertBinaryTree.cc] (./solutions/InvertBinaryTree.cc) | 第283话| [MoveZeros.cc] (./...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree ...Add Digits #260 Single Number III #274 H-Index #283 Move Zeroes #292 Nim Game #318 Maximum P

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    LeetCode去除数组重复元素 Arithmetic-Swift 一些算法的swift实现 桶排序 ...Digits 二叉树最深 104. Maximum Depth of Binary Tree 【递归】 遍历求单个数字 136. Single Number 石头游戏 292. Nim Gam

    面试算法必考(3)leetcode

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。 示例: 输入: 38 输出: 2 ... def addDigits(self, num: int) -&gt; int: while num&gt;=10: digit=list(map(int,str(num))) num=0

    s03-recursion-PMacStew:s03-recursion-PMacStew由GitHub Classroom创建

    public static int addDigits(int x) { } 接下来,编写一种方法来检查给定的String是否是回文。 请记住,回文词是向前和向后相同的词。 (您可能要考虑一种类似于class中的nestParens示例的方法)。 此方法应具有...

    vs没报错leetcode报错-leetcode-easy-medium:leetcode-easy-medium

    Digits (num-1)%9+1 389-383-387-242 都是很类似题 第一种解法:哈希表(key:character,value:次数或者index) 389findDifference:第一次遍历向hashmap中存string1,key-character,value-出现次数,第二次遍历...

    cordova-digits:Digits.com的Cordova插件

    cordova plugin add https://github.com/cosmith/cordova-digits.git --variable FABRIC_API_KEY=your_api_key --variable FABRIC_API_SECRET=your_api_secret 用法 目前仅实现一种方法,该方法显示身份验证屏幕。 ...

    多线程leetcode-100-days-grinding:100天研磨

    #(Day7)---------Add Digits #(Day8)---- -----Rotate Arrays #(Day9)---------Intersection of Arrays #(Day10)--------3SUM #(Day11)--------优化了重复字符串程序并解决了2Sum程序....#(Day12)--------解决了...

    LeetCode2 Add Two Numbers

    The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading ...

    add-two-numbers

    The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading ...

    AddTwoNumbers

    The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading ...

    HugeInteger大整数计数器作业

    HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For ...

    Android代码-DialerLoading

    Add DialerLoadingView: To start loading, call dial method: rotary_dialer.dial( endless = true // enable endless mode (default is true) digits = arrayIntOf(1,2,3) // numbers to dial ) To stop ...

    leetcode2sumc-add-two-numbers-solution:我的LeetCodeC解决方案:添加两个数字

    digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading ...

    8051助记符/地址对照表

    ADD, ADDC - Add Accumulator (With Carry) AJMP - Absolute Jump ANL - Bitwise AND CJNE - Compare and Jump if Not Equal CLR - Clear Register CPL - Complement Register DA - Decimal Adjust DEC - Decrement ...

Global site tag (gtag.js) - Google Analytics