【LeetCode】LCP 07. 传递信息】的更多相关文章

LCP 07. 传递信息 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/chuan-di-xin-xi 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 题目描述 小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下: 有 n 名玩家,所有玩家编号分别为 0 - n-1,其中小朋友 A 的编号为 0 每个玩家都有固定的若干个可传信息的其他玩家(也可能没有).传信息的关系是单向的(比如 A 可以向 B…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcode-cn.com/problems/na-ying-bi/ 题目描述 小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下: 有 n 名玩家,所有玩家编号分别为 0 - n-1,其中小朋友 A 的编号为 0 每个玩家都有固定的若干个可传信息的其他玩家(也可能没有).传信息的关系是单向的(比如…
题目 小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下: 有 n 名玩家,所有玩家编号分别为 0 - n-1,其中小朋友 A 的编号为 0 每个玩家都有固定的若干个可传信息的其他玩家(也可能没有).传信息的关系是单向的(比如 A 可以向 B 传信息,但 B 不能向 A 传信息). 每轮信息必须需要传递给另一个人,且信息可重复经过同一个人 给定总玩家数 n,以及按 [玩家编号,对应可传递玩家编号] 关系组成的二维数组 relation.返回信息从小 A (编号 0 ) 经过 k 轮传递…
小A 和 小B 在玩猜数字.小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜.他们一共进行三次这个游戏,请返回 小A 猜对了几次? 输入的guess数组为 小A 每次的猜测,answer数组为 小B 每次的选择.guess和answer的长度都等于3. 示例 1: 输入:guess = [1,2,3], answer = [1,2,3] 输出:3 解释:小A 每次都猜对了. 示例 2: 输入:guess = [2,2,3], answer = [3,2,…
桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中.我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数. 示例 1: 输入:[4,2,1] 输出:4 解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完. 示例 2: 输入:[2,3,10] 输出:8 限制: 1 <= n <= 4 1 <= coins[i] <= 10 来源:力扣(LeetCode) 链接:https://leetcode…
题目解析: 对于本题主要的核心是对于一个指令字符串如“RURUU”,如果我们假设它的终点坐标为(8,8),其实只要统计指令字符串中的R的个数和U的个数(对于我给出的例子而言,num_R == 2,num_U == 3),显然不管我们是否能到达终点,这条指令至少要走不止一遍才有可能,那么我们只要将它在达到终点前必走的x轮减去(对于我给出的例子而言x == min(8 / 2 ,8 / 3)== 2,则忽略中间走的轮数,就能把终点的位置转化为(4,2)),则就能求出最后那不足一轮的情况下需要U多少个…
[算法训练营day4]LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II LeetCode24. 两两交换链表中的节点 题目链接:24. 两两交换链表中的节点 初次尝试 比较暴力的解法,利用三个指针,进行类似反转链表里面的反转next指针指向的操作,然后三个指针整体向后移动到下一组节点,暴力但是ac. /** * Definition for singly-link…
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for the nice explanation of recursion and backtracking, highly recommended. in hdu 2553 cout N-Queens solutions problem, dfs is used. // please ignore, bel…
最近忙着面试耽误了几天,今天刷了07,09都是字符串处理,一个是大数反转,一个是回文数判断,我都是转成字符串处理的,过了是过了,但是挺慢的,先记着,等有机会优化下 题目 给定一个 32 位有符号整数,将整数中的数字进行反转. /** * @param {number} x * @return {number} */ var reverse = function(x) { x=x.toString(); if(x[x.length-1]==0&&x.length==1){ return pa…
题目如下: 小A 和 小B 在玩猜数字.小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜.他们一共进行三次这个游戏,请返回 小A 猜对了几次? 输入的guess数组为 小A 每次的猜测,answer数组为 小B 每次的选择.guess和answer的长度都等于3. 示例 1: 输入:guess = [1,2,3], answer = [1,2,3]输出:3解释:小A 每次都猜对了. 示例 2: 输入:guess = [2,2,3], answer = […
面试题 01.07. 旋转矩阵 给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节.请你设计一种算法,将图像旋转 90 度. 不占用额外内存空间能否做到? 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换 日期 题目地址:https://leetcode-cn.com/problems/na-ying-bi/ 题目描述 桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中.我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数. 示例 1: 输入:[4,2,1] 输出:4 解释:第一堆力扣币最少需要拿 2 次,第二堆最少…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 两次翻转 日期 题目地址:https://leetcode-cn.com/problems/longest-happy-string/ 题目描述 给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节.请你设计一种算法,将图像旋转 90 度. 不占用额外内存空间能否做到? 示例 1: 给定 matrix = [ [1,2,3], [4,…
这个也是简单题目.可是关键在于题意的理解. 题目原文就一句话:Write a function to find the longest common prefix string amongst an array of strings. 题意是给一个字符串数组,找出这个字符串数组中全部字符串的最长公共前缀. 注意是限定的前缀.而不是子串. 所以,直接的解法就是以第一个字符串为基准,逐个比較每一个字符.算法复杂度也显然是O(M*N).M是字符串的个数,N是最长前缀的长度. 代码例如以下: class…
题目如下: 有一个同学在学习分式.他需要将一个连分数化成最简分数,你能帮助他吗? 连分数是形如上图的分式.在本题中,所有系数都是大于等于0的整数. 输入的cont代表连分数的系数(cont[0]代表上图的a0,以此类推).返回一个长度为2的数组[n, m],使得连分数的值等于n / m,且n, m最大公约数为1. 示例 1: 输入:cont = [3, 2, 0, 2]输出:[13, 4]解释:原连分数等价于3 + (1 / (2 + (1 / (0 + 1 / 2)))).注意[26, 8],…
题目如下: 力扣团队买了一个可编程机器人,机器人初始位置在原点(0, 0).小伙伴事先给机器人输入一串指令command,机器人就会无限循环这条指令的步骤进行移动.指令有两种: U: 向y轴正方向移动一格R: 向x轴正方向移动一格.不幸的是,在 xy 平面上还有一些障碍物,他们的坐标用obstacles表示.机器人一旦碰到障碍物就会被损毁. 给定终点坐标(x, y),返回机器人能否完好地到达终点.如果能,返回true:否则返回false. 示例 1: 输入:command = "URR"…
题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/ 给定两个(单向)链表,判定它们是否相交并返回交点.请注意相交的定义基于节点的引用,而不是基于节点的值.换句话说,如果一个链表的第k个节点与另一个链表的第j个节点是同一节点(引用完全相同),则这两个链表相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5]…
题目 题目链接:https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/ 初步题解 先放代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *…
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.…
开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ tmp = []…
August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# SortedList class. Try to get some workout on the C# programming practice. 我的体会是代码写得太少,  要多读不同的代码, 多调试, 开阔对一个问题思考的思路和深度, 准确度, 实现能力. 1. Blog to read: http://…
August 2, 2015 在http://zzk.cnblogs.com/ 用"找一找", 花了几个小时, 找出比较好的Leetcode博客. Read the leetcode question and solutions: 1. Find lowest common ancestor in binary tree: blog of a facebook programmer working on leetcode questions: 递归版本:空间O(1),时间O(n) ht…
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, a…
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p…
转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum(closest), 4sum等问题, 这些也是面试里面经典的问题, 考察是否能够合理利用排序这个性质, 一步一步得到高效的算法. 经过总结, 本人觉得这些问题都可以使用一个通用的K sum求和问题加以概括消化, 这里我们先直接给出K Sum的问题描述和算法(递归解法), 然后将这个一般性的方法套用…
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: F(k…
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi…
Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string will be in the form of each letter followed by a positive integer representing the numbe…
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find out their common interest with the least list index sum. If there is a choice tie betw…
We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1. Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subseque…