-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path266.cpp
More file actions
66 lines (53 loc) · 1.7 KB
/
Copy path266.cpp
File metadata and controls
66 lines (53 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Given a string, determine if a permutation of the string could form a palindrome.
For example,
"code" -> False, "aab" -> True, "carerac" -> True.
Hint:
Consider the palindromes of odd vs even length. What difference do you notice?
Count the frequency of each character.
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times? */
class Solution {
public:
bool canPermutePalindrome(string s) {
unordered_map<char, int> m;
int cnt = 0;
for (auto a : s) ++m[a];
for (auto it = m.begin(); it != m.end(); ++it) {
if (it->second % 2) ++cnt;
}
// 偶数长度字符串,每个字符都是偶数
// 奇数长度字符串, 只有一个
return cnt == 0 || (s.size() % 2 == 1 && cnt == 1);
}
};
class Solution {
public:
bool canPermutePalindrome(string s) {
unordered_set<char> t;
for (auto a : s) {
if (t.find(a) == t.end()) t.insert(a);
else t.erase(a);
}
return t.empty() || t.size() == 1;
}
};
class Solution {
public:
int missingNumber(vector<int>& nums) {
for(int i = 0; i < nums.size(); i++){
if(nums[i] == nums.size() || nums[i] == i){
continue;
}else{
if(nums[i] != i){
int tmp = nums[nums[i]];
nums[nums[i]] = nums[i];
nums[i] = tmp;
}
}
}
for(int i = 0; i < nums.size(); i++){
if(nums[i] == nums.size())
return i;
}
return nums.size();
}
};