LeetCode Weekly Contest 117
已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode。
这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~)。
第一题:
965. Univalued Binary Tree
Return true
if and only if the given tree is univalued.
Example 1:
Input: [1,1,1,1,1,null,1]
Output: true
Example 2:
Input: [2,2,2,5,2]
Output: false
Note:
- The number of nodes in the given tree will be in the range
[1, 100]
. - Each node's value will be an integer in the range
[0, 99]
.
题目意思很简单,就是给你一棵树,让你判断这棵树所有节点的值是不是都是同一个数。
直接遍历节点,然后记录下来再判断就好。(其实可以边遍历边判断)
/**
* 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 {
private:
int a[];
public: void view(TreeNode* root) {
if( root != NULL ) a[root->val] ++;
if( root->right != NULL ) view(root->right);
if( root->left != NULL ) view(root->left);
} bool isUnivalTree(TreeNode* root) {
memset(a, , sizeof(a));
view(root);
int cnt = ;
for(int i=; i<; i++) {
if( a[i] != ) cnt ++;
}
return cnt == ;
}
};
第二题:
967. Numbers With Same Consecutive Differences
Return all non-negative integers of length N
such that the absolute difference between every two consecutive digits is K
.
Note that every number in the answer must not have leading zeros except for the number 0
itself. For example, 01
has one leading zero and is invalid, but 0
is valid.
You may return the answer in any order.
Example 1:
Input: N = 3, K = 7
Output: [181,292,707,818,929]
Explanation: Note that 070 is not a valid number, because it has leading zeroes.
Example 2:
Input: N = 2, K = 1
Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]
Note:
1 <= N <= 9
0 <= K <= 9
题目意思很简单,看样例基本能明白,给你一个长度n,和一个限定差值k,让你找出所有长度为n并且相邻数位之间的差值等于k的这些数(任何顺序),除0之外不能有任何数是以0开头。
有两个坑点:
1、当N为1的时候,0是正确的数。
2、当K为0的时候,注意不要重复计算。
class Solution {
public:
vector<int> numsSameConsecDiff(int N, int K) {
vector<int> ans;
if( N == ) ans.push_back();
for(int i=; i<; i++) {
queue<int> q;
q.push(i);
int len = N-;
while( len!= ) {
int si = q.size();
while( si -- ) {
int st = q.front(); q.pop();
int last = st % ;
if( last + K < ) q.push(st*+last+K);
if( last - K >= && (last+K != last-K) ) q.push(st*+(last-K));
}
len --;
}
while( !q.empty() ) {
int top = q.front();
ans.push_back(top);
q.pop();
}
}
return ans;
}
};
点击查看代码
LeetCode Weekly Contest 117的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- c语言二级指针内存模型
第一种: 指针数组作为输入参数 char *myArray[] = {"aaaaaa", "ccccc", "bbbbbb", " ...
- Python 面试中可能会被问到的30个问题
第一家公司问的题目 1 简述解释型和编译型编程语言? 解释型语言编写的程序不需要编译,在执行的时候,专门有一个解释器能够将VB语言翻译成机器语言,每个语句都是执行的时候才翻译.这样解释型语言每执行一次 ...
- SSM 记录
前言:本过程从0开始,先是导入最核心的jar包,然后随着ssm中的功能实现,打包===>启动===>报错,一步步解决问题,增加额外的必须的jar包来熟悉ssm 1.导包(核心包) myba ...
- tcpdump我的交叉编译(mips)
一.libpcap交叉编译 1.下载libpcap-1.8.1(http://www.tcpdump.org/) 2.解压 3.修改configure文件 a.注释掉 #if test -z &quo ...
- react将字符串转义成html语句
在使用reactjs库的时候,会遇到将一段html的字符串,然后要将它插入页面中以html的形式展现,然而直接插入的话页面显示的就是这段字符串,而不会进行转义,可以用以下方法插入,便可以html的形式 ...
- python摸爬滚打之day29----socketserver实现服务端和多个客户端通信
什么是socketserver? TCP协议下的socket实现了服务端一次只能和一个客户端进行通信, 而socketserver实现了服务端一次能和多个客户端进行通信, 底层调用的还是socket. ...
- 学号 20175313 《实验三 敏捷开发与XP实践》实验报告
目录 实验三 敏捷开发与XP实践 一.实验内容 二.实验步骤 四.心得体会 五.码云链接 六.参考资料 实验三 敏捷开发与XP实践 一.实验内容 (1)编码标准 在IDEA中使用工具(Code-> ...
- Python3学习之路~7.1 静态方法、类方法、属性方法
静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量 ...
- linux 按文件大小排序
1.按文件大小查看文件 a.降序:ls -lsh moudaen@morton:~$ ls -lsh total 20M 20M -rw-r–r– 1 moudaen 65536 20M Nov 11 ...
- 3.2.2 SpringMVC配置式开发
SpringMVC配置式开发 1. SpringMVC运行原理(执行过程) 2. 需求 用户提交一个请求, 服务端处理器接收到请求后, 给出一条信息,在相应页面中显示该条信息 3. 开发步骤 (1) ...