zoukankan      html  css  js  c++  java
  • 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记

     496. Next Greater Element I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

    The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

    class Solution {
    public:
        vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
            vector<int> tNums;
            for(int i = 0, sz = findNums.size(); i < sz; i++){
                bool finded = false;
                int index = 0;
                for(int j = findN(findNums[i], nums), nsz = nums.size(); j < nsz; j++){
                    if(nums[j] > findNums[i]){
                        index = j;
                        break;
                    }
                }
                if(index == 0) tNums.push_back(-1);
                else tNums.push_back(nums[index]);
            }
            return tNums;
        }
        // return index of nums[k] == num
        int findN(int num, vector<int>& nums){
            for(int i = 0, sz = nums.size(); i < sz; i++){
                if(nums[i] == num){
                    return i;
                }
            }
            return -1;
        }
    };
    c++

    513. Find Bottom Left Tree Value

    Given a binary tree, find the leftmost value in the last row of the tree.

     (BFS)

    /**
     * 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 findBottomLeftValue(TreeNode* root) {
            queue<TreeNode*> leaves;
            leaves.push(root);
            while(!leaves.empty()){
                TreeNode *temp = leaves.front();leaves.pop();
                if(!temp->right && !temp->left && leaves.empty()) return temp->val;
                if(temp->right)
                    leaves.push(temp->right);
                if(temp->left)
                    leaves.push(temp->left);
            }
        }
    };
    c++
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def findBottomLeftValue(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            list = []
            list.append(root)
            while(len(list)):
                temp = list.pop(0)
                if(temp.right == None and temp.left == None and len(list) == 0):
                    return temp.val
                if(temp.right):
                    list.append(temp.right)
                if(temp.left):
                    list.append(temp.left)
            
    python3

    540. Single Element in a Sorted Array

    Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.

     (二分搜索)

    class Solution {
    public:
        int singleNonDuplicate(vector<int>& nums) {
            int left = 0, right = nums.size()-1;
            int mid = (left + right) / 2;
            if(mid == left) return nums[mid];
            while(left < right){
                if(mid % 2 == 0){
                    if(Left(mid, nums)) {
                        right = mid-1;
                        mid = (right + left)/2;
                    }
                    else if(Right(mid, nums)){
                        left = mid+1;
                        mid = (left + right)/2;
                    }
                    else return nums[mid];
                }else{
                    if(Left(mid, nums)){
                        left = mid+1;
                        mid = (left + right)/2;
                    }else{
                        right = mid-1;
                        mid = (right + left)/2;
                    }
                }
            }
            return nums[mid];
        }
        
        bool Left(int i, vector<int>& nums){
            int left = 0;
            if(i == 0) return false;
            else if (nums[i] != nums[i-1]) return false;
            else return true;
        }
        
        bool Right(int i, vector<int>& nums){
            int right = nums.size()-1;
            if (i == right) return false;
            else if (nums[i] != nums[i+1]) return false;
            else return true;
        }
    };
    c++

    647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string.

    The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

    class Solution {
    public:
       int countSubstrings(string s) {
            int left = 0, right = s.size();
            // cout<<right<<endl;
            // return traceBack(s, left, right);
            int count = 0;
            for(int i = left; i < right; i++){
                for(int j = i; j < right; j++){
                    bool palindromic = true;
                    for(int ind = i, end = j; ind <= end; ind++, end--){
                        if(s[ind] != s[end]) palindromic = false;
                    }
                    if(palindromic) count++;
                }
            }
            return count;
        }
    };
    c++

    637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

    /**
     * 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<double> averageOfLevels(TreeNode* root) {
            queue<TreeNode*> q;
            queue<long long> level;
            vector<double> ave;
            
            q.push(root);
            level.push(0);
            long long last = 0, sum = 0, num = 0;
            while(!q.empty()){
                TreeNode * temp = q.front();q.pop();
                long long tp = level.front(); level.pop();
                
                if(temp->right) {q.push(temp->right);level.push(tp+1);}
                if(temp->left)  {q.push(temp->left);level.push(tp+1);}
                
                if(tp == last){
                    sum += temp->val;
                    num ++;
                }else{
                    ave.push_back((double)sum/(double)num);
                    sum = 0;
                    num = 1;
                    last = tp;
                    sum += temp->val;
                }
                if(q.empty()) ave.push_back((double)sum/(double)num);
            }
            return ave;
        }
    };
    c++

    515. Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree.

    /**
     * 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<int> largestValues(TreeNode* root) {
            queue<TreeNode*> q;
            queue<int> level;
            vector<int> ave;
            
            if(root == NULL) return ave;
            
            q.push(root);
            level.push(0);
            int last = 0, max = -(1<<31);
            while(!q.empty()){
                TreeNode * temp = q.front();q.pop();
                int tp = level.front(); level.pop();
                
                if(temp->right) {q.push(temp->right);level.push(tp+1);}
                if(temp->left)  {q.push(temp->left);level.push(tp+1);}
                
                if(tp == last){
                    if(max < temp->val)
                        max = temp->val;
                }else{
                    ave.push_back(max);
                    max = -(1<<31);
                    last = tp;
                    if(max < temp->val)
                        max = temp->val;
                }
                if(q.empty()) ave.push_back(max);
            }
            return ave;
        }
    };
    c++
  • 相关阅读:
    Anaconda 换国内源、删源最全集锦(转载)
    【2019】安装anaconda及环境变量的配置(安装第三方库测试)(转载)
    python 中 urlparse 模块介绍
    正则表达式之前瞻后顾
    indows下如何安装python第三方库lxml
    Ubuntu下开启SSH服务 vs:18
    Linux终端访问网页
    关于ubuntu无线网络配置
    SQL语句报错,无法绑定由多个部分组成的标识符解决
    "@P0"附近有语法错误解释及定位修复
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/7871786.html
Copyright © 2011-2022 走看看