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 · 268 阅读 · 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 · 127 阅读 · 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 · 81 阅读 · 0 评论 -
2019/3/16 字节跳动笔试题
有n个人参加编程比赛,比赛结束后每个人都得到一个分数;现在所有人排成一圈(第一个和第n个相邻)领取奖品,要求;1:如果某个人的分数比左右的人高,那么奖品数量也要比左右的人多;2:每个人至少得到有个奖品。问最少应该准备多少奖品。输入描述:第一行是整数n,表示测试样例个数每个测试样例的第一行是一个正整数n,表示参加比赛的人数;(0<n<100000)第二行是n个正整数a[i]...原创 2019-03-17 02:35:26 · 1853 阅读 · 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 · 604 阅读 · 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 · 367 阅读 · 0 评论 -
腾讯内推笔试题之_气球颜色
题目:解析:其实维护一个m个大小的数组即可,然后每次进入的时候判断一下是否为一种新的颜色,如果是则总的颜色总数color_sum++,当达到m的时候去的最小坐标和最大坐标,随机更新答案,如果不是新的颜色,就用当前颜色的下标去覆盖即可,有种贪心的思想在里面,时间复杂度O(n*m)代码:#include<cstdio>#include<map>#include<...原创 2019-03-10 14:48:25 · 612 阅读 · 0 评论 -
两个基础题:用栈实现队列和用队列实现栈
Leetcode 232. Implement Queue using Stacks 用队列实现栈Leetcode225. Implement Stack using Queues 用栈实现队列首先我们得知道队列和栈的性质。队列:先进先出。栈:先进后出。所以怎么用栈实现队列呢怎么实现先进先出呢?用一个栈肯定是无法实现的,因为栈只有一个开口,所以可以使用两个栈。一个作为数据存储,一个...原创 2019-03-14 17:15:33 · 140 阅读 · 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 · 380 阅读 · 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 · 770 阅读 · 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 · 452 阅读 · 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 · 206 阅读 · 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 · 116 阅读 · 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 · 103 阅读 · 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 · 122 阅读 · 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 · 776 阅读 · 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 · 131 阅读 · 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 · 179 阅读 · 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 · 155 阅读 · 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 · 141 阅读 · 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 · 124 阅读 · 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 · 317 阅读 · 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 · 225 阅读 · 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 · 172 阅读 · 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 · 136 阅读 · 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 · 186 阅读 · 0 评论 -
11月29日 剑指offer 平衡二叉树 && Leetcode
题目描述输入一棵二叉树,判断该二叉树是否是平衡二叉树。解析:首先得明白平衡二叉树的定义,就是左右子树高度差的绝对值不超过1。所以用递归可以解决,对二叉树进行左右遍历,然后得到左右子树的高度,判断绝对值的值是否大于1,然后一个节点的高度取决于return max(left_depth, right_depth);代码:int tryTree(TreeNode* curNode, int ...原创 2018-12-02 21:24:31 · 729 阅读 · 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 · 123 阅读 · 0 评论 -
剑指offer&&Leetcode 动态规划专题
1:连续子数组的最大和题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个...原创 2018-09-13 22:45:43 · 214 阅读 · 0 评论 -
Leetcode_16. 3Sum Closest
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would ...原创 2018-06-10 16:28:42 · 105 阅读 · 0 评论 -
Leetcode_14 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”.Example 1:Input: [“flower”,”flow”,”flight”] Output:...原创 2018-06-10 15:24:00 · 95 阅读 · 0 评论 -
Leetcode_15 3Sum
题目:Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not...原创 2018-06-10 15:07:31 · 82 阅读 · 0 评论 -
Leetcode_11 Container With Most Water
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two li...原创 2018-05-22 22:04:21 · 109 阅读 · 0 评论 -
Leetcode_6 ZigZag Conversion
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N A P L S I I...原创 2018-05-16 13:11:15 · 102 阅读 · 0 评论 -
Leetcode42 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by...原创 2018-05-25 23:18:24 · 150 阅读 · 0 评论 -
Leetcode_5 solve Longest Palindromic Substring
题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: “babad” Output: “bab” Note: “aba” is also a valid answer....原创 2018-05-14 22:04:40 · 94 阅读 · 0 评论 -
Leetcode_10 Regular Expression Matching
题目: Given an input string (s) and a pattern (p), implement regular expression matching with support for ‘.’ and ‘*’.‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding elem...原创 2018-05-19 23:02:13 · 127 阅读 · 0 评论 -
Leetcode_4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:nums1 = [1, 3]...原创 2018-05-13 21:59:29 · 334 阅读 · 0 评论 -
Leetcode_3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters.Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”,...原创 2018-05-13 20:54:28 · 71 阅读 · 0 评论 -
Leetcode_234. Palindrome Linked List
题目: Given a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2 Output: falseExample 2:Input: 1->2->2->1 Output: trueFollow up: Could you do it in O(n) ...原创 2018-05-18 23:29:42 · 116 阅读 · 0 评论