解题思路:传统方法,按位相加,循环,直至只有一位为止。
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/
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
⾃⼰当时想的解法(但⽤了循环和判断) class Solution(object): def addDigits(self, num): while True: if num>9: s=0 for i in range (len(str(num))): s=int(str(num)[i])+s num=s else: break return num 下⾯...
...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....|-----|---------------- | --------------- |...
示例:输入: 38输出: 2解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。public int addDigits(int num) {
(./solutions/AddDigits.cc) | 104 | | [MaxDepthBinaryTree.cc] (./solutions/MaxDepthBinaryTree.cc) | 第226话| [InvertBinaryTree.cc] (./solutions/InvertBinaryTree.cc) | 第283话| [MoveZeros.cc] (./...
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实现 桶排序 ...Digits 二叉树最深 104. Maximum Depth of Binary Tree 【递归】 遍历求单个数字 136. Single Number 石头游戏 292. Nim Gam
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。 示例: 输入: 38 输出: 2 ... def addDigits(self, num: int) -> int: while num>=10: digit=list(map(int,str(num))) num=0
public static int addDigits(int x) { } 接下来,编写一种方法来检查给定的String是否是回文。 请记住,回文词是向前和向后相同的词。 (您可能要考虑一种类似于class中的nestParens示例的方法)。 此方法应具有...
Digits (num-1)%9+1 389-383-387-242 都是很类似题 第一种解法:哈希表(key:character,value:次数或者index) 389findDifference:第一次遍历向hashmap中存string1,key-character,value-出现次数,第二次遍历...
cordova plugin add https://github.com/cosmith/cordova-digits.git --variable FABRIC_API_KEY=your_api_key --variable FABRIC_API_SECRET=your_api_secret 用法 目前仅实现一种方法,该方法显示身份验证屏幕。 ...
#(Day7)---------Add Digits #(Day8)---- -----Rotate Arrays #(Day9)---------Intersection of Arrays #(Day10)--------3SUM #(Day11)--------优化了重复字符串程序并解决了2Sum程序....#(Day12)--------解决了...
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 ...
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 ...
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 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 ...
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 ...
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 ...
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 ...