java最大连续子串_字符串中找出连续最长的数字串

0

//弄成字符串组,字符串长度对比

import java.util.Scanner;

public class Main{

public static String characterToSpace(String string){

char[] arrays = string.toCharArray();

for(int i = 0;i < arrays.length;i++){

if(!Character.isDigit(arrays[i]))

arrays[i] = ' ';

}

String result = "";

for(Character cc : arrays){

result += cc.toString();

}

return result;

}

public static String longestDigitString(String string){

String strings[] = characterToSpace(string).split("\\s+");

int maxLength = 0;

String result = "";

for(String ss:strings){

if(ss.length() > maxLength){

maxLength = ss.length();

result = ss;

}

}

return result;

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

while(in.hasNext()){

String string = in.nextLine();

System.out.println(longestDigitString(string));

}

}

}

发表于 2017-09-21 16:27:21

回复(0)

更多回答

36

/*算法思想:用max表示经过的数字长度最大值,count表示数字计数器,当为字母时重置为0

*end表示数字尾部,每次满足数字时,对max进行判断,当max小于于count时,更新max和end

*/

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while(scanner.hasNext()){

String str = scanner.nextLine();

int max = 0,count=0,end=0;

for(int i=0;i

if(str.charAt(i)>='0' && str.charAt(i)<='9'){

count++;

if(max

max= count;

end = i;

}

}else{

count = 0;

}

}

System.out.println(str.substring(end-max+1,end+1));

}

}

}

编辑于 2017-03-15 20:29:12

回复(14)

26

"""

看你们写的好累啊

python这种题目四行解决问题

"""

import re

input_val = raw_input()

split_char = re.split(r'\D*', input_val)

print max(split_char, key=len)

编辑于 2017-04-01 08:22:28

回复(13)

7

虽然简单到令人发指 但是很经典的算法 #include

#include

#include

#include

using namespace std;

int main(){

string ans = "", str;

cin>>str;

int i = 0, max_len = 0, strlen = str.length();

while(i < strlen){

if(str[i] < '0' || str[i] > '9') i++;

else{

int len = 0;

string tmp = "";

while(i < strlen && str[i] <= '9' && str[i] >= '0'){

tmp = tmp + str[i];

i++; len++;

}

if(len > max_len) {max_len = len; ans = tmp;}//memcpy(&ans, &tmp, sizeof(tmp)); }

}

}

cout<

return 0;

}

/*

abcd12345ed125ss123456789

*/

编辑于 2018-09-17 22:10:18

回复(1)

8

华为的笔试考的这道题:

python解法如下: a = input()

maxLen, curLen, maxStr, curStr = 0, 0, "", ""

for i, v in enumerate(a):

if v.isnumeric():

curLen += 1

curStr += v

if curLen >= maxLen:

maxLen = curLen

maxStr = curStr

else:

curLen = 0

curStr = ""

print(maxStr)

编辑于 2017-09-07 13:59:50

回复(2)

7

#include

#include

int main(){

int i,k,key,sum,max,len[300];

char a[300];

scanf("%s",a);

for(int j=0;j<300;j++){//初始化长度数组

len[j]=0;

}

for(i=0;i

k=i;

sum=0;

while(a[i]>='0' && a[i]<='9'){

i++;

sum++;

}

len[k]=sum;//将每个位置出现的数组串长度,保存在len[]中,没出现的为 0

}

max=0;

for(i=0;i

if(max

max = len[i]; //保存最长的数字串长度

key = i;//记录最长数字串, 初始地址

}

}

for(i=0;i

printf("%c",a[key++]);

}

printf("\0");

printf("\n");

return 0;

}

发表于 2017-02-24 22:33:07

回复(0)

4

#include

#include

usingnamespacestd;

int main()

