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

📅  最后修改于: 2021-04-24 04:59:22             🧑  作者: 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个索引将出现3,依此类推。
还有一点是,最后一个元素的出现将仅限于一个。最后第二个元素将被限制为一和十。最后3位将达到100位,以此类推。
根据以上几点,找出总和。

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
?>


输出 :

8421