235. 二叉搜索树的最近公共祖先
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p->val < root->val && q->val < root->val)
return lowestCommonAncestor(root->left,p,q);
if(p->val > root->val && q->val > root->val)
return lowestCommonAncestor(root->right,p,q);
return root;
}
};
237. 删除链表中的节点
class Solution {
public:
void deleteNode(ListNode* node) {
if(node == NULL || node->next == NULL)
return;
ListNode* temp = node->next;
node->val = temp->val;
node->next = temp->next;
delete temp;
}
};
242. 有效的字母异位词
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size() != t.size())
return false;
char sArr[26] = {0};
char tArr[26] = {0};
for(int i = 0; i < s.size(); ++i){
sArr[s[i]-'a']++;
tArr[t[i]-'a']++;
}
for(int i = 0; i < 26; ++i){
if(sArr[i]!=tArr[i])
return false;
}
return true;
}
};
257. 二叉树的所有路径
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(root==NULL)
return res;
if(root->left == NULL && root->right == NULL)
res.push_back(to_string(root->val));
vector<string> leftS = binaryTreePaths(root->left);
for(int i = 0; i < leftS.size(); ++i){
res.push_back(to_string(root->val)+"->"+leftS[i]);
}
vector<string> rightS = binaryTreePaths(root->right);
for(int i = 0; i < rightS.size(); ++i){
res.push_back(to_string(root->val)+"->"+rightS[i]);
}
return res;
}
};
258. 各位相加
class Solution {
public:
int addDigits(int num) {
return (num-1)%9+1;
}
};
263. 丑数
class Solution {
public:
bool isUgly(int num) {
if(num == 0)
return false;
while(num%2==0)
num /= 2;
while(num%3==0)
num /= 3;
while(num%5==0)
num /= 5;
if(num == 1)
return true;
else
return false;
}
};
268.缺失数字
class Solution {
public:
int missingNumber(vector<int>& nums) {
if(nums.size() == 1)
return 1 - nums[0];
sort(nums.begin(), nums.end());
if(nums[0]!=0)
return 0;
for(int i = 1; i < nums.size(); ++i){
if(nums[i]-nums[i-1] != 1)
return nums[i-1]+1;
}
return nums.size();
}
};
278.第一个错误的版本
class Solution {
public:
int firstBadVersion(int n) {
int low = 1;
int high = n;
while(low <= high){
int mid = low+(high-low)/2;
if(isBadVersion(mid)){
high = mid - 1;
}else{
low = mid + 1;
}
}
return low;
}
};
283. 移动零
class Solution {
public:
void moveZeroes(vector<int>& nums) {
if(nums.size() <= 1)
return;
int k = 0;
for(int i = 0; i < nums.size(); ++i){
if(nums[i]!=0){
if(k!=i){
swap(nums[k],nums[i]);
}
k++;
}
}
}
};
290. 单词规律
class Solution {
public:
bool wordPattern(string pattern, string str) {
map<char, string> mm;
istringstream in(str);
string word;
int i = 0;
while(in>>word){
if(mm.find(pattern[i])!=mm.end()){
if(mm[pattern[i]] != word)
return false;
}else{
for(auto &p : mm){
if(p.second == word)
return false;
}
mm[pattern[i]] = word;
}
++i;
}
if(i == pattern.size())
return true;
else
return false;
}
};
292. Nim 游戏
class Solution {
public:
bool canWinNim(int n) {
return n%4==0?false:true;
}
};
303. 区域和检索 - 数组不可变
class NumArray {
public:
NumArray(vector<int>& nums) {
arr = nums;
}
int sumRange(int i, int j) {
int sum = 0;
for(int k = i; k <= j; ++k){
sum += arr[k];
}
return sum;
}
private:
vector<int> arr;
};
326. 3的幂
class Solution {
public:
bool isPowerOfThree(int n) {
if(n<=0)
return false;
int k = log(0x7fffffff)/log(3);
int b = pow(3,k);
return b%n == 0;
}
};
342. 4的幂
class Solution {
public:
bool isPowerOfFour(int num) {
long n = (long)num;
return ((n&n-1) == 0) && (n > 0) && ((n&0x55555555) != 0);
}
};
344. 反转字符串
class Solution {
public:
void reverseString(vector<char>& s) {
if(s.empty())
return;
int left = 0, right = s.size()-1;
while(left < right){
swap(s[left++],s[right--]);
}
}
};
345. 反转字符串中的元音字母
class Solution {
public:
string reverseVowels(string s) {
if(s.empty())
return s;
string vowel = "aeiouAEIOU";
int left = 0, right = s.length()-1;
while(left < right){
if(vowel.find(s[left])== string::npos)
left++;
else if(vowel.find(s[right])== string::npos)
right--;
else {
swap(s[left++],s[right--]);
}
}
return s;
}
};
349. 两个数组的交集
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
set<int> set1(nums1.begin(),nums1.end());
set<int> res;
for(int i = 0; i < nums2.size(); ++i){
if(set1.find(nums2[i])!= set1.end())
res.insert(nums2[i]);
}
return vector<int>(res.begin(), res.end());
}
};
350. 两个数组的交集 II
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
map<int,int> map1;
vector<int> res;
for(int i = 0; i < nums1.size(); ++i){
map1[nums1[i]]++;
}
for(int i = 0; i < nums2.size(); ++i){
if(map1[nums2[i]] > 0){
res.push_back(nums2[i]);
map1[nums2[i]]--;
}
}
return res;
}
};
367. 有效的完全平方数
class Solution {
public:
bool isPerfectSquare(int num) {
long left = 1, right = (long)num;
while(left <= right){
long mid = left + (right-left)/2;
if(mid*mid == num)
return true;
else if(mid*mid > num)
right = mid -1;
else
left = mid + 1;
}
return false;
}
};
371. 两整数之和
class Solution {
public:
int getSum(int a, int b) {
while(b){
auto c = ((unsigned int)a&b) << 1;
a = a ^ b;
b = c;
}
return a;
}
};
374.猜数字大小
class Solution {
public:
int guessNumber(int n) {
int left = 1, right = n;
while(left < right){
int mid = left + (right-left)/2;
if(guess(mid) == 1)
left = mid + 1;
else if(guess(mid) == -1)
right = mid;
else
return mid;
}
return n;
}
};
383. 赎金信
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
map<char,int> m1;
for(int i = 0; i < magazine.length(); ++i){
m1[magazine[i]]++;
}
for(int i = 0; i < ransomNote.length(); ++i){
m1[ransomNote[i]]--;
if(m1[ransomNote[i]] < 0)
return false;
}
return true;
}
};