{

// insert code here...

string str;

cin>>str;

string res;

int length=0;

int pos=0;

for(int i=0,j=0;j

{

if(str[j]'9')

{

++j;

i=j;

}

else

{

++j;

if((j-i)>length)

{

pos=i;

length=j-i;

}

}

}

res=str.substr(pos,length);

cout<

}

思路:双指针,遇到数字的时候,更新pos起始位置和子串的长度

发表于 2018-07-12 01:37:24

回复(2)

4

$str=trim(fgets(STDIN));

$len=strlen($str);

preg_match_all("/[0-9]+/",$str,$num);

$max=$num[0][0];

for($i=0;$i

{

if($num[0][$i]>$max) $max=$num[0][$i];

}

echo $max;

发表于 2017-09-16 16:34:39

回复(0)

6

//C++也可以很简洁

#include

#include

using namespace std;

int main()

{

string str,res,cur;

cin>>str;

for(int i=0;i<=str.length();i++){

if(str[i]>='0' && str[i]<='9')cur+=str[i];

else{

if(res.length()

cur="";

}

}

cout<

return 0;

}

编辑于 2018-07-20 23:58:57

回复(9)

3

Python n = input().strip()

ans,temp = [],[]

for i in n:

if 48 <= ord(i) <= 57:

temp.append(i)

else:

ans.append(temp)

temp = []

ans.append(temp)

ans = sorted(list(filter(None,ans)),key = len)

print(''.join(ans[-1]))

发表于 2018-07-08 22:32:24

回复(0)

3

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

String[] arr = s.split("[\\D]+");

String result="";

int max = 0;

for(int i=0;i

if(arr[i].length()>max){

max=arr[i].length();

result = arr[i];

}

}

System.out.println(result);

}

}

发表于 2017-09-08 15:28:44

回复(3)

5

编辑于 2017-02-17 23:54:35

回复(6)

2

let line = readline(); let arr = line.charAt(0);

let max = "";

for(let j = 1;j

if(arr.charCodeAt(arr.length-1)+1==line.charCodeAt(j)){

arr = arr+line.charAt(j);

}else{

arr = line.charAt(j);

}

if(max.length<=arr.length){

max = arr;

}

}

console.log(max);

编辑于 2018-10-09 17:29:16

回复(0)

1

public static void main(String[] args) {

ArrayList strlsit = new ArrayList<>();

String stri ="abcd12345ed125ss123456789";

StringBuffer str = new StringBuffer();

int size=0,max=0;

for(int i=0;i

if(stri.charAt(i)>='0'&&stri.charAt(i)<='9') {

str.append(stri.charAt(i));

size++;

}else {

str = new StringBuffer();

size=0;

}

}

max=size>max?size:max;

strlsit.add(str);

for(StringBuffer s:strlsit) {

if(s.length()==max) {

System.out.println(s);

}

}

发表于 2020-07-28 20:31:26

回复(0)

1

import java.util.Scanner;

public class Main {

//获取最长的连续数字串

public static String maxDigitalStr(String str){

char[] arrayChar = str.toCharArray();//将str转换成字符数组

int maxDigiStrLen = 0;//最长连续数字串的长度

String maxDigiStr = "";//最长连续数字串

int tempStrLen = 0;//临时数字串长度

StringBuffer tempStr = new StringBuffer();//临时数字串

for(int i = 0; i 

//过滤掉非数字的字符

if(arrayChar[i] >= '0' && arrayChar[i] <= '9'){

tempStr.append(arrayChar[i]);

tempStrLen++;

if(tempStrLen > maxDigiStrLen){

maxDigiStrLen = tempStrLen;//更新 最长连续数字串长度

maxDigiStr = tempStr.toString();//更新 最长连续数字串

}

}else{

tempStrLen = 0;//临时数字串长度 置0

tempStr.setLength(0);//临时数字串 清空

}

}

return maxDigiStr;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String tempStr = sc.next();

System.out.println(maxDigitalStr(tempStr));

}

}

编辑于 2020-07-22 22:55:48

回复(0)

1

直接新建一个一维数组,存放升序的累计个数,然后找最大,并往前输出最大长度的子字符串即为所求

while True:

try:

s=input()

res=[1 for i in range(len(s))]

for i in range(1,len(s)):

if ord(s[i])==ord(s[i-1])+1:

res[i]=res[i-1]+1

mmax=max(res)

ind=res.index(mmax)

print(s[ind+1-mmax:ind+1])

except:

break

发表于 2019-09-23 21:19:42

回复(0)

1

import re

input_val = input()

split_char = re.split(r'\D*', input_val)

lst = []

for i in split_char:

lst.append(len(i))

print(split_char[lst.index(max(lst))])

'''

max(lst)找出lst中最大的元素的值

lst.index(max(lst))找出“max(lst)找出lst中最大的元素的值”对应的索引

lst的大小和split_char是一致的,所以直接在split_char使用索引即可

'''

发表于 2019-08-04 13:52:28

回复(0)

1

#include

using namespace std;

int main()

{

string a;

cin>>a;

int j=0;

int count=0;

int max=0;

int maxi;

for(int i=0;a[i]!='\0';i++)

{

if((int)a[i]<58&&(int)a[i]>47) //判断是不是0-9的数字

{

count++;

if(count>max)

{

max=count; //记录最大长度

maxi=i; //记录最大长度的结尾坐标

}

}

else //不满足条件,重新计数

{

count=0;

}

}

for(int i=maxi-max+1;i<=maxi;i++)

{

cout<

}

}

发表于 2019-04-21 20:26:11

回复(0)

1

/*

使用两个字符串,字符串sb记录当前数字串及长度,strTemp记录下一个数字串及长度,如

果strTemp长度比sb长,则替换sb,即sb=strTemp,最后输出sb

*/

import java.util.Scanner;

public class Main{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

while(sc.hasNext()){

StringBuilder sb = new StringBuilder("");

StringBuilder strTemp = new StringBuilder();

char[] ch = sc.nextLine().toCharArray();

for(int i=1;i

if(ch[i]>='0' && ch[i]<='9'){

strTemp.append(ch[i]);

}

if(ch[i]'9' || i == ch.length-1){

if(strTemp.length()>sb.length()){

sb = strTemp;

}

strTemp = new StringBuilder("");

}

}

System.out.println(sb);

}

}

}

发表于 2018-10-09 11:16:50

回复(0)

1

importjava.util.ArrayList;importjava.util.Scanner;public classSolution30 { public staticString MaxSubArray(char[] chars, intn) { if(n == 0) { return"";}

ArrayList curSum = newArrayList<>();ArrayList maxSum = newArrayList<>();StringBuilder sb = newStringBuilder();for(inti = 0;i < n;i++) { if(Character.isDigit(chars[i])) {

curSum.add(chars[i]);} else{ if(curSum.size() > maxSum.size()) {

maxSum.clear();for(intj = 0;j < curSum.size();j++) {

maxSum.add(curSum.get(j));}

}

curSum.clear();}

} for(inti = 0;i < maxSum.size();i++) {

sb.append(maxSum.get(i));} returnsb.toString();} public static voidmain(String[] args) {

Scanner in = newScanner(System.in);String str = in.nextLine();char[] chars = str.toCharArray();char[] newChars = new char[str.length() + 1];for(inti = 0;i < chars.length;i++) {

newChars[i] = chars[i];} intn = newChars.length;newChars[n - 1] = '#';System.out.println(MaxSubArray(newChars,n));}

}

发表于 2018-09-03 15:00:31

回复(0)

1

具体思想就是遍历去找数字,这个时间是标准O(n)的,关键在i=j这句, 遍历过的不用再遍历一次,因为比之前短的就不可能成为答案。往末尾加“a“是懒得边界判断了,加“a”很方便。 import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) throws Exception{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String line = br.readLine().trim()+"a";

char []strs = line.toCharArray();

int tmpl = 0;

int tmpi = 0;

for(int i = 0; i< strs.length; i++){

if(Character.isDigit(strs[i])){

for(int j =i+1; j< strs.length; j++){

if (!Character.isDigit(strs[j])) {

if(j-i>tmpl){

tmpl = j-i;

tmpi = i;

}

i = j;

break;

}

}

}

}

System.out.println(line.substring(tmpi, tmpi+tmpl));

}

}

发表于 2018-09-01 15:35:58

回复(0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值