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 <= 90 <= 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 ...
随机推荐
- 【Python基础】lpthw - Exercise 40 模块、类和对象
一. 模块(module) 模块中包含一些函数和变量,在其他程序中使用该模块的内容时,需要先将模块import进去,再使用.操作符获取函数或变量,如 # This goes in mystuff.py ...
- k8s-N0.4-service
本章目录 k8s中的三种网络 service的构建及参数说明 一 k8s的三种网络 在k8s集群中,k8s是有三种网络类型的,下面我们看一下下面这个图 1 节点网络:顾名思义,节点网络就是你每台物理 ...
- 20190423 PowerDesigner 数据库模型快速建立
后面我在做一个视频的讲解记录吧! 那种讲解记录,只是为了演示按钮功能在什么地方,这个功能的作用是什么 这个软件相对比较简单的使用步骤,主要有三步 第一. 选择好你针对的数据库版本和类型创建数据库名称基 ...
- HTTP中Post与Put的区别
PUT请求是向服务器端发送数据的,从而改变信息,该请求就像数据库的update操作一样,用来修改数据的内容,但是不会增加数据的种类等,也就是说无论进行多少次PUT操作,其结果并没有不同. POST请求 ...
- 2019春第九周作业Compile Summarize
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 这里 我在这个课程的目标是 能更加进一步的够熟练掌握指针的用法 这个作业在那个具体方面帮助我实现目标 能解更多的题 参考文献与网址 C语言 ...
- 2019.04.16 python基础50
第五十一节 pycharm安装 https://www.jetbrains.com/pycharm/download/#section=windows 这是另一个叫jetbrains公司开发的 默认 ...
- table添加正确的样式
以前在做表格的时候,会在表格<table>标签中添加一些属性,来改变表格的样式,经常用到的有这几个 width 表格的宽度border 表格边框的宽度cellpadding 单元边沿与其 ...
- PeopleSoft 单点登录
第一次会以guest 用户进来,code 为空 第二次也以guest 进来code 从ssoAP获取到code,根据code 获取token,根据token 获取用户id.
- windows程序设计 获取磁盘容量
//磁盘分区的总容量(字节)=总簇数*每簇扇区数*每扇区字节数 //磁盘分区的空闲空间(字节)=空闲簇数*每簇扇区数*每扇区字节数 BOOL GetDiskFreeSpace( LPCTSTR lpR ...
- .net连接数据库递归
private void Form1_Load(object sender, EventArgs e) { List<Regions> regions = GetRegions().Whe ...