已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode。

这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~)。

第一题:

965. Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value.

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:

  1. The number of nodes in the given tree will be in the range [1, 100].
  2. 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. 1 <= N <= 9
  2. 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的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. Python基础之语句2

    一.if条件语句 1.语法: 2.if语句的真值表达式和条件表达式: 例题1:真值表达式判断奇偶数 num = int(input('请输入一个整数:')) if num % 2 : print('该 ...

  2. 逆向工程-真码保存在系统文件破解QQ游戏对对碰助手

    1)注册栏中输入任意值测试 1.2)记录弹出的关键字对话框 2.1)发送至PEID进行查壳 2.2)发现无壳 2.3)将软件载入OllyDBG程序 2.4)在反汇编栏下右键选择中文搜索引擎->智 ...

  3. 为Vue.js添加友好日志

    const isDebugEnabled = process.env.NODE_ENV !== "production"; const isInfoEnabled = true; ...

  4. vue 实现子向父传值

    父组件 <template> <div id="app"> <child @onChange='onChildValue'></child ...

  5. Android的几种Manager

    电话管理器TelephoneManager 第一个实例是获取网络和SIM卡信息:界面是一个列表,这里省略,Java代码如下: public class MainActivity extends Act ...

  6. laravel 控制器类DB类操作

    例子:TrGo表(trgo_chip): laravel框架建立:TrGoModel <?php namespace TrChaos\Model; class TrGoModel extends ...

  7. Linux基础命令1

    文件的操作: Tab键:自动补齐 反斜杠‘\’:强制换行 Ctrl+u:清空至行首 Ctrl+k:清空至行尾 Ctrl+l:清屏   --clear 清屏 Ctrl+c:取消本次命令编辑,中断当前操作 ...

  8. callback function(回调函数) - 术语

    回调函数(CallBack Function)的定义: [todo] 下面是callback的一个场景,linux内核.LwIP里也有使用callback funcs.

  9. 《图解HTTP》读书笔记(三:无状态协议/cookie管理状态)

    HTTP是一种不保存状态,即无状态(stateless)协议.HTTP协议自身不对请求和响应之间的通信状态进行保存. ——HTTP/1.1虽然是无状态协议,但为了实现期望的保持状态功能,于是引入了Co ...

  10. Cocos Creator LabelAtlas(艺术数字的使用)

    # 艺术数字资源 (LabelAtlas) **艺术数字资源** 是一种用户自定义的资源,它可以用来配置艺术数字字体的属性. ## 创建艺术数字资源 在 **资源管理器** 中右键,可以在如下菜单中找 ...