欢迎提问,一起解决
- 题目:求一段字符串内最长的非空子字符串的长度
- 解答方案
- 提升思考 怎么将字符 一起输出
- 优化思考
题目:求一段字符串内最长的非空子字符串的长度
示例:
输入:s = "book"
输出:2
解释:子字符串 "oo" 长度为 2 ,只包含字符 'o' 。
输入:s = "example"
输出:1
解释:子字符串无重复。
解答方案
public class One {
public static int subset(String s) {
int longs= 1, temp = 1;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) == s.charAt(i - 1)) {
++temp;
} else {
temp = 1;
}
//比较换值
if(longs
提升思考 怎么将字符 一起输出
public class One {
//注意这时返回类型应改为String型
public static String subset(String s) {
int longs= 1, temp = 1;
char longsChar = 0,tempChar=0;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) == s.charAt(i - 1)) {
++temp;
tempChar=s.charAt(i-1);
} else {
temp = 1;
}
//比较换值
if(longs
优化思考
longs= Math.max(longs, temp );
相当于
if(longs