刷题系列(LeetCode、牛客等)
想提高一下算法设计和C++编程的能力,因此在空闲时间刷一刷LeetCode题目,并分享给大家,共同交流进步
Efan_w
雄关漫道真如铁,而今迈步从头越
展开
-
LeetCode 115. Distinct Subsequences 递归法(回溯法)/动态规划 int-unsigned int
题目Given a string S and a string T, count the number of distinct subsequences of S which equals T.A subsequence of a string is a new string which is formed from the original string by deleting some (...原创 2019-09-05 11:47:18 · 173 阅读 · 0 评论 -
LeetCode703. Kth Largest Element in a Stream(优先队列)
题目描述:Design a class to findthekth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.YourKthLargestclass will have a constru...原创 2019-08-03 17:31:40 · 141 阅读 · 0 评论 -
LeetCode784. Letter Case Permutation && 回溯法总结 && 记录调试经验输出中的空格问题
LeetCode784. Letter Case Permutation && 记录调试经验:cout << ' '中使用多个空格输出数值题目说明思路子集树排序树调试记录回溯法参考:题目说明Given a string S, we can transform every letter individually to be lowercase or uppercase...原创 2019-09-01 11:10:42 · 234 阅读 · 0 评论 -
POJ2318 Toys(计算几何,C++, 叉积判断线段与点的位置关系,二分法)
目录题目描述:算法实现判断点(toys)和线段(partitions)的位置关系:优化具体程序:题目描述:出自ACMToyDescriptionCalculate the number of toys that land in each bin of a partitioned toy box.Mom and dad have a problem - th...原创 2018-10-18 10:56:24 · 378 阅读 · 0 评论 -
剑指offer:丑数
题目:把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。思路:我们可以依次写出丑数1,2,3,4,5,6,8,9,10,12,15,16,18,20......仔细观察我们发现因为丑数只包含2,3,5三个因子,因此“下一个丑数”可以由前面的某一个丑数乘...原创 2019-05-14 19:48:09 · 210 阅读 · 1 评论 -
剑指offer面试题之字符串的排列
题目:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。算法:采用递归的方式,将传入的数组在当前位置和其后的所有元素依次交换位置,交换后再依次递归后面的位置。对可能出现的重复元素则不再交换代码:#include <iostream>#inc...原创 2019-05-09 20:02:27 · 131 阅读 · 0 评论 -
剑指offer:二叉搜索树与双向链表
题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向代码:思路采用递归的方式实现(类似树的中序遍历),定义一个已经调整好的链表的左端节点指针和右端节点指针#include "pch.h"#include <iostream>#include <sstream>#include &...原创 2019-05-07 21:39:29 · 147 阅读 · 0 评论 -
剑指offer面试题之复杂链表的复制
目录题目:解决方案:方法一:方式二:题目:输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空解决方案:方法一:思路是先复制链表的正常部分(不复制random部分链接),完成正常部分的复制后再复制r...原创 2019-05-07 12:01:50 · 193 阅读 · 0 评论 -
LeetCode算法系列:100.Same Tree
题目描述:Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.E...原创 2018-10-10 11:46:24 · 157 阅读 · 0 评论 -
LeetCode算法系列:99.Recover Binary Search Tree
题目描述:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Example 1:Input: [1,3,null,null,2] 1 / 3 \ 2Output: [3,1,n...原创 2018-10-10 11:44:13 · 141 阅读 · 0 评论 -
LeetCode算法系列:98.Interleaving String
题目描述:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key...原创 2018-10-10 11:35:01 · 145 阅读 · 0 评论 -
LeetCode算法系列:97.Interleaving String
题目描述:Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.Example 1:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"Output: trueExample 2:Input: s1 = "aabcc", ...原创 2018-10-10 11:32:40 · 256 阅读 · 0 评论 -
LeetCode算法系列:95 && 96 Unique Binary Search Trees I && II
题目描述:96.Unique Binary Search TreesGiven n, how many structurally unique BST's (binary search trees) that store values 1 ... n?Example:Input: 3Output: 5Explanation:Given n = 3, there are a ...原创 2018-10-09 21:20:30 · 161 阅读 · 0 评论 -
LeetCode算法系列:94.Binary Tree Inorder Traversal
题目描述:Given a binary tree, return the inorder traversal of its nodes' values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [1,3,2]Follow up: Recursive solution is trivial, ...原创 2018-10-09 21:10:41 · 118 阅读 · 0 评论 -
LeetCode算法系列:93.Restore IP Addresses
题目描述Given a string containing only digits, restore it by returning all possible valid IP address combinations.Example:Input: "25525511135"Output: ["255.255.11.135", "255.255.111.35"]算法实现这个...原创 2018-10-09 20:54:17 · 168 阅读 · 0 评论 -
LeetCode算法系列:92&&206 Reverse Linked List
题目描述:92Reverse a linked list from position m to n. Do it in one-pass.Note: 1 ≤ m ≤ n ≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4Output: 1->4->3-&...原创 2018-10-09 20:50:07 · 139 阅读 · 0 评论 -
LeetCode算法系列:91.Decode Ways
题目描述:A 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 containing only digits, dete...原创 2018-10-09 20:42:11 · 217 阅读 · 0 评论 -
LeetCode算法系列:90.Subsets
题目描述:Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: [...原创 2018-10-09 20:34:32 · 145 阅读 · 0 评论 -
LeetCode算法系列:89.Gray Code
题目描述:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequenc...原创 2018-10-09 20:29:50 · 189 阅读 · 0 评论 -
LeetCode算法系列:88.Merge Sorted Array
题目描述:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and nrespectively. You may assu...原创 2018-10-09 20:19:30 · 115 阅读 · 0 评论 -
LeetCode算法系列:87.Scramble String
题目描述:Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \...原创 2018-10-08 11:52:49 · 178 阅读 · 0 评论 -
LeetCode算法系列:86.Partition List
题目描述Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in eac...原创 2018-10-08 11:47:11 · 170 阅读 · 0 评论 -
LeetCode算法系列:85. Maximal Rectangle
题目描述:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.Example:Input:[ ["1","0","1","0","0"], ["1","0",&quo原创 2018-09-30 18:12:13 · 541 阅读 · 0 评论 -
LeetCode算法系列:84. Largest Rectangle in Histogram
题目描述The largest rectangle is shown in the shaded area, which has area = 10 unit. Example:Input: [2,1,5,6,2,3]Output: 10算法实现:关键在于思路,具体实现并不难。首先考虑到如果是一个顺序排列,问题会比较简单,如2,3,4,5只需要比较2*4,3*3,...原创 2018-09-29 18:05:01 · 102 阅读 · 0 评论 -
LeetCode算法系列:83. Remove Duplicates from Sorted List
题目描述:Given a sorted linked list, delete all duplicates such that each element appear only once.Example 1:Input: 1->1->2Output: 1->2Example 2:Input: 1->1->2->3->3Outp...原创 2018-09-29 10:47:30 · 118 阅读 · 0 评论 -
LeetCode算法系列:82. Remove Duplicates from Sorted List II
题目描述:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Example 1:Input: 1->2->3->3->4->4->5Output...原创 2018-09-29 10:43:32 · 125 阅读 · 0 评论 -
LeetCode算法系列:81. Search in Rotated Sorted Array II
题目描述:Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).You are given a target value to search. If...原创 2018-09-28 17:58:31 · 119 阅读 · 0 评论 -
LeetCode算法系列: 80. Remove Duplicates from Sorted Array II
题目描述:Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.Do not allocate extra space for another array, you must do this...原创 2018-09-28 16:52:51 · 137 阅读 · 0 评论 -
LeetCode算法系列:79. Word Search
题目描述:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or verti...原创 2018-09-28 16:48:32 · 244 阅读 · 0 评论 -
LeetCode算法系列:78. Subsets
题目描述:Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[ [3...原创 2018-09-28 16:41:28 · 115 阅读 · 0 评论 -
LeetCode算法系列:77. Combinations
题目描述:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.Example:Input: n = 4, k = 2Output:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]算法实现...原创 2018-09-28 16:37:59 · 291 阅读 · 0 评论 -
LeetCode算法系列:76. Minimum Window Substring
题目描述:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).Example:Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"N...原创 2018-09-28 16:32:43 · 174 阅读 · 0 评论 -
LeetCode算法系列:75. Sort Colors
题目描述:Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use...原创 2018-09-28 16:09:09 · 113 阅读 · 0 评论 -
LeetCode算法系列:74. Search a 2D Matrix
题目描述:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of e...原创 2018-09-19 20:06:33 · 114 阅读 · 0 评论 -
LeetCode算法系列:73. Set Matrix Zeroes
题目描述:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1...原创 2018-09-19 20:12:12 · 144 阅读 · 0 评论 -
LeetCode算法系列:72. Edit Distance
题目描述;Given 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:Insert a character Delete ...原创 2018-09-19 12:01:45 · 132 阅读 · 0 评论 -
LeetCode算法系列:71. Simplify Path
目录 题目描述算法实现关于字符串分割题目描述Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"path = "/a/../../b/../c//...原创 2018-09-19 09:50:00 · 159 阅读 · 0 评论 -
LeetCode算法系列:70. Climbing Stairs
题目描述You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a po...原创 2018-09-18 09:25:56 · 117 阅读 · 0 评论 -
LeetCode算法系列:69. Sqrt(x)
目录 题目描述:算法实现他山之石:题目描述:Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the dec...原创 2018-09-18 09:16:18 · 124 阅读 · 0 评论 -
LeetCode算法系列:68. Text Justification
题目描述: Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.You should pack your words in a gre...原创 2018-09-17 21:27:15 · 153 阅读 · 0 评论