zoukankan      html  css  js  c++  java
  • LeetCode Weekly Contest 21

    LeetCode Weekly Contest 21

    530. Minimum Absolute Difference in BST

     
    • User Accepted: 1081
    • User Tried: 1220
    • Total Accepted: 1107
    • Total Submissions: 2328
    • Difficulty: Easy

    Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

    Example:

    Input:
    
       1
        
         3
        /
       2
    
    Output:
    1
    
    Explanation:
    The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
    

    Note: There are at least two nodes in this BST.

    /**
     * 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:
        void dfs(TreeNode *root, int cur, int& val){
            if(root->left){
                if(abs(cur - root->left->val) < val){
                    val = abs(cur - root->left->val); 
                }
                dfs(root->left, cur, val); 
            }
            if(root->right){
                if(abs(cur - root->right->val) < val){
                    val = abs(cur - root->right->val); 
                }
                dfs(root->right, cur, val); 
            }
        }
        
        int getMinimumDifference(TreeNode* root) {
            if(root == NULL){
                return 0; 
            }
            int val = 0x3f3f3f3f; 
            dfs(root, root->val, val); 
            if(root->left){
                val = min(val, getMinimumDifference(root->left)); 
            }
            if(root->right){
                val = min(val, getMinimumDifference(root->right)); 
            }
            return val; 
        }
    };
    

      

    523. Continuous Subarray Sum

     
    • User Accepted: 824
    • User Tried: 1109
    • Total Accepted: 837
    • Total Submissions: 5803
    • Difficulty: Medium

    Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

    Example 1:

    Input: [23, 2, 4, 6, 7],  k=6
    Output: True
    Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
    

    Example 2:

    Input: [23, 2, 6, 4, 7],  k=6
    Output: True
    Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
    

    Note:

    1. The length of the array won't exceed 10,000.
    2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.
    class Solution {
    public:
        bool checkSubarraySum(vector<int>& nums, int k) {
            if(k == 0){
                int cnt = 0, i = 0; 
                while(i<nums.size()){
                    while(i<nums.size() && nums[i] == 0){
                        cnt++;
                        i++; 
                    }
                    if(cnt >= 2){
                        return true; 
                    }else{
                        cnt = 0; 
                    }
                    i++; 
                }
                return false; 
            }
            map<int, int> mp; 
            mp[0] = 0; 
            int tmp, sum = 0; 
            for(int i=0; i<nums.size(); ++i){
                sum += nums[i]; 
                tmp = sum % k; 
                if(mp.find(tmp) == mp.end()){
                    mp[tmp] = i+1; 
                }else{
                    if( (i+1 - mp[tmp]) >= 2){
                        return true; 
                    }
                }
            }
            return false; 
        }
    };
    

      

    524. Longest Word in Dictionary through Deleting

     
    • User Accepted: 609
    • User Tried: 827
    • Total Accepted: 619
    • Total Submissions: 2039
    • Difficulty: Medium

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

    Example 1:

    Input:
    s = "abpcplea", d = ["ale","apple","monkey","plea"]
    
    Output: 
    "apple"
    

    Example 2:

    Input:
    s = "abpcplea", d = ["a","b","c"]
    
    Output: 
    "a"
    

    Note:

    1. All the strings in the input will only contain lower-case letters.
    2. The size of the dictionary won't exceed 1,000.
    3. The length of all the strings in the input won't exceed 1,000.
        bool mycmp(string a, string b){
            if(a.length() == b.length()){
                for(int i=0; i<a.length(); ++i){
                    if(a[i] != b[i]){
                        return a[i] < b[i]; 
                    }
                }
            }
            return a.length() > b.length(); 
        }
    
    class Solution {
    public:
    
        string findLongestWord(string s, vector<string>& d) {
            sort(d.begin(), d.end(), mycmp); 
            
            for(int i=0; i<d.size(); ++i){
                int k = 0; 
                for(int j=0; j<s.length(); ++j){
                    if(k == d[i].length()){
                        return d[i]; 
                    }
                    if(s[j] == d[i][k]){
                        k++; 
                    }
                }
                if(k == d[i].length()){
                    return d[i]; 
                }
            }
            return "";
        }
    };
    

      

  • 相关阅读:
    (原创)分享一下最近搞的tiny210V2从nand启动支持(K9GAG08U0F).
    (转)uboot 与系统内核中 MTD分区的关系
    (转)mkimage制作linux内核映像 即uImage是怎么制作的
    ExtJs 一些属性备忘录
    CSS 元素选择器
    为什么匿名内部类参数必须为final类型
    Unbuntu 14.04 64位 搭建交叉编译环境
    eclipse调试运行
    Linux中cat命令的用法
    android之json
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/6446045.html
Copyright © 2011-2022 走看看