最近搞了几场编程比赛,面试题或者是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:

  1. Input: 12
  2. Output: 21

Example 2:

  1. Input: 21
  2. Output: -1
  1. class Solution {
  2. public:
  3. int nextGreaterElement(int n) {
  4. char s[];
  5. sprintf(s, "%d", n);
  6. int len = strlen(s);
  7. sort(s, s + len);
  8. do{
  9. if(atoll(s) > n && atoll(s) < INT_MAX)
  10. return atoll(s);
  11. }while(next_permutation(s, s + len));
  12. return -;
  13. }
  14. };

1. 使用 next_permutation

2. atoll ~ atoi

3. to_string()          int->string

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

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

  1.  

  第三题:

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:

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

Example 2:

  1. Input:
  2. 2
  3. / \
  4. 1 3
  5. Output: 3
  6. Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1].
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10.  
  11. class Solution {
  12. public:
  13. int longestConsecutive(TreeNode* root) {
  14. if(!root) return ;
  15. unordered_map<TreeNode*, vector<TreeNode*>>g;
  16. queue<TreeNode*> que;
  17. que.push(root);
  18. while(!que.empty()){
  19. TreeNode *u = que.front();
  20. que.pop();
  21. if(u -> left){
  22. g[u].push_back(u -> left);
  23. g[u -> left].push_back(u);
  24. que.push(u -> left);
  25. }if(u -> right){
  26. g[u].push_back(u -> right);
  27. g[u -> right].push_back(u);
  28. que.push(u -> right);
  29. }
  30. }
  31. int ret = ;
  32. for(auto &it: g){
  33. ret = max(ret, helper(it.first, g));
  34. }
  35. return ret + ;
  36. }
  37.  
  38. int helper(TreeNode* u, unordered_map<TreeNode*, vector<TreeNode*>>&g){
  39. if(u == NULL) return ;
  40. int level = ;
  41. for(int i = ; i < g[u].size(); i ++){
  42. if(g[u][i] -> val == u -> val + ){
  43. level = max(level, helper(g[u][i], g) + );
  44. }
  45. }
  46. return level;
  47. }
  48. };
  49.  
  50. /*
  51. class Solution {
  52. public:
  53. int longestConsecutive(TreeNode* root) {
  54. if (!root) return 0;
  55. unordered_map<TreeNode*, vector<TreeNode*>> g;
  56. queue<TreeNode*> q;
  57. q.push(root);
  58. while (!q.empty()) {
  59. TreeNode* t = q.front();
  60. q.pop();
  61. if (t->left) { g[t].push_back(t->left); g[t->left].push_back(t); q.push(t->left); }
  62. if (t->right) { g[t].push_back(t->right); g[t->right].push_back(t); q.push(t->right); }
  63. }
  64. int result = 0;
  65. for (auto& p : g) {
  66. result = max(result, helper(p.first, g));
  67. }
  68. return result + 1;
  69.  
  70. }
  71. int helper(TreeNode* s, unordered_map<TreeNode*, vector<TreeNode*>>& g) {
  72. int level = 0;
  73. for (TreeNode* n : g[s]) {
  74. if (n->val == s->val + 1) level = max(level, 1 + helper(n, g));
  75. }
  76. return level;
  77. }
  78. };
  79. */

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

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

LeetCode 要记得一些小trick的更多相关文章

  1. 通过一个小Trick实现shader的像素识别/统计操作

    2018/12/14日补充:后来发现compute shader里用AppendStructuredBuffer可以解决这类问题,请看这里:https://www.cnblogs.com/hont/p ...

  2. 一些计数小Trick

    一些计数小Trick 虽然说计数问题如果不是特别傻逼的话想做出来基本随缘. 但是掌握一些基本的计数方法还是十分有必要的. 想到了就更新. 1. 对于排列的DP问题,一般是不能够按照位置一个一个放的,一 ...

  3. 语法上的小trick

    语法上的小trick 构造函数 虽然不写构造函数也是可以的,但是可能会开翻车,所以还是写上吧.: 提供三种写法: ​ 使用的时候只用: 注意,这里的A[i]=gg(3,3,3)的"gg&qu ...

  4. file_put_contens小trick

    file_put_contents tricks 0x01 trick1 来自于P神的实例: <?php $text = $_GET['text']; if(preg_match('[<& ...

  5. Python multiprocessing 基础使用和小trick

    最近进行数据预处理时(噪声插入),单进程严重影响实验周期,故学习了multiprocessing并发执行不同数据集的处理,加快执行效率.现于此进行一些简单记录以供日后参考. 1. 基础: From m ...

  6. web小trick

    1.linux下交换文件 .index.php.swp 有时可查看源码2.当php后缀被过滤的时候可以直接对ph开头的后缀进行一个fuzz测试可以上传的文件后缀名3.curl -x 123.45.67 ...

  7. Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

    https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...

  8. 小trick总结

    一个圆上的整点数量不会很多.(Cf AIM TR 5 F) 二分图完美匹配求字典序最小的方案:先一遍匈牙利求出任意一组完美匹配.再跑一遍逐位确定,要求不能修改编号比它小的匹配.(LG 4100) 如果 ...

  9. 关于php文件操作的几个小trick

    记录一些ctf题目中近期遇到的一些文件操作trick,不定时更新 1.move_uploaded_file 一般用来保存上传的文件,第二个参数一般是最终保存的文件名,针对此函数,若在一定条件下$new ...

随机推荐

  1. VIM使用技巧14

    经常使用vim的童鞋可能会注意到,实际操作过程中,处于插入模式中是非常少的,更多的是查看和浏览,偶尔修改即可.因此,快速从插入模式退出进入普通模式,就显得非常重要.主要有以下四种方式: 一.在插入模式 ...

  2. P1605 迷宫 洛谷

    https://www.luogu.org/problem/show?pid=1605 题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐 ...

  3. 玩转iOS开发 - 消息推送

    消息推送

  4. AE After Effect 如何分段渲染

    如果只要第一段的话,你把要输出的那段首尾处分别按下B键和N键,这样输出时就会只输出这一段了(拖动首尾的栏目修改起始和终止的时间):如果是批量渲染的话你只要在这些不同的合成层里,每个按下ctrl+M键, ...

  5. SSLStrip 终极版 —— location 瞒天过海

    之前介绍了 HTTPS 前端劫持 的方案,尽管非常有趣.然而现实却并不理想. 其唯一.也是最大的缺陷.就是无法阻止脚本跳转.若是没有这个缺陷,那就非常完美了 -- 当然也就没有必要写这篇文章了. 说究 ...

  6. 工作总结 js for 循环遍历 json 数据

    [{"Branch":"Bangkok","2017-01|Replenishment":"0","2017- ...

  7. asp.net mvc 的 视图(view )的模块化开发

    目前所在项目有一个视图,几个模块都涉及到,对应不同的开发人员,虽然有SVN在管理,但代码冲突时有发生.并且大家的代码挤于其中,逻辑比较容易混乱. 将不同部件独立出去,实有必要. 分离方式,我知道的有 ...

  8. 解决Hibernate4执行update操作,不更新数据的问题

    后台封装java对象,使用hibernate4再带的update,执行不更新数据,不报错. 下面贴出解决方法: 失败的方法 hibernate自带update代码:(失效) Session sessi ...

  9. BZOJ 1055 区间DP

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1144  Solved: 668[Submit][Statu ...

  10. ugc pgc ogc web2.0 mgc

    http://yjy.people.com.cn/n/2014/0120/c245079-24169402.html machine