2 回答
TA贡献1853条经验 获得超6个赞
连续的意思是上一个字符和下一个字符的ascii值相差1,并且连续出现4次和以上。这个貌似c语言作业...
java代码:
String str = "12345678abcABCDefghijk9874321YXWV321";
char f=str.charAt(0);
int count = 0;
for(int i=1 ; i < str.length(); ++i) {
char c = str.charAt(i);
if(c-f == 1 || f-c==1) {
++count;
}else {
if(count >= 3) {
System.out.println( str.substring( i-count-1 ,i) ) ;
}
count = 0;
}
f=c;
}
TA贡献1801条经验 获得超16个赞
function trans (str) {
let before = ''
let len = 0
let order = null
let matched = []
for (let i = 0, length = str.length; i < length; i++) {
let cur = str[i]
if (len === 0) {
before = cur
len = 1
order = null
continue
}
let diff = cur.charCodeAt(0) - before.charCodeAt(0)
if (Math.abs(diff) === 1) {
order = order || diff
if (order === diff) {
len += 1
before = cur
continue
}
}
if (len >= 4) {
matched.push(str.slice(i - len, i))
}
before = cur
len = 1
order = null
}
if (len >= 4) {
matched.push(str.slice(str.length - len))
}
return {
count: matched.length,
matched
}
}
trans("12345678abcABCDefghijk9874321YXWV321").count
添加回答
举报