[英]Given a singly linked list how to find the mod of it with a number
Given a decimal number represented as list from least significant bit to most significant bit how should one find mod with k if the list were a number. 给定一个十进制数字,表示从最低有效位到最高有效位的列表,如果该列表是一个数字,应该如何用k查找mod。
Example :- number - 1234567 示例:-数字-1234567
list representation of number: 7->6->5->4->3->2->1 列表的数字表示形式:7-> 6-> 5-> 4-> 3-> 2-> 1
k = 7 k = 7
ans = 1234567%7 = 5 ans = 1234567%7 = 5
In python and java I could have converted the list to a number BigInteger(java) and have taken the mod. 在python和java中,我可以将列表转换为数字BigInteger(java)并采用mod。 But I wanted to do it without using BigInteger.
但是我想不使用BigInteger来做到这一点。
I have tried calculating the mod while iterating the list 我尝试在迭代列表时计算mod
total=0
for digit in numberList:
total = (total * 10 + digit) % k
return total
But this is not going to work. 但这是行不通的。
You can calculate the remainder of the i'th digit multiplied by 10^i for each digit. 您可以为每个数字计算第i个数字的余数乘以10 ^ i。 Then sum all these remainders and find the remainder of the sum :
然后将所有这些余数求和,找到总和的余数:
int mul = 1;
int remainder = 0;
for (int digit : digitArray)
{
remainder += (digit * mul) % k;
mul *= 10;
}
remainder = remainder % k;
For 1234567%7
, this loop basically computes : 对于
1234567%7
,此循环基本上计算:
(7 % 7 + 60 % 7 + 500 % 7 + 4000 % 7 + 30000 % 7 + 200000 % 7 + 1000000 % 7) % 7 =
(0 + 4 + 3 + 3 + 5 + 3 + 1) % 7 = 19 % 7 = 5
Your code is close, but you need to process your number list in reverse. 您的代码已关闭,但是您需要对号码列表进行反向处理。 I'll illustrate with a simple Python
list
. 我将用一个简单的Python
list
。
num = 1234567
# Construct numberList
numberList = []
n = num
while n:
n, d = divmod(n, 10)
numberList.append(d)
k = 7
print(num, numberList, num % k)
# Find numberList mod k
r = 0
for d in reversed(numberList):
r = (10 * r + d) % k
print(r)
output 产量
1234567 [7, 6, 5, 4, 3, 2, 1] 5
5
Think of the basic property of modular arithmetic : 考虑一下模块化算术的基本属性:
(a*b)%m=(a%m * b%m ) % m (a * b)%m =(a%m * b%m)%m
(a+b)%m=(a%m + b%m ) % m (a + b)%m =(a%m + b%m)%m
Now, to find modulo of 1234567 现在,找到1234567的模
1234567 % m = (1234 * 1000 + 567)%m =((1234 * 1000)%m + 567%m) %m 1234567%m =(1234 * 1000 + 567)%m =((1234 * 1000)%m + 567%m)%m
Now, take a little small no. 现在,取一点小号。 to better understand this trick
更好地理解这个技巧
12345 =( ( (1 * 10 + 2) * 10 + 3) * 10 + 4) *10 +5 12345 =((((1 * 10 + 2)* 10 + 3)* 10 + 4)* 10 +5
Now, 现在,
12345 % m = (((((((1*10)%m + 2)*10)%m + 3)*10)%m + 4)*10 + 5)%m 12345%m =((((((((((1 * 10)%m + 2)* 10)%m + 3)* 10)%m + 4)* 10 + 5)%m
Now to find modulo of a number that is represented by a linked list its actually easy 现在找到由链表表示的数字的模,实际上很容易
list1: 1->2->3->4->5....... list1:1-> 2-> 3-> 4-> 5 .......
take variable result and initialize with 0 取变量结果并用0初始化
ie, result=0 and just traverse the list and do: 即,结果= 0,然后遍历列表并执行:
result = (result*10 )% m + node->data 结果=(结果* 10)%m +节点->数据
see the code below it is too simple 看到下面的代码太简单了
int result=0;
while(list1){
int x=list1->data;
result=(result*10)%m + x;
list1=list1->next;
}
result=result%m;
return result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.