[LeetCode] Add Digits - 数字各个位数求和
LeetCode 数字 add 求和 digits
2023-09-14 08:56:52 时间
题目概述:
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
主要考察整数各个位数求和,当且仅当和小于10时输出。可能会走入整数转换为字符串的误区,直接使用取余(%)个位数相加即可,需要注意:
1.非负整数判断
2.输入0时输出0,输入10时输出1
3.至于O(1)时间复杂度考察数学公式:1 + (num-1) % 9
4.整数转换字符串可以类似思想:循环个位数转换reslut=num%10,num=num/10
我的代码:
int addDigits(int num) { int result; //负数跳出 if(num =0) return 0; if(num 10) return num; result=num; while(result =10) num=result; result=0; while(num =10) { //计算数字和 result+=num%10; //个位 num=num/10; result+=num; return result; }推荐代码:
public class Solution { public int addDigits(int num) { return 1 + (num-1) % 9; }题目笔记:
Roman to Integer 罗马数字转换考察switch和数字各个位数转换
int getNumToInt(char ch) { //此题需要注意switch写法和罗马数字计算 switch(ch) { case I: return 1; case V: return 5; case X: return 10; case L: return 50; case C: return 100; case D: return 500; case M: return 1000; default: return 0; return 0; int romanToInt(char* s) { 1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"} 10~90: {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"} 100~900: {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"} 1000~3000: {"M", "MM", "MMM"} 如果当前处理的字符对应的值和上一个字符一样,那么临时变量加上这个字符。比如III = 3 如果当前比前一个大,说明这一段的值应该是当前这个值减去前面记录下的临时变量中的值。比如IIV = 5 – 2 如果当前比前一个小,那么就可以先将临时变量的值加到结果中,然后开始下一段记录。比如VI = 5 + 1 DCXXI=500+100+10+10+1=621 int length; int i,j; int result; //结果 int last; //前一个值 int curr; //当前值 int num; //临时变量 length = strlen(s); last = getNumToInt(s[0]); num = last; for(i=1; i length; i++) { curr = getNumToInt(s[i]); if(curr == last) { num += curr; else if(curr last) { result += num; num = curr; else if(curr last) { num = curr - num; last = curr; result += num; return result; }
PS:我代码的缺点包括:我喜欢过于复杂的注释,而直观的代码好于注释;代码中的临时变量转换过多,需要更精简;字符串数组操作习惯计算长度后s[i],而不是使用*s。
(By:Eastmount 2015-9-8 下午6点半 http://blog.csdn.net/eastmount/)
LeetCode - #2 Add Two Numbers 我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。微博:@故胤道长)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。
LeetCode 2:两数相加 Add Two Numbers 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
# Leetcode 67:Add Binary(二进制求和) Leetcode 67:Add Binary(二进制求和) (python、java) Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 给定两个二进制字符串,返回他们的和(用二进制表示)。
相关文章
- leetcode 之LRU Cache(26)
- leetcode 之Permutation(七)
- Java实现 LeetCode 784 字母大小写全排列(DFS)
- Java实现 LeetCode 面试题62. 圆圈中最后剩下的数字(约瑟夫环)
- Java实现 LeetCode 514 自由之路
- Java实现 LeetCode 440 字典序的第K小数字
- Java实现 LeetCode 405 数字转换为十六进制数
- Java实现 LeetCode 378 有序矩阵中第K小的元素
- Java实现 LeetCode 374 猜数字大小
- Java实现 LeetCode 137 只出现一次的数字 II(二)
- Java实现LeetCode_0013_RomanToInteger
- Java实现 Leetcode 169 求众数
- LeetCode(23):合并K个排序链表
- [LeetCode] Valid Parentheses
- [LeetCode] Find Peak Element
- LeetCode(21):合并两个有序链表
- LeetCode-1780. 判断一个数字是否可以表示成三的幂的和【数学】
- LeetCode-429. N 叉树的层序遍历【层序遍历代码模板】
- LeetCode - 402 移掉 K 位数字
- Leetcode 1385. 两个数组间的距离值
- Leetcode 2190. 数组中紧跟 key 之后出现最频繁的数字(可以,一次过)
- Leetcode 1295. 统计位数为偶数的数字
- Leetcode 剑指 Offer 57. 和为s的两个数字(map查找速率快)
- Leetcode 只出现一次的数字
- leetcode 561. Array Partition I
- 【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
- 【LeetCode】 82.删除排序链表中的重复元素II