// @lc code=start // Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 intguess(int num);
classSolution { public:
//二分查找 intguessNumber(int n) { long left = 1; long right = n; while (left <= right) { long mid = (left + right) / 2; if (guess(mid) == 1) left = mid + 1; elseif (guess(mid) == -1) right = mid - 1; else return mid; } return left; }
boolcanConstruct(string ransomNote, string magazine) { if (ransomNote.length() > magazine.size()) returnfalse; int maga[128] = {0}; for (int i = 0; i < magazine.length(); i++) { maga[magazine[i]]++; } for (int i = 0; i < ransomNote.length(); i++) { if (!maga[ransomNote[i]]--) returnfalse; } returntrue; }
intfirstUniqChar(string s) { int table[128] = {0}; for (int i = 0; i < s.length(); i++) { table[s[i]]++; } for (int i = 0; i < s.length(); i++) { if (table[s[i]] == 1) return i; } return-1; }