📜  从给定的字符串提取最大数值|第一组(一般方法)

📅  最后修改于: 2021-05-04 07:26:55             🧑  作者: Mango

给定一个字母数字字符串,请从该字符串提取最大数值。字母只会小写。
这里讨论一种解决问题的方法,在第2组中给出了使用正则表达式的另一种方法
例子:

Input : 100klh564abc365bg
Output : 564
Maximum numeric value among 100, 564 
and 365 is 564.

Input : abchsd0sdhs
Output : 0

它的解决方案很简单,即开始遍历字符串并执行两个操作:

  1. 如果当前索引处存在数值,则将其转换为整数
    num = num*10 + (str[i]-'0') 
  2. 否则,更新最大值并重置num = 0。

最后返回最大值。

C++
// C++ program to extract the maximum value
#include
using namespace std;
  
// Function to extract the maximum value
int extractMaximum(string str)
{
    int num = 0, res = 0;
  
    // Start traversing the given string
    for (int i = 0; i= '0' && str[i] <= '9')
            num = num * 10 + (str[i]-'0');
  
        // Update maximum value
        else
        {
            res = max(res, num);
  
            // Reset the number
            num = 0;
        }
    }
  
    // Return maximum value
    return max(res, num);
}
  
// Driver program
int main()
{
    string str = "100klh564abc365bg";
    cout << extractMaximum(str);
    return 0;
}


Java
// Java program to extract the maximum value
  
class GFG 
{
    // Method to extract the maximum value
    static int extractMaximum(String str)
    {
        int num = 0, res = 0;
       
        // Start traversing the given string
        for (int i = 0; i


Python3
# Python 3 program to extract
# the maximum value
def extractMaximum(ss):
    num, res = 0, 0
      
    # start traversing the given string 
    for i in range(len(ss)):
          
        if ss[i] >= "0" and ss[i] <= "9":
            num = num * 10 + int(int(ss[i]) - 0)
        else:
            res = max(res, num)
            num = 0
          
    return max(res, num)
  
# Driver Code
ss = "100klh564abc365bg"
  
print(extractMaximum(ss))
  
# This code is contributed
# by mohit kumar 29


C#
// C# program to extract the maximum value
using System;
  
class GFG {
      
    // Method to extract the maximum value
    static int extractMaximum(String str)
    {
        int num = 0, res = 0;
      
        // Start traversing the given string
        for (int i = 0; i < str.Length; i++)
        {
              
            // If a numeric value comes, start 
            // converting it into an integer 
            // till there are consecutive
            // numeric digits
            if (char.IsDigit(str[i]))
                num = num * 10 + (str[i]-'0');
      
            // Update maximum value
            else
            {
                res = Math.Max(res, num);
      
                // Reset the number
                num = 0;
            }
        }
      
        // Return maximum value
        return Math.Max(res, num);
    }
      
    // Driver method
    public static void Main() 
    {
        String str = "100klh564abc365bg";
          
        Console.Write(extractMaximum(str));
    } 
}
  
// This code is contributed by nitin mittal.


PHP
= '0' && $str[$i] <= '9')
            $num = $num * 10 + ($str[$i]-'0');
  
        // Update maximum value
        else
        {
            $res = max($res, $num);
  
            // Reset the number
            $num = 0;
        }
    }
  
    // Return maximum value
    return max($res, $num);
}
  
    // Driver Code
    $str = "100klh564abc365bg";
    echo extractMaximum($str);
  
// This code is contributed by nitin mittal.
?>


C++
// C++ program for above implementation
#include
using namespace std;
  
// Utility function to find maximum string
string maximumNum(string curr_num, string res)
{
    int len1 = curr_num.length();
    int len2 = res.length();
  
    // If both having equal lengths
    if (len1 == len2)
    {
        // Reach first unmatched character / value
        int i = 0;
        while (curr_num[i]== res[i])
            i++;
  
        // Return string with maximum value
        if (curr_num[i] < res[i])
            return res;
        else
            return curr_num;
    }
  
    // If different lengths
    // return string with maximum length
    return len1 < len2 ? res: curr_num;
}
  
