面试题01.04-回文排列

面试题01.04-回文排列_第1张图片

class Solution {
public:
    bool canPermutePalindrome(string s) {
        vector<int>arr(128, 0);
        for (int i = 0; i < s.size(); i++) {
            int index = s[i] - 0;
            arr[index]++;
        }
        // 字符个数为奇数的个数
        int countOf1 = 0;
        for (int i = 0; i < 128; i++) {
            if(arr[i] == 0) {
                continue;
            }
            else if (arr[i] % 2 == 1 && countOf1 > 0){
                return false;
            }
            else if (arr[i] % 2 == 1 && countOf1 == 0) {
                countOf1++;
            }
        }
        return true;
    }
};

你可能感兴趣的:(leetcode)