0
点赞
收藏
分享

微信扫一扫

NC174 最大值

NC174 最大值_方法名

NC174 最大值_子串_02
示例1

输入:
"321",2
返回值:
32
说明:
所有长度为 22 的子串为:"32""21",显然3232是最大的。

示例2

输入:
"1234",4
返回值:
1234
说明:
所有长度为 44 的子串只有它自己本身,因此答案为 12341234

备注:
NC174 最大值_c++_03
Code:
枚举法,遍历全部子串

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param k int整型
* @return int整型
*/
int maxValue(string s, int k) {
// write code here
int res=0;
for(int i=0;i<s.length();i++)
{
if((i+k)>s.length())
break;
string temp=s.substr(i,k);
cout<<temp<<endl;
res=max(atoi(temp.c_str()),res);
}
return res;
}
};


举报

相关推荐

0 条评论