// Function to extract the maximum value
string extractMaximum(string str)
{
    int n = str.length();
    string curr_num ="";
    string res;
  
    // Start traversing the string
    for (int i = 0; i='0' && str[i]<='9')
        {
            curr_num = curr_num + str[i];
            i++;
        }
  
        if (i == n)
            break;
  
        if (curr_num.size() > 0)
            i--;
  
        // Update maximum string
        res = maximumNum(curr_num, res);
  
        curr_num = "";
    }
  
    // To handle the case if there is only
    // 0 numeric value
    if (curr_num.size()== 0 && res.size()== 0)
        res = res + '0';
  
    // Return maximum string
    return maximumNum(curr_num, res);
}
  
// Drivers program
int main()
{
    string str ="100klh564abc365bg";
    cout << extractMaximum(str) << endl;
    return 0;
}


Java
// Java program for above implementation
  
class GFG 
{
    // Utility method to find maximum string
    static String maximumNum(String curr_num, String res)
    {
        int len1 = curr_num.length();
        int len2 = res.length();
       
        // If both having equal lengths
        if (len1 == len2)
        {
            // Reach first unmatched character / value
            int i = 0;
            while (curr_num.charAt(i) == res.charAt(i))
                i++;
       
            // Return string with maximum value
            if (curr_num.charAt(i) < res.charAt(i))
                return res;
            else
                return curr_num;
        }
       
        // If different lengths
        // return string with maximum length
        return len1 < len2 ? res: curr_num;
    }
       
    // Method to extract the maximum value
    static String extractMaximum(String str)
    {
        int n = str.length();
        String curr_num ="";
        String res="";
       
        // Start traversing the string
        for (int i = 0; i 0)
                i--;
       
            // Update maximum string
            res = maximumNum(curr_num, res);
       
            curr_num = "";
        }
       
        // To handle the case if there is only
        // 0 numeric value
        if (curr_num.length() == 0 && res.length() == 0)
            res = res + '0';
       
        // Return maximum string
        return maximumNum(curr_num, res);
    }
      
    // Driver method
    public static void main(String[] args) 
    {
        String str = "100klh564abc365bg";
        System.out.println(extractMaximum(str));
    }    
}


Python3
# Python3 program for above implementation
  
# Utility function to find maximum string
def maximumNum(curr_num, res):
  
    len1 = len(curr_num);
    len2 = len(res);
  
    # If both having equal lengths
    if (len1 == len2):
          
        # Reach first unmatched character / value
        i = 0;
        while (curr_num[i]== res[i]):
            i += 1;
  
        # Return string with maximum value
        if (curr_num[i] < res[i]):
            return res;
        else:
            return curr_num;
  
    # If different lengths
    # return string with maximum length
    return res if(len1 < len2) else curr_num;
  
# Function to extract the maximum value
def extractMaximum(str):
  
    n = len(str);
    curr_num = "";
    res = "";
  
    # Start traversing the string
    for i in range(n):
          
        # Ignore leading zeroes
        while (i < n and str[i]=='0'):
            i += 1;
  
        # Store numeric value into a string
        while (i < n and str[i] >= '0' and 
                         str[i] <= '9'):
            curr_num += str[i];
            i += 1;
  
        if (i == n):
            break;
  
        if (len(curr_num) > 0):
            i -= 1;
  
        # Update maximum string
        res = maximumNum(curr_num, res);
  
        curr_num = "";
  
    # To handle the case if there is only
    # 0 numeric value
    if (len(curr_num) == 0 and len(res) == 0):
        res += '0';
  
    # Return maximum string
    return maximumNum(curr_num, res);
  
# Driver Code
str ="100klh564abc365bg";
print(extractMaximum(str));
      
# This code is contributed by mits


C#
// C# program for above implementation 
using System;
class GFG 
{ 
    // Utility method to find maximum string 
    static String maximumNum(string curr_num, string res) 
    { 
        int len1 = curr_num.Length; 
        int len2 = res.Length; 
      
        // If both having equal lengths 
        if (len1 == len2) 
        { 
            // Reach first unmatched character / value 
            int i = 0; 
            while (curr_num[i] == res[i]) 
                i++; 
      
            // Return string with maximum value 
            if (curr_num[i] < res[i]) 
                return res; 
            else
                return curr_num; 
        } 
      
        // If different lengths 
        // return string with maximum length 
        return len1 < len2 ? res: curr_num; 
    } 
      
