请一个在字符串中找出连续最长的数字串,并把这个串的长度返回;如果存在长度相同的连续数字串,返回最后一个连续数字串;
注意:数字串只需要是数字组成的就可以,并不要求顺序,比如数字串“1234”的长度就小于数字串“1359055”,如果没有数字,则返回空字符串(“”)而不是NULL!
样例输入
abcd12345ed125ss123058789
abcd12345ss54761
样例输出
输出123058789,函数返回值9
输出54761,函数返回值5
函数原型:
unsignedint Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串;
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;
返回值:
连续最长的数字串的长度
C++形式:
#include <stdlib.h>
#include "oj.h"
#include <cstring>
/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
*/
unsigned int Continumax(char** pOutputstr, char* intputstr)
{
char *pTemp =new char[10];
memset(pTemp,'\0',10);
char temp[10] = {""};
strncpy_s(pTemp,10, temp,10);
if (NULL == intputstr)
{
*pOutputstr = pTemp;
return 0;
}
size_t strLen = strlen(intputstr);
char* pMaxNum = new char[strLen+1];
size_t numLen = 0,maxNumLen = 0;
size_t startNum=0;
for(size_t i=0;i<strLen;++i)
{
if (intputstr[i]>='0' && intputstr[i]<='9')
{
startNum = i;
numLen = 0;
while(i<strLen && intputstr[i]>='0' && intputstr[i]<='9')
{
++numLen;
++i;
}
if (numLen!=0 && maxNumLen<=numLen)
{
memset(pMaxNum,'\0',strLen+1);
strncpy_s(pMaxNum,strLen+1, intputstr+startNum,numLen);
maxNumLen = numLen;
}
}
}
if (maxNumLen == 0)
{
delete [] pMaxNum;
pMaxNum = NULL;
*pOutputstr = pTemp;
}
else
{
*pOutputstr = pMaxNum;
}
return (unsigned int)maxNumLen;
}
javascript形式:
用isNaN()方法数值型字符和非数值型字符
isNaN("3") //false
isNaN("b") //true
采用副本的方式:(从联动下拉框项目获得启发)
tempStartIndex,tempLen 作为暂时最佳的结果,循环结束后则自然成为最终结果。startIndex, len作为循环变量
在遍历过程中,根据字符类型和字符的位置(字符的位置指的是,是否是字符串的开头第一个位置,是否是字符串的结尾一个位置,前后字符是否是同一种类型),将当前字符分为以下情形:
- 数字字符子串的开头(数字字符而且是是字符串的开头第一个位置,或者前一个字符是字母)
- 数字字符子串的结尾(当前字符是字母且前一个字符是数字,或者当前字符是数字而且是字符串的结尾)
- 字母字符子串的中间和结尾部分
function findMaxSerialNumberStr(str){
var tempStartIndex,tempLen=0, startIndex, len=0, char;
for(var i=0;i<str.length;i++){
char= isNaN(str.charAt(i));
if(char&&len==0){
//如果是字母类型,并且当前字母是字母字符子串的中间和结尾部分
continue;
}else if(char&&len!=0){
//如果是字母类型,并且当前字母是字母字符子串的开头
if(len>=tempLen){
tempLen=len; tempStartIndex=startIndex;
}
len =0; startIndex=undefined;
}else if(!char){
//如果是数字类型,并且当前数字是数字字符子串的开头
if(len==0){
startIndex =i;
}
//只要是数字类型,都要len加一
len++;
//如果是数字类型,并且当前数字是数字字符子串的结尾
if(i==str.length-1&&len>=tempLen){
tempLen=len; tempStartIndex=startIndex;
}
}
}
var maxStr = str.substr(tempStartIndex,tempLen);
console.log(maxStr+","+tempLen);
}
以上代码的if else嵌套还是有点多,将提供更加简洁的if else判断方式。
java形式:
Character.isDigit()方法判断一个字符是否是数字。
maxStartIndex和maxLen作为当前遍历过程中的最优结果。tempStartIndex和tempLen作为临时变量。
在遍历过程中,根据字符类型和字符的位置(字符的位置指的是,是否是字符串的开头第一个位置,是否是字符串的结尾一个位置,前后字符是否是同一种类型),将当前的数字字符分为以下情形:
- 当前数字字符是数字子串的开头
- 当前数字字符是数字子串的中间和结尾
- 当前数字字符是数字子串的结尾
public static int findLongestNumStr(String str){
int maxStartIndex = 0, maxLen = 0;
int tempStartIndex = 0, tempMaxLen = 0;
for(int i = 0;i < str.length();i++){
//如果当前字符是数字类型
if (Character.isDigit(str.charAt(i))){
//如果当前数字字符是数字子串的开头
if(i ==0 ||!Character.isDigit(str.charAt(i-1))){
tempStartIndex = i;
tempMaxLen = 0;
tempMaxLen ++;
//如果当前数字字符是数字子串的中间和结尾
}else if(i!=0 && Character.isDigit(str.charAt(i-1))){
tempMaxLen ++;
}
//如果当前数字字符是数字子串的结尾
if(i == str.length() -1 || !Character.isDigit(str.charAt(i+1))){
if(tempMaxLen >= maxLen){
maxStartIndex = tempStartIndex;
maxLen = tempMaxLen;
}
}
}
}
System.out.println(str.substring(maxStartIndex,maxStartIndex+maxLen)+","+maxLen);
return maxLen;
}
牛客网:在字符串中找出连续最长的数字串
题目描述:
输入一个字符串,输出字符串中最长的数字字符串和它的长度。如果有相同长度的串,则要一块儿输出,但是长度还是一串的长度
javascript形式:
function findMaxSerialNumberStr(str){
var tempStartIndex,tempLen=0, startIndex, len=0, char,sameMaxLenIndex=[];
for(var i=0;i<str.length;i++){
char= isNaN(str.charAt(i));
if(char&&len==0){ //如果在非数值区域
continue;
}else if(char&&len!=0){//如果刚进入非数值区域
compareLen();
len =0; startIndex=undefined;
}else if(!char){ //如果在数值区域
if(len==0){ //如果是刚进入数值区域
startIndex =i;
}
len++;
if(i==str.length-1){ //如果在该数值区域中已经读到字符串末尾
compareLen();
}
}
}
var tempStr="";
for(var i=0;i<sameMaxLenIndex.length;i++){
tempStr+=str.substr(sameMaxLenIndex[i],tempLen);
}
console.log(tempStr+","+tempLen);
function compareLen(){
if(len>=tempLen){
if(len>tempLen){
sameMaxLenIndex=[startIndex];
}else{
sameMaxLenIndex.push(startIndex);
}
tempLen=len; tempStartIndex=startIndex;
}
}
}
while(str = readline()){ //测试
findMaxSerialNumberStr(str);
}
java形式:
import java.util.*;
public class Main{
public static int findLongestNumStr(String str){
int maxStartIndex = 0, maxLen = 0;
int tempStartIndex = 0, tempMaxLen = 0;
List<Integer> list = new ArrayList<>();
for(int i = 0;i < str.length();i++){
if (Character.isDigit(str.charAt(i))){
if(i ==0 ||!Character.isDigit(str.charAt(i-1))){
tempStartIndex = i;
tempMaxLen = 0;
tempMaxLen ++;
}else if(i!=0 && Character.isDigit(str.charAt(i-1))){
tempMaxLen ++;
}
if(i == str.length() -1 || !Character.isDigit(str.charAt(i+1))){
if(tempMaxLen > maxLen){
maxStartIndex = tempStartIndex;
maxLen = tempMaxLen;
list.clear();
}else if(tempMaxLen == maxLen){
list.add(tempStartIndex);
}
}
}
}
String result = str.substring(maxStartIndex,maxStartIndex+maxLen);
if (list.size() > 0){
for (int i = 0; i < list.size(); i++) {
result += str.substring(list.get(i),list.get(i)+maxLen);
}
}
System.out.println(result+","+maxLen);
return maxLen;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
String str=scan.nextLine();
findLongestNumStr(str);
}
}
}