![](https://img-blog.csdnimg.cn/20190908115156502.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
编程题
Bob__yuan
这个作者很懒,什么都没留下…
展开
-
LeetCode - 138. Copy List with Random Pointer 随机节点链表复制
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list. 节点结构如下:class Node {public: ...原创 2020-01-30 10:29:43 · 217 阅读 · 0 评论 -
单词翻转
一、字符串按照单词顺序翻转 LeetCode - 151. Reverse Words in a StringGiven an input string, reverse the string word by word.Input: “the sky is blue”Output: “blue is sky the” Example 2:Input: " hello world! ...原创 2019-11-17 12:29:57 · 430 阅读 · 0 评论 -
【二分+贪心】【最小值最大化】牛牛分田地
牛客网 https://www.nowcoder.com/questionTerminal/fe30a13b5fb84b339cb6cb3f70dca699牛牛分田地牛牛和 15 个朋友来玩打土豪分田地的游戏,牛牛决定让你来分田地,地主的田地可以看成是一个矩形,每个位置有一个价值。分割田地的方法是横竖各切三刀,分成 16 份,作为领导干部,牛牛总是会选择其中总价值最小的一份田地, 作为牛...原创 2019-11-03 16:08:47 · 483 阅读 · 0 评论 -
牛客网 - 剑指offer - “树的子结构”
题目描述输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)bool isSubTree(TreeNode* p1, TreeNode* p2){ if(!p2) return true; // p2已经遍历到底了还是对的,这部分就是对的 if(p1 && p2 && p1->val ==...原创 2018-12-05 10:54:41 · 196 阅读 · 0 评论 -
找同学(只能转弯两次的"DFS")
#include <iostream>#include <vector>#include <algorithm>using namespace std;/*3 44 3 2 10 0 0 01 2 3 441 1 2 43 4 1 12 4 2 13 3 1 10 0*/vector<vector<int>&...原创 2019-11-21 15:55:14 · 154 阅读 · 0 评论 -
topK问题
topK 问题基本有以下几种:数组前 K 大的 k 和数字数组第 K 大的数字(一个数)前k大的k个数 第k大的数 使用快排思想,参考 https://blog.csdn.net/Bob__yuan/article/details/100145469。...原创 2019-10-28 23:51:12 · 290 阅读 · 0 评论 -
【DP】LeetCode - House Robber 问题
这个系列共三道题,前两天类似,是一维数组dp问题,第三题是树的遍历:198. House Robber213. House Robber II337. House Robber IIILeetCode - 198. House Robber 给定一个非负整数的数组nums,表示每个房子的价值,小偷偷房子里的东西,不能偷两个相邻的房子,问最多投多少?Input: [1,2,3...原创 2019-10-27 23:38:23 · 279 阅读 · 0 评论 -
雀魂启动!
https://www.nowcoder.com/practice/448127caa21e462f9c9755589a8f2416?tpId=0&tqId=0&rp=10&ru=/ta/2019test&qru=/ta/2019test/question-ranking雀魂启动! 暴力对每一个出现次数 >= 2 的数作为雀头进行尝试,然后剩下的 12 ...原创 2019-10-25 17:09:48 · 2324 阅读 · 0 评论 -
数组中每个位置右边第一个比它大的数
给定一个数组,O(N) 时间复杂度求出原创 2019-10-24 09:44:49 · 1097 阅读 · 0 评论 -
数组逆序对个数
牛客网 “数组中的逆序对” 题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出(即输出P%1000000007 )。输入描述: 题目保证输入的数组中没有的相同的数字对于 %50 的数据, size<=104对于 %75 的数据, size<=105对于...原创 2019-10-21 20:47:12 · 425 阅读 · 0 评论 -
【DP】LeetCode - 1218. 最长等差数列子数组
给一个已排序数组,问最长的等差数列子数组多长。这道题和 LIS 基本一模一样,LIS 可以参考 https://blog.csdn.net/Bob__yuan/article/details/99696806。样例输入:51 2 3 4 5样例输出: 5 用一个二维 dp 矩阵,dp[i][d] 表示以 i 位置为结尾的,差为 d 的等差数列的长度。#include <...原创 2019-09-16 23:20:51 · 426 阅读 · 0 评论 -
【DP】【LCS】最长公共子序列及其应用
Given two strings text1 and text2, return the length of their longest common subsequence.A subsequence of a string is a new string generated from the original string with some characters(can be non...原创 2019-08-17 15:30:57 · 896 阅读 · 0 评论 -
【DP】【LPS】最长回文子序列
Given a string s, find the longest palindromic subsequence’s length in s.You may assume that the maximum length of s is 1000.Example 1:Input: “bbbab”Output: 4One possible longest palindromic sub...原创 2019-08-17 18:42:07 · 225 阅读 · 0 评论 -
【DP】【LIS】最长递增子序列 - O(N^2)方法 + O(NlogN)方法
LeetCode - 300. Longest Increasing SubsequenceGiven an unsorted array of integers, find the length of longest increasing subsequence(LIS).Example:Input: [10,9,2,5,3,7,101,18]Output: 4Explanat...原创 2019-08-17 21:38:57 · 398 阅读 · 0 评论 -
【DP】日本Indeed公司机试题 - Number of Paths
Problem StatementThere is a gridof square cells with H rows and W columns. The celss at the i-th row and the j-th column (1 i H, 1 j W) will be denoted as (i, j).Each cell in this grid is pa...原创 2018-09-17 20:13:09 · 1334 阅读 · 0 评论 -
【DP】Expression Matching
两道 hard 的 DP 题,但是理解了就其实不难:1、LeetCode - 44. Wildcard Matching2、LeetCode - 10. Regular Expression MatchingLeetCode - 44. Wildcard MatchingGiven an input string (\s) and a pattern (\p), implement wi...原创 2019-09-11 15:42:02 · 176 阅读 · 0 评论 -
【DP】【概率】红蓝球
有 n 个红球和 m 个蓝球,A,B,C三个人,轮流取球。若 A 取到红球则 A 胜利(结束),若 B 取到红球则 B 胜利,C只负责取球捣乱,不参与胜负。若最后全部球取完后 A 没有拿到红球,则 B 胜利,求 A 胜利的概率。输入:m, n输出:A 胜利的概率,保留 5 位小数(补零)输入: 3 4 (3 个红球,4 个蓝球)输出 0.62857...原创 2019-09-10 11:17:44 · 944 阅读 · 0 评论 -
【DP】LeetCode - Decode Ways I、II
#### LeetCode - 91. Decode WaysA message containing letters from A-Z is being encoded to numbers using the following mapping:‘A’ -> 1‘B’ -> 2…‘Z’ -> 26Given a non-empty string containi...原创 2019-09-09 20:42:35 · 135 阅读 · 0 评论 -
【DP】【DFS】LeetCode - Word Break I - II、Concatenated Words
LeetCode - 139. Word BreakGiven a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more di...原创 2019-08-30 18:46:11 · 137 阅读 · 0 评论 -
【DP】【编辑距离】LeetCode - 72. Edit Distance
LeetCode - 72. Edit DistanceGiven two words word1 and word2, find the minimum number of operations required to convert word1 to word2.You have the following 3 operations permitted on a word:Inser...原创 2019-09-02 20:53:26 · 1218 阅读 · 0 评论 -
【DP】LeetCode - 120. Triangle、贝壳找房“采木头,锯子斧头”问题
锯子和斧头轮流砍树问题: 第一行输入树的个数 n, 接下来的 n 行,每行分别输入三个数 a、b、c,分别代表用锯子和斧头砍该棵树的时间,以及换工具砍树所需要的时间。现在手上是斧头,问看完这些树,最短需要多长时间。输入:320 40 2010 4 2590 100 5输出:139Explanation:第一棵树用斧头砍(40),第二颗树还用斧头(4),第三棵树,换成锯子(5 + ...原创 2019-08-11 15:03:59 · 358 阅读 · 1 评论 -
【二分+贪心】用 N 根绳子裁剪出 M 根等长绳子
有 N 根绳子,第 i 根绳子的长度为 l[i],现在需要 M 根等长的绳子。你可以对这 N 根绳子进行任意裁剪(不能拼接),请你计算出这 M 根绳子最长的长度。输入描述: 第一行包括两个整数 N, M,含义如题所述(1 <= N, M <= 100000) 第二行包含N个整数,分别对应N根绳子的长度(0 < l[i] < 109)输出描述: 一个数字,...原创 2019-08-29 17:18:14 · 1767 阅读 · 0 评论 -
【二分+贪心】【最大值最小化】公路上建加油站,最小距离最大化
网上有道题目是这样:有一条高速公路,想要建设 m 个加油站,一共有 n 个可以选择建设加油站的地点(n >= m),在这 n 个可选位置中选择 m 个建加油站有很多种方式,每一种方式中,两个相邻加油站之间的距离都有一个最小值,每种情况的最小距离可能不一样,问所有情况中,这个最小距离的最大值是多少?2 <= n, m <= 1000001 <= a[i] <...原创 2019-08-24 11:12:06 · 1757 阅读 · 0 评论 -
二项式定理
二项式定理(Binomial Theorem)就是:(x+y)n(x + y)^n(x+y)n 的展开: (x+y)n=Cn0anb0+Cn1a(n−1)b1+...+Cnn−1a1bn−1+Cnna0bn(x + y)^n = C_n^0 a^n b^0 + C_n^1 a^{(n-1)} b^1 + ... + C_n^{n - 1} a^1 b^{n - 1} + C_n^n a^0 ...原创 2019-10-16 23:28:56 · 1526 阅读 · 0 评论 -
经典划船渡河问题
经典的一帮人,一艘船,渡河问题: n 个人,一艘船,每次过河穿上最多坐两个人,每个人有一个过河时间 a[i],两个人一起坐船,时间为慢的人的时间,过去之后,需要有人带着船回来,才能继续过河,这个时间也需要计算,问最短需要多少时间能够所有人过河(最后一次船不需要回来)?样例输入:51 2 4 5 15样例输出:27 首先能明确的是,如果过河能够两个人一起过,那就两个人一起过...原创 2019-10-16 22:50:06 · 4509 阅读 · 0 评论 -
单链表上的快排
正常数组上的快排很常考,可以参考 https://blog.csdn.net/Bob__yuan/article/details/98782859 以及求数组第K大元素时使用的快排:https://blog.csdn.net/Bob__yuan/article/details/100145469。 在链表上进行排序一般用归并,可以参考 https://blog.csdn.net/Bob__...原创 2019-10-15 12:06:23 · 346 阅读 · 0 评论 -
LeetCode - 234. Palindrome Linked List 判断回文链表
LeetCode - 234. Palindrome Linked ListGiven a singly linked list, determine if it is a palindrome.Input: 1->2 Output: falseInput: 1->2->2->1 Output: trueFollow up: Could you do it i...原创 2019-10-13 14:47:46 · 263 阅读 · 0 评论 -
字符串去除匹配所有括号
每一个字符串,比如 (())()()Abc(DEF)(g)))(hi)((,去除所有匹配括号,也就是变成 AbcDEFg))hi((。 可以从左往右遍历,也可以从右往左遍历。这种左右括号的题目,一般都要用栈,从左往右的方式如下:void erase_braces(string& s) { stack<int> st; for(int i = 0; i < s...原创 2019-10-12 21:09:51 · 1489 阅读 · 0 评论 -
队列实现栈,栈实现队列
很常见的两道题:1、用 queue 实现 stack:LeetCode - 225. Implement Stack using Queues2、用 stack 实现 queue:LeetCode - 232. Implement Queue using Stacks一、Implement Stack using Queues 就是用 queue 来模拟 stack,实现 push、...原创 2019-09-16 10:52:32 · 184 阅读 · 0 评论 -
回旋矩阵中对应位置值
记录一下19-3-17刚做的LH的机试题(远程),共 5 道编程题,从19:00 到 23:00 四个小时。1、结构体排序 第一道题是一道很简单的结构体排序题,直接冒泡就过了2、回旋矩阵中对应位置值 将从0开始的自然数按照以下规则放置在一个无限大的网格中: 1、0 放在(0,0)位置 2、1 放在(1,0)位置 3、后续所有数字围绕已放置的数字逆时针循环(如下图,只展示...原创 2019-09-15 12:58:23 · 127 阅读 · 0 评论 -
LeetCode - 62、63. Unique Paths I - II、64. Minimum Path Sum
本文总结两道矩阵地图中行走的基础题,作为对DP算法的总结。还有一道也是用二维矩阵表示地图,在里边计算路径数的题目是一道找工作的机试题,在我的另一篇博客中有记录,https://blog.csdn.net/Bob__yuan/article/details/82733954。引用一下百度百科里的说法: 任何思想方法都有一定的局限性,超出了特定条件,它就失去了作用。同样,动态规划也...原创 2018-09-18 16:28:52 · 230 阅读 · 0 评论 -
一维数组中最大连续子数组、二维矩阵中最大子矩阵
共两道题,都是不难,还是记录一下:1、在一个一维数组中,找出连续子数组的最大和:LeetCode - 53. Maximum Subarray2、在一个二维矩阵中,找出子矩阵最大和:https://www.51nod.com/Challenge/Problem.html#problemId=1051一、数组子数组最大和Given an integer array nums, find...原创 2019-09-15 11:57:47 · 854 阅读 · 0 评论 -
LeetCode - 468.Validate IP Address
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.IPv4 addresses are canonically represented in dot-decimal notation,which consists of four decima...原创 2019-09-17 09:29:57 · 126 阅读 · 0 评论 -
将数组分成两部分,使得 |sum1 - sum2| 最小. LeetCode - 1049
将一个数组分成两部分,不要求两部分所包含的元素个数相等,要求使得这两个部分的和的差值绝对值最小(|S1 - S2|,S1、S2 是两个数组的和)。比如对于数组 {1,0,1,7,2,4},可以分成 {1,0,1,2,4} 和 {7},使得这两部分的差值最小。 这个问题可以转化为求数组的一个子集,使得这个子集中的元素的和尽可能接近sum/2,其中sum为数组中所有元素的和。这样转换之后这个问...原创 2019-09-17 09:59:01 · 4998 阅读 · 0 评论 -
LeetCode - 236. 二叉树两节点公共祖先
LeetCode - 236. 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:“Th...原创 2019-10-09 14:14:11 · 282 阅读 · 0 评论 -
【旋转数组】LeetCode中旋转数组问题总结
共四道题:1、33. Search in Rotated Sorted Array - 旋转数组中的查找(没有重复元素)2、81. Search in Rotated Sorted Array II - 旋转数组中的查找(有重复元素)3、153. Find Minimum in Rotated Sorted Array - 旋转数组中最小值(没有重复元素)4、154. Find Minim...原创 2019-10-08 19:13:57 · 152 阅读 · 0 评论 -
LeetCode -11. Container With Most Water(最大盛水、经典“哨兵”题)
LeetCode - 11. Container With Most WaterGiven 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 l...原创 2019-10-08 16:59:40 · 143 阅读 · 0 评论 -
LeetCode - Container With Most Water、Largest Rectangle in Histogram - 直方图中面积问题
本文会总结以下两道题目:11 -Container With Most Water - 直方图中盛水问题 (Medium)84- Largest Rectangle in Histogram - 直方图中最大矩形面积问题 (Hard)还有两道题目也是直方图中面积问题,在另一篇中总结,(https://blog.csdn.net/Bob__yuan/article/details/8...原创 2018-09-13 21:45:31 · 283 阅读 · 0 评论 -
LeetCode -166. Fraction to Recurring Decimal 计算除法
LeetCode - 166. Fraction to Recurring DecimalGiven two integers representing the numerator and denominator of afraction, return the fraction in string format.If the fractional part is repeating, e...原创 2019-10-08 11:03:59 · 171 阅读 · 0 评论 -
LeetCode - 1190. Reverse Substrings Between Each Pair of Parentheses
LeetCode - 1190. Reverse Substrings Between Each Pair of ParenthesesYou are given a string s that consists of lower case English lettersand brackets.Reverse the strings in each pair of matching pa...原创 2019-09-26 13:06:52 · 594 阅读 · 0 评论