    // Method to extract the maximum value 
    static string extractMaximum(string str) 
    { 
        int n = str.Length; 
        string curr_num =""; 
        string res=""; 
      
        // Start traversing the string 
        for (int i = 0; i 0) 
                i--; 
      
            // Update maximum string 
            res = maximumNum(curr_num, res); 
      
            curr_num = ""; 
        } 
      
        // To handle the case if there is only 
        // 0 numeric value 
        if (curr_num.Length == 0 && res.Length == 0) 
            res = res + '0'; 
      
        // Return maximum string 
        return maximumNum(curr_num, res); 
    } 
      
    // Driver method 
    public static void Main() 
    { 
        string str = "100klh564abc365bg"; 
        Console.WriteLine(extractMaximum(str)); 
    }     
} 
// This code is contributed by mits


PHP
='0' && $str[$i]<='9')
        {
            $curr_num .= $str[$i];
            $i++;
        }
  
        if ($i == $n)
            break;
  
        if (strlen($curr_num) > 0)
            $i--;
  
        // Update maximum string
        $res = maximumNum($curr_num, $res);
  
        $curr_num = "";
    }
  
    // To handle the case if there is only
    // 0 numeric value
    if (strlen($curr_num)== 0 && strlen($res)== 0)
        $res .= '0';
  
    // Return maximum string
    return maximumNum($curr_num, $res);
}
  
// Drivers program
  
    $str ="100klh564abc365bg";
    echo extractMaximum($str);
      
// this code is contributed by mits
?>


输出:

564

但是由于C和C++中的整数范围,在上述情况下,上述程序将无法正常工作。因此,要处理大数的情况,我们必须将每个数字值作为单独的字符串,然后找到最大值。

1) Start traversing the given string.
   Continue traversing if there are any 
   leading zeroes or any lowercase character.
  b) Form a string of integer values.
  c) Update the maximum string.
    i) If the maximum string and current 
       string are having equal lengths then 
       on the basis of the first unmatched 
       value return maximum string.
    ii) If both are having different lengths 
        then return the string with greater 
        length.

2) Return maximum string.

C++

// C++ program for above implementation
#include
using namespace std;
  
// Utility function to find maximum string
string maximumNum(string curr_num, string res)
{
    int len1 = curr_num.length();
    int len2 = res.length();
  
    // If both having equal lengths
    if (len1 == len2)
    {
        // Reach first unmatched character / value
        int i = 0;
        while (curr_num[i]== res[i])
            i++;
  
        // Return string with maximum value
        if (curr_num[i] < res[i])
            return res;
        else
            return curr_num;
    }
  
    // If different lengths
    // return string with maximum length
    return len1 < len2 ? res: curr_num;
}
  
// Function to extract the maximum value
string extractMaximum(string str)
{
    int n = str.length();
    string curr_num ="";
    string res;
  
    // Start traversing the string
    for (int i = 0; i='0' && str[i]<='9')
        {
            curr_num = curr_num + str[i];
            i++;
        }
  
        if (i == n)
            break;
  
        if (curr_num.size() > 0)
            i--;
  
        // Update maximum string
        res = maximumNum(curr_num, res);
  
        curr_num = "";
    }
  
    // To handle the case if there is only
    // 0 numeric value
    if (curr_num.size()== 0 && res.size()== 0)
        res = res + '0';
  
    // Return maximum string
    return maximumNum(curr_num, res);
}
  
// Drivers program
int main()
{
    string str ="100klh564abc365bg";
    cout << extractMaximum(str) << endl;
    return 0;
}

Java

// Java program for above implementation
  
class GFG 
{
    // Utility method to find maximum string
    static String maximumNum(String curr_num, String res)
    {
        int len1 = curr_num.length();
        int len2 = res.length();
       
        // If both having equal lengths
        if (len1 == len2)
        {
            // Reach first unmatched character / value
            int i = 0;
            while (curr_num.charAt(i) == res.charAt(i))
                i++;
       
            // Return string with maximum value
            if (curr_num.charAt(i) < res.charAt(i))
                return res;
            else
                return curr_num;
        }
       
        // If different lengths
        // return string with maximum length
        return len1 < len2 ? res: curr_num;
    }
       
