258. Add Digits

本文详细介绍了如何使用递归和非递归方法解决将任意非负整数连续加和至个位数的问题。通过观察规律,我们能够实现O(1)时间复杂度的解决方案。此外,文章还提供了如何从数字中提取各位数字的方法,包括使用取模和除法操作。

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 = 111 + 1 = 2. Since 2 has only one digit, return it.

Follow up:

Could you do it without any loop/recursion in O(1) runtime?


思路:看到题目,第一想法想到刚学C语言时候用程序实现算法题,就在A4纸上一点一点的推导,然后找规律;没错,确实是找规律。题目是当相加后的数字为个位数时候,返回该值。

① 由题意知,把一个数字中每个位置抽取出来再相加,(最后介绍如何获取数字的各位,其实就是相除、取模偷笑,剧透了!!!),但是会用到循环!!!

② 怎么实现O(1)复杂度,不用循环!!!如何实现?还是要观察规律抓狂观察好久有木有????????这样easy的题目观察那么久,也是需要努力了!!!奋斗


注意:① 比如给定一个值,num=12345。在C/C++等语言中,(num=12345)%10 =55+(12345/10 = 1234)=1239=num);---->循环--->【1239%10=9】+【1239/10=123】=132(=num);循环。。。直到num<10,也就是只要num>=10就接着循环,因为我们需要的是个位数啊!!!

② 观察法,找规律以完成O(1)复杂度,见后面!


方法1:with loop

class Solution{
public:
	int addDigits(int num) {
		while (num >= 10){
			/*int gw = num % 10;
			int syw = num / 10;
			num = gw + syw;*/
			num = num / 10 + num % 10;
		}
		return num;
	}
};


方法2:do it without any loop/recursion in O(1) runtime

class Solution{
public:
	int addDigits(int num){
		return 1 + (num - 1) % 9;	
	}
};

补充:如何把一个数字的各个位的数字抽取出来,常用的方法的是,数学中的相除取模法

程序语言中int型数字除以10获得是十位上的数字,除以100是百位上的数字,以此类推。而对10取模得到的是个位数字。具体见代码:

int num = 12345;// 初始化,以12345为例
vector<int> coll; // 存放各分位的数字
// 获得数字的各个分位上的数字
while (num)
{
	coll.push_back(num % 10);// 取模:存入个位数字
	num /= 10;// 相除:把num从个分位移到百分位,随着循环百分位移到千分位...
}
// 输出存到vector容器中的各分位的数字,结果为:5 4 3 2 1
copy(coll.begin(), coll.end(), ostream_iterator<int> (cout, " "));






/** * 大整数拆分 * @param num 一个大整数 * @return 分治后的两个局部大整数 */ public static testBigInteger[] splitNumber(testBigInteger num) { int n = num.digits.length; int mid = n/2; int[] low = Arrays.copyOfRange(num.digits,0,mid); int[] high = Arrays.copyOfRange(num.digits,mid,n); testBigInteger lowpart = new testBigInteger(low,false); testBigInteger highpart = new testBigInteger(high,false); return new testBigInteger[]{lowpart,highpart}; } /** * Karatsuba算法 * @param other 另一个大整数 * @return 乘法得到的结果 */ public testBigInteger karatsubamultiply(testBigInteger other){ if (this.digits.length <= 2 && other.digits.length <= 2) { return this.multipy(other); } testBigInteger[] p1 = splitNumber(this); testBigInteger p1Low = p1[0]; testBigInteger p1High = p1[1]; testBigInteger[] p2 = splitNumber(other); testBigInteger p2Low = p2[0]; testBigInteger p2High = p2[1]; testBigInteger z1 = p1High.karatsubamultiply(p2High); testBigInteger z2 = p1Low.karatsubamultiply(p2Low); testBigInteger z3 = (p1High.add(p1Low)).karatsubamultiply(p2High.add(p2Low)); testBigInteger middle = z3.subtract(z2).subtract(z1); int n =this.digits.length; int m =n/2; testBigInteger result = z1.shiftLeft(2*m).add(middle.shiftLeft(m)).add(z2); result.isNegative = this.isNegative != other.isNegative; if (result.isZero()) result.isNegative = false; return result; } /** * 左移操作 * @param m 进位的多少 * @return 返回数组*10^m次方后的结果 */ public testBigInteger shiftLeft(int m) { if(m==0) return this; int[] newDigits = new int[this.digits.length+m] ; System.arraycopy(this.digits,0,newDigits,0,this.digits.length); return new testBigInteger(newDigits,false);} 计算结果不对怎么办
最新发布
12-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值