LeetCode 要记得一些小trick

  最近搞了几场编程比赛,面试题或者是LeetCode周赛。每次都不能做完,发现时间不够用。

  看了别人的代码才知道,同样实现相同的功能,可能别人只需要用一个恰当的函数,就会比自己少些不少代码,争得了时间。所以这些小技巧对于提升名次来说,十分重要。以后需要

更加重视才行。

  拿LeetCode Weekly Contest 27 来举个例子:

  第二题:

556. Next Greater Element III

Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.

Example 1:

Input: 12
Output: 21

Example 2:

Input: 21
Output: -1
class Solution {
public:
    int nextGreaterElement(int n) {
        char s[32];
        sprintf(s, "%d", n);
        int len = strlen(s);
        sort(s, s + len);
        do{
            if(atoll(s) > n && atoll(s) < INT_MAX)
                return  atoll(s);
        }while(next_permutation(s, s + len));
        return -1;
    }
};

1. 使用 next_permutation

2. atoll ~ atoi

3. to_string()          int->string

sprintf(s, "%d", n);     int->char{]

4.32bit int 最大值 INT_MAX 定义在头文件<limits.h>中。


  第三题:

549. Binary Tree Longest Consecutive Sequence II

Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree.

Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not valid. On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order.

Example 1:

Input:
        1
       / 
      2   3
Output: 2
Explanation: The longest consecutive path is [1, 2] or [2, 1].

Example 2:

Input:
        2
       / 
      1   3
Output: 3
Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1].
/**
 * 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 longestConsecutive(TreeNode* root) {
        if(!root) return 0;
        unordered_map<TreeNode*, vector<TreeNode*>>g;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            TreeNode *u = que.front();
            que.pop();
            if(u -> left){
                g[u].push_back(u -> left);
                g[u -> left].push_back(u);
                que.push(u -> left);
            }if(u -> right){
                g[u].push_back(u -> right);
                g[u -> right].push_back(u);
                que.push(u -> right);
            }
        }
        int ret = 0;
        for(auto &it: g){
            ret = max(ret, helper(it.first, g));
        }
        return ret + 1;
    }
    
    int helper(TreeNode* u, unordered_map<TreeNode*, vector<TreeNode*>>&g){
        if(u == NULL) return 0;
        int level = 0;
        for(int i = 0; i < g[u].size(); i ++){
            if(g[u][i] -> val == u -> val + 1){
                level = max(level, helper(g[u][i], g) + 1);    
            }
        }
        return level;
    }
};


/*
class Solution {
public:
    int longestConsecutive(TreeNode* root) {
        if (!root) return 0;
        unordered_map<TreeNode*, vector<TreeNode*>> g;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            TreeNode* t = q.front();
            q.pop();
            if (t->left) { g[t].push_back(t->left); g[t->left].push_back(t); q.push(t->left); }
            if (t->right) { g[t].push_back(t->right); g[t->right].push_back(t); q.push(t->right); }
        }
        int result = 0;
        for (auto& p : g) {
            result = max(result, helper(p.first, g));
        }
        return result + 1;

    }
    int helper(TreeNode* s, unordered_map<TreeNode*, vector<TreeNode*>>& g) {
        int level = 0;
        for (TreeNode* n : g[s]) {
            if (n->val == s->val + 1) level = max(level, 1 + helper(n, g));
        }
        return level;
    }
};
*/

我的代码一开始传递参数的时候,没传引用,就TLE了。

传递引用虽然不安全,但是要比cp一个新的数据结构要快太多了。

原文地址:https://www.cnblogs.com/luntai/p/6684687.html