2020-06-30 刷题2(数组)

2020-07-03  本文已影响0人  nowherespyfly

1. 判断字符是否唯一(面试题01.01)

用数组


2. 判定是否为字符串重排

还是用数组。

class Solution {
public:
    bool CheckPermutation(string s1, string s2) {
        int cnt_map[256] = {0};
        for(int i = 0; i < s1.size(); i++)
            cnt_map[s1[i]]++;
        bool flag = true;
        for(int i = 0; i < s2.size(); i++){
            cnt_map[s2[i]]--;
            if(cnt_map[s2[i]] < 0){
                return false;
            }
        }
        for(int i = 0; i < 256; i++){
            if(cnt_map[i])
                return false;
        }
        return true;
    }
};

上一篇 下一篇

猜你喜欢

热点阅读