最长连续递增子串

#include 
#include 

std::string findLongestIncreasingSubstr(const std::string& str) {
    std::string longestSubstr = "";
    std::string currentSubstr = "";

    for (size_t i = 0; i < str.length(); i++) {
        // 如果当前字符是数字,并且与前一个字符递增,则将其添加到当前子串中
        if (i > 0 && isdigit(str[i]) && str[i] - str[i - 1] == 1) {
            if (currentSubstr.empty()) {
                currentSubstr += str[i - 1];
            }
            
            currentSubstr += str[i];
        } else {
            // 否则,比较当前子串与最长子串的长度,并更新最长子串
            if (currentSubstr.length() > longestSubstr.length()) {
                longestSubstr = currentSubstr;
            }
            // 重置当前子串
            currentSubstr = "";
        }
    }

    // 比较最后一个子串与最长子串的长度,并更新最长子串
    if (currentSubstr.length() > longestSubstr.length()) {
        longestSubstr = currentSubstr;
    }

    return longestSubstr;
}

int main() {
    std::string str = "abcd123456";
    std::string longestSubstr = findLongestIncreasingSubstr(str);
    std::cout << "Longest increasing substring: " << longestSubstr << std::endl;

    return 0;
}

你可能感兴趣的:(LeetCode,c++,算法,开发语言)