![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
刷题
Nick-Hwong
机器学习 深度学习 图像处理
展开
-
leetcode 14. 最长公共前缀
题目:编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 “”。示例 1:输入: [“flower”,“flow”,“flight”]输出: “fl”示例 2:输入: [“dog”,“racecar”,“car”]输出: “”解释: 输入不存在公共前缀。来源:力扣(LeetCode)链接:https://leetcode-cn.com/prob...原创 2019-09-03 16:08:05 · 148 阅读 · 0 评论 -
刷题:序列化二叉树
题目:请实现两个函数,分别用来序列化和反序列化二叉树idea先序遍历,采用带引用的idx记录当前序列下标当结点为空时,用’#'标记,并返回由于char为一个字节,而int为8个字节。当结点非空时用四个单位来保存int数值,分别右移24、16、8、0位然后跟255进行与运算。反序列化时则将这四个单位分别左移然后跟255与运算,最后或起来。code/*struct TreeN...原创 2019-04-24 19:56:10 · 166 阅读 · 0 评论 -
刷题:不用加减法做加法
题目:写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。idea:首先先去思考加法怎么算。先弄出进位,与操作以后得出两个数都是1的位,这些位需要进位因此将与结果左移1位,将移位后的进位(num1&num2)<<1再跟两个数中那些只有一个数是1的位(异或操作)进行相加(递归),当进位全为0的时候就递归结束。codeclass Solution...原创 2019-04-20 10:08:58 · 294 阅读 · 0 评论 -
刷题:把字符串转成整数
题目:将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。idea:首先判断首尾字符,首先根据首位字符决定符号位。。然后从左到右遍历字符串,如果字符不合法直接返回0,否则ans = ans10+str[i]-‘0’。假如前边都是0会被a...原创 2019-04-20 10:30:21 · 131 阅读 · 0 评论 -
minimum-depth-of-binary-tree
题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.idea:层次遍历,找到第一个叶子节点时返回当前深度。code#in...原创 2019-04-29 14:31:50 · 83 阅读 · 0 评论 -
evaluate reverse-polish notation
题目:Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are+,-,,/. Each operand may be an integer or another expression.Some examples:[“2”, “1”, “+”, “3”, ""]...原创 2019-04-29 15:19:15 · 96 阅读 · 0 评论 -
刷题:二叉搜索树的第k个结点
题目:给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。idea:使用中序遍历,每遍历一个结点k–,假如k==0则将ans改成该结点。code/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *...原创 2019-04-24 20:51:40 · 111 阅读 · 0 评论 -
数据流中的中位数
题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。idea:使用最大堆保存前半数据,最小堆保存后半段数据。保证最大堆size>=最小堆size。...原创 2019-04-24 21:57:15 · 172 阅读 · 0 评论 -
滑动窗口的最大值(max pooling的实现)
题目:给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,...原创 2019-04-25 15:58:08 · 1900 阅读 · 0 评论 -
刷题:孩子们的游戏(圆圈中最后剩下的数)
题目:每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数…这样下去…直到剩下最后一个...原创 2019-04-19 22:23:46 · 82 阅读 · 0 评论 -
刷题:求1+2+3+...+n
题目:求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。idea:利用A&&B假如A为假则B不执行的特点codeclass Solution {public: int Sum_Solution(int n) { n&&(n+=Sum_Solut...原创 2019-04-19 21:11:51 · 284 阅读 · 0 评论 -
刷题:数字在排序数组中出现的次数
题目:统计一个数字在排序数组中出现的次数。解法一idea:找出第一个等于k的位置,如果不存在k则返回0。注意临界点(low<=high而不是low<high)。找到k以后在(0~index(而不是0~index-1))找出第一个等于k但是前一个数不等于k的下标,右边同理。codeclass Solution {public: int GetNumberOfK(...原创 2019-04-19 10:19:42 · 99 阅读 · 0 评论 -
刷题:二叉树的深度
题目:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。idea:递归,左右两颗子树中深度最深的+1即可(或者使用层次遍历)codeclass Solution {public: int TreeDepth(TreeNode* p) { if (!p) return 0; ...原创 2019-04-19 11:01:28 · 176 阅读 · 0 评论 -
刷题:平衡二叉树
题目:输入一棵二叉树,判断该二叉树是否是平衡二叉树。idea:假如左右两颗子树深度之差大于1则代表不平衡,递归求深度,假如求解过程中发现某个子树不平衡,则将flag赋值为false(flag初始化为true)。在求深度时首先判定flag假如为false则返回-1code#include <bits/stdc++.h>class Solution {public: ...原创 2019-04-19 11:44:44 · 123 阅读 · 0 评论 -
刷题:数组中只出现一次的数字
题目:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。idea:异或具有交换律,结合律。因此一个数组中假如只有一个数a出现一次时其他数都出现两次时,异或会得到a。可以试想一下按二进制展开所有数,所有单独对第一位进行异或(设a第一位为1),那么第一位全部异或肯定得1(其他数都出现两次,那么必然有2n+1(1是a)个数出现1, 2m个数出现0)。其他位亦...原创 2019-04-19 15:09:04 · 89 阅读 · 0 评论 -
刷题:和为s的连续正数序列
题目:小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!idea:转化为动态规划问题。每次求解以数i为...原创 2019-04-19 16:04:23 · 149 阅读 · 0 评论 -
刷题:和为S的两个数字
题目:输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。idea:乘积最小的必然是最左和最右的那一对。因此令high指向最后一个,low指向第一个。当low和high的和为sum时直接返回,否则若和大于sum则high–,和小于sum则low++。codeclass Solution {public: ...原创 2019-04-19 16:49:18 · 86 阅读 · 0 评论 -
刷题:左翻转字符串
题目:汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!idea:如果len>n,则令n=n%len。首先翻转整个字符串,然后分别翻转前len-n个字...原创 2019-04-19 19:06:47 · 87 阅读 · 0 评论 -
刷题:扑克牌顺子
题目:LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌...原创 2019-04-19 20:04:21 · 133 阅读 · 0 评论 -
矩阵中的路径
题目:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。 例如 a b c e s f c s a d e e 这样的3 X 4 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串...原创 2019-04-25 17:55:10 · 84 阅读 · 0 评论 -
max-points-on-a-line
题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.idea两点确定一条直线,使用斜率来标记一条直线(以及当前遍历的点)特殊情况:两个点重复时,该点的任意一条直线上的点都应该+1。因此用一个变量dup来保存于当前点重复的点当直接垂...原创 2019-05-27 20:21:57 · 99 阅读 · 0 评论 -
single-number-ii
题目:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without u...原创 2019-08-06 15:08:25 · 153 阅读 · 0 评论 -
candy
题目:There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least on...原创 2019-08-06 22:38:57 · 111 阅读 · 0 评论 -
gas-station
题目:There are N gas stations along a circular route, where the amount of gas at station i isgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from station i to it...原创 2019-08-09 16:20:40 · 137 阅读 · 0 评论 -
palindrome-partitioning-ii
题目: 给出一个字符串s,分割s使得分割出的每一个子串都是回文串 计算将字符串s分割成回文分割结果的最小切割数 例如:给定字符串s="aab", 返回1,因为回文分割结果["aa","b"]是切割一次生成的。 Given a string s, partition s such that every substring of the partition is a pa...原创 2019-08-14 20:10:47 · 141 阅读 · 0 评论 -
palindrome-partitioning
题目:给定一个字符串s,分割s使得s的每一个子串都是回文串返回所有的回文分割结果例如:给定字符串s=“aab”,返回[↵ [“aa”,“b”],↵ [“a”,“a”,“b”]↵ ]Given a string s, partition s such that every substring of the partition is a palindrome.Retur...原创 2019-08-14 22:50:54 · 131 阅读 · 0 评论 -
longest-consecutive-sequence
题目:给定一个无序的整数类型数组,求最长的连续元素序列的长度。例如:给出的数组为[100, 4, 200, 1, 3, 2],最长的连续元素序列为[1, 2, 3, 4]. 返回这个序列的长度:4你需要给出时间复杂度在O(n)之内的算法 Given an unsorted array of integers, find the length of the longest cons...原创 2019-08-15 17:35:09 · 194 阅读 · 0 评论 -
无重复字符的最长子串
题目给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: “abcabcbb”输出: 3解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。示例 2:输入: “bbbbb”输出: 1解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。示例 3:输入: “pwwkew”输出: 3解释: 因为无重复字符的最长子串是 “w...原创 2019-08-30 13:09:43 · 206 阅读 · 0 评论 -
最长回文子串
题目给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。示例 1:输入: “babad”输出: “bab”注意: “aba” 也是一个有效答案。示例 2:输入: “cbbd”输出: “bb”来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-palindromic-subs...原创 2019-08-30 14:25:52 · 236 阅读 · 0 评论 -
word-break
题目:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens =“leetcode”,dict =[“leet”,...原创 2019-08-05 20:51:47 · 130 阅读 · 0 评论 -
最大乘积
题目:给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大,要求时间复杂度:O(n),空间复杂度:O(1)输入描述:无序整数数组A[n]输出描述:满足条件的最大乘积示例1输入3 4 1 2输出24idea:由于数组中包含整数、负数和0,那么最大的乘积有两种可能:(1)最大的三个正数 (2)最大的一个正数、最小的两个负数冒泡分别求出最大的三个正...原创 2019-07-22 20:13:55 · 251 阅读 · 0 评论 -
sort-list
题目:sort a linked list in O(n log n) time using constant space complexity.idea:使用自底向上来归并排序链表,首先将左右两个待合并的链表分别cut切开,即将左右两个链表的结尾节点的next设置为NULL每次合并完两个链表以后将上一个合并完的链表的尾部的next指向当前合并完的链表code:/** * ...原创 2019-07-09 14:32:46 · 132 阅读 · 0 评论 -
linked-list-cycle
题目:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?idea:使用快慢指针,假如快指针追上慢指针fast==slow,则存在cycle。如果fast为空则不存在cycle。code/** * Defin...原创 2019-07-14 21:33:25 · 82 阅读 · 0 评论 -
insert-sort-list
题目:Sort a linked list using insertion sort.idea:直接新建一个链表,遍历原链表每一个结点p,从头开始遍历新链表,当p的值小于等于new_p(新链表指针)的值时,将prev的next指向p,p指向new_p。code/** * Definition for singly-linked list. * struct ListNode { *...原创 2019-07-10 20:01:17 · 98 阅读 · 0 评论 -
binary-tree-postorder-traversal(二叉树后序遍历)
题目:Given a binary tree, return the postorder traversal of its nodes’ values.For example:Given binary tree{1,#,2,3},12/3return[3,2,1].Note: Recursive solution is trivial, could you do it ite...原创 2019-07-10 20:33:35 · 128 阅读 · 0 评论 -
binary-tree-preorder-traversal
题目:Given a binary tree, return the preorder traversal of its nodes’ values.For example:Given binary tree{1,#,2,3},12/3return[1,2,3].Note: Recursive solution is trivial, could you do it iter...原创 2019-07-10 20:46:09 · 110 阅读 · 0 评论 -
reorder-list
题目:Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…You must do this in-place without altering the nodes’ values.For example,Given{1,2,3,4}, reorder...原创 2019-07-10 21:32:48 · 80 阅读 · 0 评论 -
linked-list-cycle-ii
题目:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?idea:使用快慢指针,快指针每次走两步,慢指针每次走一步假设慢指针走了(...原创 2019-07-11 20:50:54 · 162 阅读 · 0 评论 -
word-break-ii
题目:Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens =“cat...原创 2019-07-16 22:21:16 · 168 阅读 · 0 评论 -
刷题:两个链表的第一个公共结点
题目:输入两个链表,找出它们的第一个公共结点。idea:因为假如两个链表有一个公共结点,代表他们在第一个公共结点以后都是重复的,因此只要计算出两个链表的长度之差,先让长的链表追上短的就可以。code/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL)...原创 2019-04-18 20:54:18 · 112 阅读 · 0 评论