LeetCode刷算法题-简单难度(二)题号100-169

100.相同的树
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p == NULL && q == NULL)
            return true;
        if(p == NULL || q == NULL)
            return false;
        if(p->val != q->val)
            return false;
        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
};
101.对称二叉树
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool rec(TreeNode *left, TreeNode *right){
        if(left == NULL && right == NULL)
            return true;
        if(left == NULL || right == NULL)
            return false;
        if(left->val != right->val)
            return false;
        return rec(left->left, right->right) && rec(left->right, right->left);       
    }
    bool isSymmetric(TreeNode* root) {
        if(root == NULL)
            return true;

        return rec(root->left, root->right);
    }
};
104.二叉树最大深度
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == NULL)
            return 0;
        int leftDepth = maxDepth(root->left);
        int rightDepth = maxDepth(root->right);
        int treeDepth = leftDepth > rightDepth ? leftDepth : rightDepth;
        return treeDepth+1;
    }
};
105.二叉树的层次遍历 使用队列和栈辅助
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int> > v;
        stack<vector<int> > s;
        queue<TreeNode *> q;
        q.push(root);
        if(root == NULL)
            return v;
        while(!q.empty()){
            queue<TreeNode*> next;
            vector<int> vv;
            while(!q.empty()){
                TreeNode *tree = q.front();
                q.pop();
                vv.push_back(tree->val);
                if(tree->left)
                    next.push(tree->left);
                if(tree->right)
                    next.push(tree->right);
            }
            s.push(vv);
            q = next;
        }
        while(!s.empty()){
            v.push_back(s.top());
            s.pop();
        }
        return v;
    }
};
108.将有序数组转换为二叉搜索树
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        if(nums.empty())
            return NULL;
        int mid = nums.size() >> 1;
        TreeNode* root = new TreeNode(nums[mid]);
        vector<int> nums_l(nums.begin(), nums.begin() + mid);
        vector<int> nums_r(nums.begin() + mid + 1, nums.end());
        root->left = sortedArrayToBST(nums_l);
        root->right = sortedArrayToBST(nums_r);
        return root;
    }
};
110.平衡二叉树 使用二层递归 height和isBalanced
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int height(TreeNode* root){
        if(root == NULL)
            return 0;
        int left = height(root->left);
        int right = height(root->right);
        return max(left+1, right+1);
    }
    bool isBalanced(TreeNode* root) {
        if(root == NULL)
            return true;
        if(abs(height(root->left) - height(root->right)) > 1)
            return false;
        return isBalanced(root->left) && isBalanced(root->right);
    }
};
111.二叉树的最小深度
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == NULL)
            return 0;
        if(root->left == NULL && root->right == NULL)
            return 1;
        int leftDepth = INT_MAX;
        int rightDepth = INT_MAX;
        if(root->left)
            leftDepth = minDepth(root->left);
        if(root->right)
            rightDepth = minDepth(root->right);
        return min(leftDepth, rightDepth) + 1;
    }
};
112.路径总和
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(root == NULL)
            return false;
        if(root->left == NULL && root->right == NULL){
            return root->val == sum;
        }        
        return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
    }
};
118.杨辉三角
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> res;
        for(int i = 0; i < numRows; ++i){
            res.push_back(vector<int>(i+1,1));
        }
        if(numRows > 2){
            for(int i = 2; i < numRows; ++i){
                for(int j = 1; j < i; ++j){
                    res[i][j] = res[i-1][j] + res[i-1][j-1];
                }
            }
        }
        return res;
    }
};
119.杨辉三角II 滚动数组
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        if(rowIndex < 0)
            return {};
        if(rowIndex == 0)
            return {1};
        vector<int> res(rowIndex+1, 0);
        res[0] = 1;
        vector<int> cur(res);
        for(int i = 1; i <= rowIndex; ++i){
            for(int j = 1; j <= i; ++j)
                res[j] = cur[j-1] + cur[j];
            cur = res;
        }
        return res;
    }
};
121.买入股票的最佳时机
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.empty())
            return 0;
        int buy = prices[0];
        int profit = 0;
        for(int i = 0; i < prices.size(); ++i){
            buy = min(buy, prices[i]);
            profit = max(profit, prices[i] - buy);
        }
        return profit;
    }
};
122.买入股票的最佳时机II
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.empty())
            return 0;
        int sum = 0;
        for(int i = 0; i < prices.size() -1 ; i++){
            if(prices[i] < prices[i+1])
                sum += prices[i+1] - prices[i];
        }
        return sum;
    }
};
125.验证回文串
class Solution {
public:
    bool isAlphaNum(char &c){
        if(c >= 'a' && c <= 'z')
            return true;
        if(c >= 'A' && c <= 'Z')
            return true;
        if(c >= '0' && c <= '9')
            return true;
        return false;
    }
    bool isPalindrome(string s) {
        if(s.empty())
            return true;
        int left = 0, right = s.size() - 1;
        while(left < right){
            if(!isAlphaNum(s[left]))
                ++left;
            else if(!isAlphaNum(s[right]))
                --right;
            else if((s[left] + 32 - 'a')%32 != (s[right] + 32 - 'a')%32)
                return false;
            else{
                ++left;
                --right;
            }
        }
        return true;
    }
};
136.只出现一次的数字 用异或(两次异或同一个数字归零)
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for(int elem : nums){
            res ^= elem;
        }
        return res;
    }
};
141.环形链表 用快慢指针
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *l1,*l2;
        l1 = l2 = head;
        while(l1 != NULL && l2 != NULL && l1->next != NULL){
            l1 = l1->next->next;
            l2 = l2->next;
            if(l1 == l2)
                return true;
        }
        return false;
    }
};
155.最小栈 两个stack实现 s2栈顶保存当前最小值
class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        s1.push(x);
        if(s2.empty() || x <= getMin())
            s2.push(x);
    }
    
    void pop() {
        if(s1.top() == getMin())
            s2.pop();
        s1.pop();
    }
    
    int top() {
        return s1.top();
    }
    
    int getMin() {
        return s2.top();
    }
private:
    stack<int> s1;
    stack<int> s2;
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */
160.相交链表 两个临时链表以不同顺序连续遍历两个给定链表
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA == NULL || headB == NULL)
            return NULL;
        ListNode *l1 = headA, *l2 = headB;
        while(l1 != l2){
            if(l1)
                l1 = l1->next;
            else
                l1 = headB;
            if(l2)
                l2 = l2->next;
            else    
                l2 = headA;
        }
        return l1;
    }
};
167.两数之和II输入有序数组
class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        vector<int> index;
        int i = 0, j = numbers.size() - 1;
        while(i != j){
            if(numbers[i] + numbers[j] == target){
                index.push_back(i+1);
                index.push_back(j+1);
                break;
            }
            if(numbers[i] + numbers[j] < target)
                ++i;
            else
                --j;
        }
        return index;
    }
};
168.excel列表名
class Solution {
public:
    string convertToTitle(int n) {
        string s;
        while(n != 0){
            int temp = (n-1)%26;
            s = (char)(temp + 'A') + s;
            n = (n-1)/26;
        }
        return s;
    }
};
169.多数元素 
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int len = nums.size();
        int count = len >> 1;
        int res = 0;
        map<int,int> m;
        for(int i = 0; i < len; i++){
            m[nums[i]]++;
            if(m[nums[i]] > count)
                res = nums[i];
        }
        return res;
    }
};

你可能感兴趣的:(LeetCode算法简单难度)