    // Method to extract the maximum value
    static String extractMaximum(String str)
    {
        int n = str.length();
        String curr_num ="";
        String res="";
       
        // Start traversing the string
        for (int i = 0; i 0)
                i--;
       
            // Update maximum string
            res = maximumNum(curr_num, res);
       
            curr_num = "";
        }
       
        // To handle the case if there is only
        // 0 numeric value
        if (curr_num.length() == 0 && res.length() == 0)
            res = res + '0';
       
        // Return maximum string
        return maximumNum(curr_num, res);
    }
      
    // Driver method
    public static void main(String[] args) 
    {
        String str = "100klh564abc365bg";
        System.out.println(extractMaximum(str));
    }    
}

Python3

# Python3 program for above implementation
  
# Utility function to find maximum string
def maximumNum(curr_num, res):
  
    len1 = len(curr_num);
    len2 = len(res);
  
    # If both having equal lengths
    if (len1 == len2):
          
        # Reach first unmatched character / value
        i = 0;
        while (curr_num[i]== res[i]):
            i += 1;
  
        # Return string with maximum value
        if (curr_num[i] < res[i]):
            return res;
        else:
            return curr_num;
  
    # If different lengths
    # return string with maximum length
    return res if(len1 < len2) else curr_num;
  
# Function to extract the maximum value
def extractMaximum(str):
  
    n = len(str);
    curr_num = "";
    res = "";
  
    # Start traversing the string
    for i in range(n):
          
        # Ignore leading zeroes
        while (i < n and str[i]=='0'):
            i += 1;
  
        # Store numeric value into a string
        while (i < n and str[i] >= '0' and 
                         str[i] <= '9'):
            curr_num += str[i];
            i += 1;
  
        if (i == n):
            break;
  
        if (len(curr_num) > 0):
            i -= 1;
  
        # Update maximum string
        res = maximumNum(curr_num, res);
  
        curr_num = "";
  
    # To handle the case if there is only
    # 0 numeric value
    if (len(curr_num) == 0 and len(res) == 0):
        res += '0';
  
    # Return maximum string
    return maximumNum(curr_num, res);
  
# Driver Code
str ="100klh564abc365bg";
print(extractMaximum(str));
      
# This code is contributed by mits

C#

// C# program for above implementation 
using System;
class GFG 
{ 
    // Utility method to find maximum string 
    static String maximumNum(string curr_num, string res) 
    { 
        int len1 = curr_num.Length; 
        int len2 = res.Length; 
      
        // If both having equal lengths 
        if (len1 == len2) 
        { 
            // Reach first unmatched character / value 
            int i = 0; 
            while (curr_num[i] == res[i]) 
                i++; 
      
            // Return string with maximum value 
            if (curr_num[i] < res[i]) 
                return res; 
            else
                return curr_num; 
        } 
      
        // If different lengths 
        // return string with maximum length 
        return len1 < len2 ? res: curr_num; 
    } 
      
    // Method to extract the maximum value 
    static string extractMaximum(string str) 
    { 
        int n = str.Length; 
        string curr_num =""; 
        string res=""; 
      
        // Start traversing the string 
        for (int i = 0; i 0) 
                i--; 
      
            // Update maximum string 
            res = maximumNum(curr_num, res); 
      
            curr_num = ""; 
        } 
      
        // To handle the case if there is only 
        // 0 numeric value 
        if (curr_num.Length == 0 && res.Length == 0) 
            res = res + '0'; 
      
        // Return maximum string 
        return maximumNum(curr_num, res); 
    } 
      
    // Driver method 
    public static void Main() 
    { 
        string str = "100klh564abc365bg"; 
        Console.WriteLine(extractMaximum(str)); 
    }     
} 
// This code is contributed by mits

的PHP

='0' && $str[$i]<='9')
        {
            $curr_num .= $str[$i];
            $i++;
        }
  
        if ($i == $n)
            break;
  
        if (strlen($curr_num) > 0)
            $i--;
  
        // Update maximum string
        $res = maximumNum($curr_num, $res);
  
        $curr_num = "";
    }
  
    // To handle the case if there is only
    // 0 numeric value
    if (strlen($curr_num)== 0 && strlen($res)== 0)
        $res .= '0';
  
    // Return maximum string
    return maximumNum($curr_num, $res);
}
  
// Drivers program
  
    $str ="100klh564abc365bg";
    echo extractMaximum($str);
      
// this code is contributed by mits
?>

输出:

564