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[];
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. 使用 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 ;
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 = ;
for(auto &it: g){
ret = max(ret, helper(it.first, g));
}
return ret + ;
} int helper(TreeNode* u, unordered_map<TreeNode*, vector<TreeNode*>>&g){
if(u == NULL) return ;
int level = ;
for(int i = ; i < g[u].size(); i ++){
if(g[u][i] -> val == u -> val + ){
level = max(level, helper(g[u][i], g) + );
}
}
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一个新的数据结构要快太多了。
LeetCode 要记得一些小trick的更多相关文章
- 通过一个小Trick实现shader的像素识别/统计操作
2018/12/14日补充:后来发现compute shader里用AppendStructuredBuffer可以解决这类问题,请看这里:https://www.cnblogs.com/hont/p ...
- 一些计数小Trick
一些计数小Trick 虽然说计数问题如果不是特别傻逼的话想做出来基本随缘. 但是掌握一些基本的计数方法还是十分有必要的. 想到了就更新. 1. 对于排列的DP问题,一般是不能够按照位置一个一个放的,一 ...
- 语法上的小trick
语法上的小trick 构造函数 虽然不写构造函数也是可以的,但是可能会开翻车,所以还是写上吧.: 提供三种写法: 使用的时候只用: 注意,这里的A[i]=gg(3,3,3)的"gg&qu ...
- file_put_contens小trick
file_put_contents tricks 0x01 trick1 来自于P神的实例: <?php $text = $_GET['text']; if(preg_match('[<& ...
- Python multiprocessing 基础使用和小trick
最近进行数据预处理时(噪声插入),单进程严重影响实验周期,故学习了multiprocessing并发执行不同数据集的处理,加快执行效率.现于此进行一些简单记录以供日后参考. 1. 基础: From m ...
- web小trick
1.linux下交换文件 .index.php.swp 有时可查看源码2.当php后缀被过滤的时候可以直接对ph开头的后缀进行一个fuzz测试可以上传的文件后缀名3.curl -x 123.45.67 ...
- Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0
https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...
- 小trick总结
一个圆上的整点数量不会很多.(Cf AIM TR 5 F) 二分图完美匹配求字典序最小的方案:先一遍匈牙利求出任意一组完美匹配.再跑一遍逐位确定,要求不能修改编号比它小的匹配.(LG 4100) 如果 ...
- 关于php文件操作的几个小trick
记录一些ctf题目中近期遇到的一些文件操作trick,不定时更新 1.move_uploaded_file 一般用来保存上传的文件,第二个参数一般是最终保存的文件名,针对此函数,若在一定条件下$new ...
随机推荐
- Oracle建表提示SQL 错误: ORA-00904: : 标识符无效
Oracle建表提示: 错误报告:SQL 错误: ORA-00904: : 标识符无效00904. 00000 - "%s: invalid identifier"*Cause: ...
- 用pc构建DIY计算集群
-----------------------------------------------------------------用pc构建DIY计算集群目录/构建计算集群|-- /0前言|-- /1 ...
- activiti自己定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义
注:(1)环境搭建:activiti自己定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自己定义流程之Spr ...
- Linux改动/etc/profile配置错误command is not found自救方法
我的CSDN博客地址: http://blog.csdn.net/caicongyang 博主之前在改动了/etc/profile配置文件方法后,导致bash命令无法用 运行ls命令结果例如以下: - ...
- mysql 將時間戳直接轉換成日期時間
from_unixtime()是MySQL裏的時間函數 Sql代碼 select uid,userid,username,email,FROM_UNIXTIME(addtime,'%Y年%m月%d') ...
- You don't have permission to access ××× on this server.
之前开发项目一直在linux上用的xampp集成环境,前几天突然想移到window上面去. 開始在window上安装了一个集成环境(名字大概是 Uniform Service),把项目文件已过去, o ...
- 一起talk C栗子吧(第七回:C语言实例--进制转换)
各位看官们.大家好,从今天開始.我们讲大型章回体科技小说 :C栗子,也就是C语言实例. 闲话休提, 言归正转.让我们一起talk C栗子吧! 看官们.上一回中咱们说的是生成随机数的样例.这一回咱们说的 ...
- expand_dims
tf.expand_dims | TensorFlow https://tensorflow.google.cn/api_docs/python/tf/expand_dims tf.expand_ ...
- Linux/Android——usb触摸屏驱动 - usbtouchscreen (一)【转】
本文转载自:http://blog.csdn.net/jscese/article/details/41827495 最近需要往TV上装一个触摸屏设备,现在比较常见的就是使用usb接口的触摸框,适用于 ...
- 【BZOJ 5165】 树上倍增
[题目链接] 点击打开链接 [算法] 树上倍增,时间复杂度 : O(qklog(n)) [代码] #include<bits/stdc++.h> using namespace std; ...