/**
* @author xienl
* @description 最大值
* @date 2022/7/22
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.maxValue("2473884163", 4));
}
public int maxValue (String s, int k) {
// write code here
int m = s.length();
if (m == k){
return Integer.parseInt(s);
}
int left = 0;
int res = 0;
while (left < m - k){
res = Math.max(res, getInt(s, left, k + left));
left++;
}
return res;
}
private int getInt(String s, int start, int end){
return Integer.parseInt(s.substring(start, end));
}
}
牛客网:NC174 最大值
最新推荐文章于 2025-11-30 20:35:27 发布
该博客介绍了一个Java方法,用于在给定长度为k的子串中找到字符串中的最大整数。通过遍历字符串并比较子串的最大值,实现了这一功能。适用于字符串处理和算法理解。

378

被折叠的 条评论
为什么被折叠?



