Leetcode
FBI-PC
这个作者很懒,什么都没留下…
展开
-
Lowest Common Ancestor of a Binary Tree(最小公共祖先)
Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common...原创 2019-07-23 15:33:34 · 352 阅读 · 0 评论 -
找出数组中次数超过n/k的数
题目:有一个整形数组,长度为n,有一个比较小的数k,求得是:数组中出现次数多余n/k的数字n = 100,k=2, n/k =50n = 10000, k = 10, n/k = 1000要求空间复杂度为O(k)时间复杂度为O(n*k)思路: 每次删除k个不同的数,最后剩下的数不同的数小于k等于k个就可能是答案,只需要在遍历一次即可。原理:大于n/k次的数字必定只存在小于等于k个。代...原创 2019-07-17 14:16:59 · 849 阅读 · 0 评论 -
Leetcode 215. Kth Largest Element in an Array
Kth Largest Element in an ArrayMedium1933161FavoriteShareFind the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct el...原创 2019-05-07 22:31:03 · 170 阅读 · 0 评论 -
Leetcode 213. House Robber II
House Robber IIMedium82926FavoriteShareYou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arran...原创 2019-05-07 22:24:50 · 217 阅读 · 0 评论 -
Leetcode 210. Course Schedule II 拓扑排序
Course Schedule IIMedium83763FavoriteShareThere are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have prerequisites, for example to take course 0 you have to ...原创 2019-04-13 15:42:38 · 190 阅读 · 0 评论 -
Leetcode 211. Add and Search Word - Data structure design Trie树 + dfs
Add and Search Word - Data structure designMedium76648FavoriteShareDesign a data structure that supports the following two operations:void addWord(word)bool search(word)search(word) can sea...原创 2019-04-13 15:39:49 · 185 阅读 · 0 评论 -
Leetcode 310. Minimum Height Trees
310. Minimum Height TreesFor an undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with min...原创 2019-04-11 09:58:45 · 225 阅读 · 0 评论 -
Leetcode 208. Implement Trie (Prefix Tree) Trie树(前缀树)
208. Implement Trie (Prefix Tree)MediumImplement a trie with insert, search, and startsWith methods.Example:Trie trie = new Trie();trie.insert(“apple”);trie.search(“apple”); // returns truetr...原创 2019-04-10 12:18:42 · 218 阅读 · 0 评论 -
Leetcode 204. Count Primes 用素数筛求素数
Count the number of prime numbers less than a non-negative number, n.Example:Input: 10Output: 4Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.Accepted224,278Submission...原创 2019-04-09 21:15:07 · 158 阅读 · 0 评论 -
Leetcode 206. Reverse Linked List链表逆置的两种解法
今天又看了一天的Pyhton,明天接着看Python,然后学习下opencv,然后复习下muduo。Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list ...原创 2019-04-09 21:09:02 · 372 阅读 · 0 评论 -
Leetcode Weekly Contest 131题解
1021. Remove Outermost Parentheses题目:A valid parentheses string is either empty (""), “(” + A + “)”, or A + B, where A and B are valid parentheses strings, and + represents string concatenation. Fo...原创 2019-04-07 15:48:08 · 265 阅读 · 0 评论 -
Leetcode Weekly Contest 130 题解
第一题:Binary Prefix Divisible By 5Binary Prefix Divisible By 5Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-...原创 2019-04-01 17:10:15 · 171 阅读 · 0 评论 -
Leetcode cWeekly Contest 129
Leetcode 1020. Partition Array Into Three Parts With Equal Sum题目:Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.Forma...原创 2019-03-24 12:48:28 · 154 阅读 · 0 评论 -
Leetcode 207. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:...原创 2019-03-23 15:54:19 · 135 阅读 · 0 评论 -
Leetcode 200. Number of Island
Given a 2d grid map of '1’s (land) and '0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume...原创 2019-03-21 21:47:22 · 168 阅读 · 0 评论 -
Leetcode 166. Fraction to Recurring Decimal
好久没做模拟题了,所以找个感觉,教训,写之前一定要理好逻辑。题目:166. Fraction to Recurring DecimalMedium401904FavoriteShareGiven two integers representing the numerator and denominator of a fraction, return the fraction in ...原创 2019-03-18 18:48:20 · 146 阅读 · 0 评论 -
Leetcode Weekly Contest 128
做出来三个第一题:Leetcode 1012. Complement of Base 10 Integer题目:Every non-negative integer N has a binary representation. For example, 5 can be represented as “101” in binary, 11 as “1011” in binary, and...原创 2019-03-17 19:10:56 · 107 阅读 · 0 评论 -
2019/3/16 字节跳动笔试题
有n个人参加编程比赛,比赛结束后每个人都得到一个分数;现在所有人排成一圈(第一个和第n个相邻)领取奖品,要求;1:如果某个人的分数比左右的人高,那么奖品数量也要比左右的人多;2:每个人至少得到有个奖品。问最少应该准备多少奖品。输入描述:第一行是整数n,表示测试样例个数每个测试样例的第一行是一个正整数n,表示参加比赛的人数;(0<n<100000)第二行是n个正整数a[i]...原创 2019-03-17 02:35:26 · 1931 阅读 · 3 评论 -
2019/3/16网易笔试题 约瑟夫环 翻牌 充电桩充电
第一题:n个人围成一圈,第一个人从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。最后剩下的人为胜者。指定n和m,求胜利者编号。输入描述:第一行输入n和m,空格隔开,如10,2其实就是约瑟夫环代码:#include<iostream>#include<cstdio>#include<algorithm>#include<ass...原创 2019-03-17 02:08:16 · 685 阅读 · 0 评论 -
两个基础题:用栈实现队列和用队列实现栈
Leetcode 232. Implement Queue using Stacks 用队列实现栈Leetcode225. Implement Stack using Queues 用栈实现队列首先我们得知道队列和栈的性质。队列:先进先出。栈:先进后出。所以怎么用栈实现队列呢怎么实现先进先出呢?用一个栈肯定是无法实现的,因为栈只有一个开口,所以可以使用两个栈。一个作为数据存储,一个...原创 2019-03-14 17:15:33 · 172 阅读 · 0 评论 -
腾讯内推笔试题之_硬币,奇妙的数列,猜拳游戏
第一题:硬币题目:思路:贪心即可,优先使用大面值代码:#include<iostream>#include<cstdio>using namespace std;int main(){ int n,m; while(~scanf("%d %d",&n,&m)) { if(n>=m) return 1; int ans =...原创 2019-03-10 15:14:08 · 423 阅读 · 0 评论 -
腾讯内推笔试题之_气球颜色
题目:解析:其实维护一个m个大小的数组即可,然后每次进入的时候判断一下是否为一种新的颜色,如果是则总的颜色总数color_sum++,当达到m的时候去的最小坐标和最大坐标,随机更新答案,如果不是新的颜色,就用当前颜色的下标去覆盖即可,有种贪心的思想在里面,时间复杂度O(n*m)代码:#include<cstdio>#include<map>#include<...原创 2019-03-10 14:48:25 · 666 阅读 · 0 评论 -
前序遍历(或后序遍历)和中序遍历还原二叉树
前序遍历+中序遍历构建二叉树1、前序遍历的第一个元素A,为根结点2、在中序遍历找到A,则在中序遍历中A左边是左子树,右边是右子树3、递归(左右子树同样如此)具体实现/* * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeN...原创 2019-03-08 20:29:24 · 422 阅读 · 0 评论 -
Leetcode 994. Rotting Oranges
Rotting OrangesIn a given grid, each cell can have one of three values:the value 0 representing an empty cell;the value 1 representing a fresh orange;the value 2 representing a rotten orange.Ev...原创 2019-02-17 20:41:30 · 846 阅读 · 0 评论 -
Leetcode 993. Cousins in Binary Tree
Leetcode 993. Cousins in Binary TreeIn a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.Two nodes of a binary tree are cousins if they have the same dep...原创 2019-02-17 20:29:13 · 531 阅读 · 0 评论 -
11月29日 剑指offer 平衡二叉树 && Leetcode
题目描述输入一棵二叉树,判断该二叉树是否是平衡二叉树。解析:首先得明白平衡二叉树的定义,就是左右子树高度差的绝对值不超过1。所以用递归可以解决,对二叉树进行左右遍历,然后得到左右子树的高度,判断绝对值的值是否大于1,然后一个节点的高度取决于return max(left_depth, right_depth);代码:int tryTree(TreeNode* curNode, int ...原创 2018-12-02 21:24:31 · 773 阅读 · 0 评论 -
11月27日 剑指offer 数字在排序数组中出现的次数 && Leetcode Valid Parentheses
题目描述统计一个数字在排序数组中出现的次数。解析:其实就是实现下lower_bound 和 upper_bound 。我们只要找到第一个出现的k然后找到最后一个出现的k即可。我们拿找到第一个k举例子。因为是有序的,所以肯定使用二分。那么首先得到mid值。然后分三种情况1)如果data[mid] == k ,那么里面分2种情况,(1)如果(mid>0&&da...原创 2018-11-27 23:10:09 · 238 阅读 · 0 评论 -
11月26日 每日一题 leetcode 8. String to Integer (atoi) && 剑指offer 两个链表的第一个公共结点
String to Integer (atoi) 题目:Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is fo...原创 2018-11-26 21:03:22 · 148 阅读 · 0 评论 -
剑指offer&&Leetcode 动态规划专题
1:连续子数组的最大和题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个...原创 2018-09-13 22:45:43 · 237 阅读 · 0 评论 -
剑指offer&&Leetcode 数据结构专题
最小的K个数题目描述 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。法1:每次选一个最小的数,然后和前面的交换,那么时间复杂度就是O(K*N)代码:class Solution {public: vector<int> GetLeastNumbers_Solution(vector&...原创 2018-09-12 21:34:56 · 489 阅读 · 0 评论 -
剑指offer 字符串的排列
题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 输入描述:输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。解析:这是一个字符串的全排列,所以使用回溯法解决。代码:#include "stdafx.h"#includ...原创 2018-09-11 22:24:10 · 198 阅读 · 0 评论 -
美团面试题 区间统计
文巨做的,思路很好解析:这道题可以直接枚举区间,然后从一个区间[L,R]到[L+1,R+1]的时候,把a[L]的个数剪1,a[r+1]的个数加1,如果sum[a[L]]变为t-1,那么说明a[L]是那个满足区间内大于等于t的,所以总数-1,如果新加入的a[R+1]的sum等于t,说明是新加入的,那么总数+1。代码:#include&lt;cstdio&gt;#include&...原创 2018-09-07 10:41:49 · 294 阅读 · 0 评论 -
拼多多笔试题 找字典序最小的不同的字符串
解析看代码注释:#include<iostream>#include<algorithm>#include<vector>#include<unordered_set>#include<string>using namespace std;bool permutation(vector<string>&...原创 2018-08-31 16:23:39 · 1060 阅读 · 0 评论 -
拼多多笔试题 循环小数的开始位置和循环节
题目: 给出被除数和除数,求出循环小数的开始位置(小数点之后的位数)和循环长度。输入: 第一行包含两个数字分别是被除数a和除数b (1<= a, b <= 1000000)输出:输出一行,包含一个两个数字,分别表示循环小数的开始位置和循环体的长度(无循环则开始位置为结束位置,长度为0)解析:模拟长除法的计算过程。①mod = a%b;②小数 = (mod...原创 2018-08-31 13:01:12 · 1293 阅读 · 0 评论 -
拼多多笔试题 回合制角色扮演
题目:你在玩一个回合制 角色扮演的游戏。现在你在准备一个策略,以便在最短的回合内击败敌方角色。在战斗开始时,敌人拥有HP格血量。当血量小于等于0时,敌人死去。一个缺乏经验的玩家可能简单地尝试每个回合都攻击。但是你知道辅助技能的重要性。在你的每个回合开始时你可以选择以下两个动作之一:聚力或者攻击聚力会提高你下个回合攻击的伤害。攻击会对敌人造成一定量的伤害。如果你上个回合使用了聚力,...原创 2018-08-31 12:07:40 · 711 阅读 · 0 评论 -
剑指offer&&Leetcode 之 二进制系列
1:二进制中1的个数题目:输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。思路:这个题目考查二进制的思想,有一个小诀窍,当一个数n-1时候,它的二进制和n的二进制表示,就是从后往前数,第一个1变成0,然后这个1后面的0全部变成1,然后n&amp;(n-1)我们就会发现n的从后往前数第一个1变成了0。例如:n为101100 那么n-1为101011 那么n&amp;n-...原创 2018-08-28 09:08:32 · 245 阅读 · 0 评论 -
Leetcode_31 Next Permutation
题目:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possi...原创 2018-08-03 20:56:15 · 149 阅读 · 0 评论 -
Leetcode_30 Substring with Concatenation of All Words
题目:You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once a...原创 2018-07-27 17:41:26 · 91 阅读 · 0 评论 -
Leetcode_28. Implement strStr()
题目: Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = “hello”, needle = “ll” Output: 2Exa...原创 2018-07-27 16:16:21 · 159 阅读 · 0 评论 -
Leetcode_18 4Sum
题目: Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum o...原创 2018-07-27 10:05:09 · 131 阅读 · 0 评论
分享