题解 | #最长不含重复字符的子字符串#

最长不含重复字符的子字符串

https://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7

#include <map>
#include <utility>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return int整型
     */
    int lengthOfLongestSubstring(string s) {
        // write code here
        map<char, bool> count;
        int len;
        int maxlen = 0;
        for (int i = 0; i < s.size(); ++i) {
            count.clear();
            len = 0;
            for (int j = i; j < s.size(); ++j) {
                char c = s[j];
                if (count[c] != true) {  // 检查是否已经存在
                    count[c] = true;  // 直接赋值为 true
                    ++len;
                    if (len > maxlen) {
                        maxlen = len;
                    }
                } else {
                    break;
                }
            }
        }
        return maxlen;
    }
};

全部评论

相关推荐

焦虑中,不知道怎么办了。。。
西北上单:应该放俩项目合理一些 我是一个业务开发项目 一个AI项目和你这个写的亮点差不多
你的简历改到第几版了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
03-09 19:13
求你们别卷了的大学生...:你不骂他,我就要骂你了
今天你投了哪些公司?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务