数字的子串之和

发布于 2024-10-08 14:01:50 字数 603 浏览 0 评论 0原文

求一个数字的子串之和的最佳解决方案是什么?

例如,Sum (123) = 1 + 2 + 3 + 12 + 23 + 123 = 164。

我认为是 O(n^2)。因为

sum = 0
for i in number: // O(n)
    sum += startwith(i) // O(n)
return sum

有最优解吗?最好的方法是什么?

这是我的解决方案,但 O(n^2):

public static int sumOfSubstring(int i) {
  int sum = 0;

  String s = Integer.toString(i);

  for (int j = 0, bound = s.length(); j < bound; j++) {
   for (int k = j; k < bound; k++) {
    String subString = s.subSequence(j, k + 1).toString();
    sum += Integer.valueOf(subString);
   }
  }

  return sum;
 }

What is the optimal solution to find the sum of substring of a number ?

For example, Sum (123) = 1 + 2 + 3 + 12 + 23 + 123 = 164.

I think it is O(n^2). because

sum = 0
for i in number: // O(n)
    sum += startwith(i) // O(n)
return sum

Any optimal solution? What is the best approach?

Here is my solution but O(n^2):

public static int sumOfSubstring(int i) {
  int sum = 0;

  String s = Integer.toString(i);

  for (int j = 0, bound = s.length(); j < bound; j++) {
   for (int k = j; k < bound; k++) {
    String subString = s.subSequence(j, k + 1).toString();
    sum += Integer.valueOf(subString);
   }
  }

  return sum;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦与时光遇 2024-10-15 14:01:50

请注意:

  • 对于数字 XY,您有 11X + 2Y。
  • 对于数字 XYZ,您有 111X + 22Y + 3Z。
  • 对于 WXYZ,您有 1111W + 222X + 33Y + 4Z。

这是我的 C# 实现,尽管移植到 Java 应该很简单:

static long SumSubtring(String s)
{
    long sum = 0, mult = 1;
    for (int i = s.Length; i > 0; i--, mult = mult * 10 + 1)
        sum += (s[i - 1] - '0') * mult * i;
    return sum;
}

请注意,它实际上是 O(n)。

Observe that:

  • For the number XY you have 11X + 2Y.
  • For the number XYZ you have 111X + 22Y + 3Z.
  • For WXYZ, you have 1111W + 222X + 33Y + 4Z.

Here's my C# implementation, though it should be trivial to port to Java:

static long SumSubtring(String s)
{
    long sum = 0, mult = 1;
    for (int i = s.Length; i > 0; i--, mult = mult * 10 + 1)
        sum += (s[i - 1] - '0') * mult * i;
    return sum;
}

Note that it is effectively O(n).

娜些时光,永不杰束 2024-10-15 14:01:50

对于长度为 n 的给定字符串,肯定有 ~N^2 个可能的子串。但是,我们可以使用以下等式在线性时间内计算总和:

alt text

S 代表数字序列 ( s0, s1, s2, ... , sn)。

对于S=<1,2,3>它返回 111*1+22*2+3*3=164

请注意,如果我们事先计算 10 的 N 次方,或者在循环过程中逐步计算,则运行时间是线性

There are definitely ~N^2 possible substrings of a given string of length n. However, we CAN compute the sum in linear time, using the following equation:

alt text

S stands for the sequence of digits (s0, s1, s2, ... , sn).

For S=<1,2,3> it returns 111*1+22*2+3*3=164

Note that the running time is linear if we compute the N powers of 10 beforehand, or progressively during the loop.

水晶透心 2024-10-15 14:01:50

正如 @Gabe 提供的,你可以这样做:

A0 = 1,
A1 = A0*10 + 1,
...
An-1 = An-2 * 10 + 1,

你可以在 O(n) 中计算 A0-An

a[0] = 1;
for (int i=1;i<n;i++)
 a[i] = a[i - 1] * 10 + 1;

现在计算 b[i]:

b[0] = a[0] * n
b[1] = a[1] * (n-1)
...

你可以在 O(n) 中计算所有 b[i]

现在有些是
[伪代码]

for (int i=0;i<n;i++)
   sum += S[n-i - 1] * b[i]

As @Gabe offered you can do:

A0 = 1,
A1 = A0*10 + 1,
...
An-1 = An-2 * 10 + 1,

you can compute A0-An in O(n)

a[0] = 1;
for (int i=1;i<n;i++)
 a[i] = a[i - 1] * 10 + 1;

now compute b[i]:

b[0] = a[0] * n
b[1] = a[1] * (n-1)
...

you can compute all b[i] in O(n)

Now the some is
[Pseudo code]

for (int i=0;i<n;i++)
   sum += S[n-i - 1] * b[i]
等风也等你 2024-10-15 14:01:50

上面所有的答案看起来都很棒。我最近正在解决类似的问题。上面给出的公式效果很好,但是正如您所看到的,随着字符串长度的增加,计算变得困难并且解决方案非常大。通常,当字符串的长度非常大时,您会被要求在 MOD 后给出一个很大的数字(例如 1000000007)。因此,现在您可以使用一点模块化算术轻松计算值,或者具体地说 模幂乘法逆元。因此,针对大输入修改后的新公式可以写为:
假设。

  • Modular_exp() 是计算 a^b % c 乘法逆变量的值的函数
  • ,它是 9 的乘法逆元,即 111111112,可以使用相同的 modular_exp() 找到函数,但在这里我只是硬编码它。
  • len 是仅包含从 '0' 到 '9' 字符的字符串的总长度;

这是代码:

FOR(i, len) {
    coef = (( ( modular_exp(10, len - i, MOD) - 1) * multiinverse ) % MOD) * (i + 1) % MOD;
    res += ( coef * (s[i] - '0') ) % MOD;
}
printf("%lld\n", res % MOD );

就是这样。

All the above answers looks great. I was recently solving a similar problem. The formula presented above works well but as you can see as the length of the string increases the computation becomes difficult and the solution really large. Usually when the length of the string is really large you will be asked to give the answer after MOD a large number say 1000000007. So now you can easily compute the values using little bit of modular Arithmetic, or to be specific Modular exponentiation and Multiplicative inverse. So the new formula after modification for large inputs can be written as.
Assumption made.

  • Modular_exp() is the function that computes the value of a^b % c
  • multiplicative inverse variable is the multiplicative inverse of 9 which is 111111112, which can be found out using the same modular_exp() function, but here I just hard coded it.
  • len is the total length of the string that has only characters from '0' to '9';

Here is the code:

FOR(i, len) {
    coef = (( ( modular_exp(10, len - i, MOD) - 1) * multiinverse ) % MOD) * (i + 1) % MOD;
    res += ( coef * (s[i] - '0') ) % MOD;
}
printf("%lld\n", res % MOD );

That's it.

焚却相思 2024-10-15 14:01:50

FWIW N 位数字相加的整数个数似乎是

N + N-1 + N-2 ... 1

这是一个三角数(阶乘的加法等价)
http://en.wikipedia.org/wiki/Triangle_number

添加的数量
N^2 + N / 2

但是,这没有考虑拆分数字所需的工作

FWIW the number of integers to add for number of N digits appears to be

N + N-1 + N-2 ... 1

Which is a triangular number (additive equivalent of a factorial)
http://en.wikipedia.org/wiki/Triangular_number

The number of additions
N^2 + N / 2

However, this doesn't consider the work needed to split out the digits

蓝礼 2024-10-15 14:01:50

这不是一个新答案,而是对加布给出的已接受答案的阐述。

假设数字是 19,则总和为

1+9+19
= 1+ 9 + (10*1+9)
= 11*1 + 2*9

如果数字为 486,则总和为

= 4 + 8 + 6 + 48+ 86 +486
= 4 + 8 + 6 + (10*4+8) + (10*8+6) + (100*4+10*8+6)
= 111*4+22*8+3*6

所以一般而言,如果数字表示为数字字符串“XY”,则子字符串的总和将是该数字可以计算为

sum of XY  = X +Y + (10X+Y) 
= 11X+2Y

sum of XYZ = X + Y + Z + (10X + Y)+ (10 Y+ Z) + (100X+ 10Y+Z)
= 111X+22Y+3Z

sum of XYZW = x+ y+ z + w + (10x + y) + (10y+ z)+ (10z+ w)+(100X+ 10Y+Z)+(100y+ 10z+w)+(1000x+100y+10z+w)
=1111x+222y+33z+4w

对于 9 位数字总和是

(9 times 1)*1st + (8 times 2)*2nd+ (7 times 3)*3rd + (6 times 4)*4th+(5 times 5)*5th +(4 times 6)*6th  +(3 times 7)*7th+(3 times 8)*8th+(3 times 9)*9th

Not a new answer but elaboration of accepted answer given by Gabe.

Say number is 19 then sum is

1+9+19
= 1+ 9 + (10*1+9)
= 11*1 + 2*9

If number is 486 then sum is

= 4 + 8 + 6 + 48+ 86 +486
= 4 + 8 + 6 + (10*4+8) + (10*8+6) + (100*4+10*8+6)
= 111*4+22*8+3*6

So in general term if a number is represented as a string of digits "XY" then sum of sub-strings will bw that number can be calculated as

sum of XY  = X +Y + (10X+Y) 
= 11X+2Y

sum of XYZ = X + Y + Z + (10X + Y)+ (10 Y+ Z) + (100X+ 10Y+Z)
= 111X+22Y+3Z

sum of XYZW = x+ y+ z + w + (10x + y) + (10y+ z)+ (10z+ w)+(100X+ 10Y+Z)+(100y+ 10z+w)+(1000x+100y+10z+w)
=1111x+222y+33z+4w

For 9 digit number sum is

(9 times 1)*1st + (8 times 2)*2nd+ (7 times 3)*3rd + (6 times 4)*4th+(5 times 5)*5th +(4 times 6)*6th  +(3 times 7)*7th+(3 times 8)*8th+(3 times 9)*9th
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文