📌  相关文章
📜  表示数字的字符串的所有子字符串的总和 |套装 2(恒定额外空间)

📅  最后修改于: 2021-09-22 10:17:36             🧑  作者: Mango

给定一个表示数字的字符串,我们需要得到这个字符串的所有可能子字符串的总和。
例子 :

Input : s = "6759"
Output : 8421
sum = 6 + 7 + 5 + 9 + 67 + 75 + 
      59 + 675 + 759 + 6759
      = 8421

Input : s = "16"
Output : 23
sum = 1 + 6 + 16 = 23

我们在下面的帖子中讨论了一个解决方案。
表示数字的字符串的所有子字符串的总和 |设置 1
该解决方案基于不使用任何额外空间的不同方法。
这个问题可以看如下。
让数字为 s = “6759”

1    10    100    1000
    6   1     1      1       1
    7   2     2      2
    5   3     3
    9   4

上表表明,当所有子字符串进一步转换为个、十、百等形式时,字符串的每个索引都会有一些固定的出现。第一个索引将有 1 次出现,每个一个,十个等等。第二个将有 2,第三个将有 3,依此类推。
还有一点是最后一个元素的出现将仅限于一个。最后的第二个元素将被限制为一个和十个。最后 3rd 将达到一百等。
从以上几点让我们找出总和。

sum = 6*(1*1 + 1*10 + 1*100 + 1*1000) + 7*(2*1 + 2*10 + 2*100) + 
      5*(3*1 + 3*10) + 9*(4*1)
    = 6*1*(1111) + 7*2*(111) + 5*3*(11) + 9*4*(1)
    = 6666 + 1554 + 165 + 36
    = 8421

现在,为了处理乘法,我们将有一个从 1 开始的乘法因子。从示例中可以清楚地看出乘法因子(反向)是 1、11、111……等等。所以乘法将基于三个因素。数字、它的索引和一个乘数。

C++
// C++ program to print sum of all substring of
// a number represented as a string
#include 
using namespace std;
 
// Returns sum of all substring of num
int sumOfSubstrings(string num)
{
    long long int sum = 0; // Initialize result
 
    // Here traversing the array in reverse
    // order.Initializing loop from last
    // element.
    // mf is multiplying factor.
    long long int mf = 1;
    for (int i=num.size()-1; i>=0; i--)
    {
        // Each time sum is added to its previous
        // sum. Multiplying the three factors as
        // explained above.
        // s[i]-'0' is done to convert char to int.
        sum += (num[i]-'0')*(i+1)*mf;
 
        // Making new multiplying factor as
        // explained above.
        mf = mf*10 + 1;
    }
 
    return sum;
}
 
//  Driver code to test above methods
int main()
{
    string num = "6759";
    cout << sumOfSubstrings(num) << endl;
    return 0;
}


Java
// Java program to print sum of all substring of
// a number represented as a string
import java.util.Arrays;
 
public class GFG {
     
    // Returns sum of all substring of num
    public static long sumOfSubstrings(String num)
    {
        long sum = 0; // Initialize result
      
        // Here traversing the array in reverse
        // order.Initializing loop from last
        // element.
        // mf is multiplying factor.
        long mf = 1;
        for (int i = num.length() - 1; i >= 0; i --)
        {
            // Each time sum is added to its previous
            // sum. Multiplying the three factors as
            // explained above.
            // s[i]-'0' is done to convert char to int.
            sum += (num.charAt(i) - '0') * (i + 1) * mf;
      
            // Making new multiplying factor as
            // explained above.
            mf = mf * 10 + 1;
        }
      
        return sum;
    }
     
          
    //  Driver code to test above methods
    public static void main(String[] args)
    {
        String num = "6759";
             
        System.out.println(sumOfSubstrings(num));
             
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 program to print sum of all substring of
# a number represented as a string
 
# Returns sum of all substring of num
def sumOfSubstrings(num):
 
    sum = 0 # Initialize result
 
    # Here traversing the array in reverse
    # order.Initializing loop from last
    # element.
    # mf is multiplying factor.
    mf = 1
    for i in range(len(num) - 1, -1, -1):
 
        # Each time sum is added to its previous
        # sum. Multiplying the three factors as
        # explained above.
        # int(s[i]) is done to convert char to int.
        sum = sum + (int(num[i])) * (i + 1) * mf
 
        # Making new multiplying factor as
        # explained above.
        mf = mf * 10 + 1
 
    return sum
 
# Driver code to test above methods
if __name__=='__main__':
    num = "6759"
    print(sumOfSubstrings(num))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to print sum of all substring of
// a number represented as a string
using System;
         
public class GFG {
     
    // Returns sum of all substring of num
    public static long sumOfSubstrings(string num)
    {
         
        long sum = 0; // Initialize result
     
        // Here traversing the array in reverse
        // order.Initializing loop from last
        // element.
        // mf is multiplying factor.
        long mf = 1;
         
        for (int i = num.Length - 1; i >= 0; i --)
        {
             
            // Each time sum is added to its previous
            // sum. Multiplying the three factors as
            // explained above.
            // s[i]-'0' is done to convert char to int.
            sum += (num[i] - '0') * (i + 1) * mf;
     
            // Making new multiplying factor as
            // explained above.
            mf = mf * 10 + 1;
        }
     
        return sum;
    }
     
         
    // Driver code to test above methods
    public static void Main()
    {
        string num = "6759";
             
        Console.WriteLine(sumOfSubstrings(num));
             
    }
}
 
// This code is contributed by Sam007.


PHP
= 0; $i--)
    {
        // Each time sum is added to
        // its previous sum. Multiplying
        // the three factors as explained above.
        // s[i]-'0' is done to convert char to int.
        $sum += ($num[$i] - '0') * ($i + 1) * $mf;
 
        // Making new multiplying
        // factor as explained above.
        $mf = $mf * 10 + 1;
    }
 
    return $sum;
}
 
// Driver Code
$num = "6759";
echo sumOfSubstrings($num), "\n";
 
// This code is contributed by m_kit
?>


Javascript


输出 :

8421

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程