2042. 检查句子中的数字是否递增

2042. 检查句子中的数字是否递增

class Solution {
    public boolean areNumbersAscending(String s) {
        char[] ch = s.toCharArray();
        int pre = -1;
        for (int i = 0; i < ch.length; ) {
            // 不是数字 
            if (ch[i] < '0' || ch[i] > '9') {
                i++;
                continue;
            }
            // 取字符串中该数字的值
            int temp = 0;
            while (i < ch.length && ch[i] >= '0' && ch[i] <= '9') {
                temp *= 10;
                temp += ch[i] - '0';
                i++;
            }
            // 判断是否递增
            if (temp <= pre) return false;
            else pre = temp;
        }
        return true;
    }
}
posted @ 2023-01-03 17:17  Eiffelzero  阅读(17)  评论(0)    收藏  举报