给定字符串数字和字符。编写程序以查找字符串具有最大位数的数字。
注意:该数字可能不是字符串的最大数字。例如,如果字符串是“ a123bc321”,则答案可能是123或321,因为问题是要找到长度最长而不是最大值的数字。
例子:
Input: geeks100for1234geeks
Output: 1234
Input: abx12321bst1234yz
Output: 12321
方法:想法是遍历字符串,如果遇到数字,请存储其位置,并从该位置进一步遍历直到出现字符。每次遇到连续的数字序列时,请存储其长度,并将其长度与先前查找到的数字序列的长度相匹配,以找出所有连续的数字序列中的最大值。
下面是上述方法的实现。
C++
// C++ code for finding the longest
// length integer
#include
using namespace std;
string longestInteger(string str, int l)
{
int count = 0, max = 0, pos = -1, pre_pos, pre_len, len = 0;
// Traverse the string
for (int i = 0; i < l; i++) {
// Store the previous position and previous length
// of the digits encountered.
pre_pos = pos;
pre_len = len;
count = 0;
len = 0;
// If first digit occurs, store its position in pos
if (isdigit(str[i]))
pos = i;
// Traverse the string till a character occurs.
while (isdigit(str[i])) {
count++;
i++;
len++;
}
// Check if the length of the string is
// greater than the previous ones or not.
if (count > max) {
max = count;
}
else {
pos = pre_pos;
len = pre_len;
}
}
return (str.substr(pos, len));
}
// Driver code
int main()
{
string str = "geeks100for1234geeks";
int l = str.length();
cout << longestInteger(str, l);
return 0;
}
Java
// Java code for finding the
// longest length integer
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
static String longestInteger(String str, int l)
{
int count = 0, max = 0,
pos = -1, pre_pos,
pre_len, len = 0;
// Traverse the string
for (int i = 0; i < l; i++)
{
// Store the previous position
// and previous length of the
// digits encountered.
pre_pos = pos;
pre_len = len;
count = 0;
len = 0;
// If first digit occurs,
// store its position in pos
if (Character.isDigit(str.charAt(i)))
pos = i;
// Traverse the string
// till a character occurs.
while (Character.isDigit(str.charAt(i)))
{
count++;
i++;
len++;
}
// Check if the length of
// the string is greater
// than the previous ones
// or not.
if (count > max)
{
max = count;
}
else
{
pos = pre_pos;
len = pre_len;
}
}
return (str.substring(pos, pos + len));
}
// Driver code
public static void main(String[] args)
{
String str = "geeks100for1234geeks";
int l = str.length();
System.out.print(longestInteger(str, l));
}
}
C#
// C# code for finding the
// longest length integer
using System;
class GFG
{
static string longestInteger(string str,
int l)
{
int count = 0, max = 0,
pos = -1, pre_pos,
pre_len, len = 0;
// Traverse the string
for (int i = 0; i < l; i++)
{
// Store the previous position
// and previous length of the
// digits encountered.
pre_pos = pos;
pre_len = len;
count = 0;
len = 0;
// If first digit occurs,
// store its position in pos
if (Char.IsDigit(str[i]))
pos = i;
// Traverse the string
// till a character occurs.
while (Char.IsDigit(str[i]))
{
count++;
i++;
len++;
}
// Check if the length of
// the string is greater
// than the previous ones
// or not.
if (count > max)
{
max = count;
}
else
{
pos = pre_pos;
len = pre_len;
}
}
return (str.Substring(pos, len));
}
// Driver code
public static void Main()
{
string str = "geeks100for1234geeks";
int l = str.Length;
Console.Write(longestInteger(str, l));
}
}
// This code is contributed
// by ChitraNayal
Python 3
# Python 3 code for finding the
# longest length integer
def longestInteger(s, length):
count = 0
maximum = 0
pos = -1
l = 0
# Traverse the string
for i in range(length):
# Store the previous position
# and previous length of
# the digits encountered.
pre_pos = pos
pre_len = l
count = 0
l = 0
# If first digit occurs,
# store its position in pos
if (s[i].isdecimal()):
pos = i
# Traverse the string
# till a character occurs.
while (s[i].isdecimal()):
count += 1
i += 1
l += 1
# Check if the length of
# the string is greater
# than the previous ones
# or not.
if (count > maximum):
maximum = count
else:
pos = pre_pos
l = pre_len
return (s[pos: pos + l])
# Driver code
s = "geeks100for1234geeks"
length = len(s)
print(longestInteger(s, length))
# This code is contributed
# by ChitraNayal
PHP
$max)
{
$max = $count;
}
else
{
$pos = $pre_pos;
$len = $pre_len;
}
}
return (substr($str, $pos, $len));
}
// Driver code
$str = "geeks100for1234geeks";
$l = strlen($str);
echo longestInteger($str, $l);
// This code is contributed
// by ChitraNayal
?>
输出:
1234
时间复杂度: O(n)
辅助空间复杂度: O(1)
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。