
leetcode
文章平均质量分 59
晨识草
同样都是碳
展开
-
leetcode 49: anagrams
leetcode-anagrams// leetcode 49: 将由相同字母组成的字符串进行分类// 解题思路:将字符串进行排序,排序完一样的字符串归为一类// 使用map来对字符串进行归类,最后转换为vector二维数组// 本题不要求最后输出的顺序#include <iostream>#include <vector>#include <stri...原创 2019-02-18 15:47:24 · 339 阅读 · 0 评论 -
50-pow(x,n)
类别:递归,二分 难度:medium算法分析如果是暴力进行循环求解的话,会超时,所以需要二分减少计算量。 对n为奇数和偶数的情况分别进行讨论: pow(x, n) = (n % 2 == 0) ? pow(x*x, n/2) : x * pow(x*x, n/2); 需要特别注意的是,因为在进行计算之前先将n转为正数,所以要考虑n = -(2^31)的情况,转为正数以后会出现溢出的情况。代码原创 2017-12-17 16:10:55 · 244 阅读 · 0 评论 -
45-Jump Game II
类别:BFS 难度:hard题目描述代码实现int jump(vector<int>& nums) { int n = nums.size(); if (n < 2) return 0; int level = 0, i = 0, curMax = 0, nextMax = 0; // 节点下标 while (curMax - i + 1 > 0) {原创 2017-12-17 14:02:30 · 208 阅读 · 0 评论 -
55-Jump Game
类别:BFS 难度:medium题目描述代码实现#include <iostream>#include <vector>using namespace std;bool canJump(vector<int>& nums) { int n = nums.size(); if (n < 2) return true; int level = 0, i = 0, curMa原创 2017-12-17 13:59:42 · 159 阅读 · 0 评论 -
32-longest valid parentheses
类别:dynamic programming 难度:hard题目描述算法分析这道题目涉及到括号匹配的问题,第一想法是使用stack来实现,事实上确实可以这样做。 首先需要把字符串中的符号压栈并进行匹配判断 这里不同的时候,压入栈的不是符号而是符号所在的下标,便于后面对连续匹配符号的长度的比较。 对于最后留在栈中的下标,相邻两个下标的差减1就是连续子串的长度,对每个字串的长度进行比较得到最长字原创 2017-12-07 16:18:29 · 136 阅读 · 0 评论 -
46、47-Permutations
难度:medium题目描述46-Permutations I:47-Permutations II:算法分析46: 直接使用函数next_permutation(_vector.begin(), _vector.end()) 47: 在46题的基础上,使用二层循环,对得到的全排列两两进行比较,删除重复的排列代码实现46:// use function next_permutationcla原创 2017-12-15 21:16:47 · 211 阅读 · 0 评论 -
38-Count And Say
类别:string题目描述算法分析1:“1”(有1个1) 2:“11”(表示上一个即第一个有1个1,而当前有2个1) 3:“21”(表示上一个即第二个有2个1,而当前有1个2,1个1) 3:“1211”(表示上一个即第三个有1个2和1个1,而当前有1个1,1个2,2个1) 4:“111221”…… 给定数字n,需要从第一个数推到n,初始化strs[1] = “1”,然后用一个循环计算之后的原创 2017-12-15 20:56:03 · 175 阅读 · 0 评论 -
28-Implement strStr()(匹配字符串问题KMP算法)
类别:string题目描述算法使用暴力求解的算法效率低,出现runtime error,时间复杂度是O(nm) 可以使用string的find函数,但是具体的实现应该参考KMP算法:时间按复杂度是O(n+m): https://www.61mon.com/index.php/archives/183/(里面有详细的KMP算法介绍和代码实现,此处不做赘述)代码实现#include <iostrea原创 2017-12-15 20:20:36 · 270 阅读 · 0 评论 -
279-perfect squares
类别:dynamic programming 难度:medium题目描述Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.For example, given n = 12, return 3 because原创 2017-12-06 22:27:06 · 176 阅读 · 0 评论 -
62-Unique Paths
类别:dynamic programming 难度:medium题目描述https://leetcode.com/problems/unique-paths/description/ 算法分析单纯的只往下走或者是单纯的只往右走,都只有一种可能的走法,所以初始化的时候path[i][0] = path[0][j] = 1 对于每一个位置,走到这里的上一步可能是从上面下来或者是从左边过来,所以pa原创 2017-12-05 21:47:49 · 181 阅读 · 0 评论 -
198-House Robber
类别:dynamic programming 难度:easy题目描述https://leetcode.com/problems/house-robber/description/ You are a professional robber planning to rob houses along a street. Each house has a certain amount of money原创 2017-12-05 20:17:35 · 180 阅读 · 0 评论 -
121-Best Time to Buy and Sell Stock
类别:dynamic programmin 难度:easy题目描述https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/Say you have an array for which the ith element is the price of a given stock on day i.If you原创 2017-12-05 19:36:31 · 145 阅读 · 0 评论 -
14-Longest Common Prefix
类别:string题目描述Write a function to find the longest common prefix string amongst an array of strings.算法分析两层for循环判断两两之间的相同前缀,然后取长度最小的相同前缀即为最小共同前缀。代码实现class Solution {public: string commonBetwTwo(stri原创 2017-12-14 00:12:20 · 192 阅读 · 0 评论 -
26-Remove Duplicates from Sorted Array
难度:easy 类别:array题目描述算法分析直接使用vector的erase函数即可,注意删除掉某个相同的数字之后,因为后面的数字需要与被删除数字前面的那个数字进行比较,所以i需要保持不变,所以i–和for循环中的i++使得i保持不变。代码实现class Solution {public: int removeDuplicates(vector<int>& nums) {原创 2017-12-13 22:36:54 · 186 阅读 · 0 评论 -
9-palindrome Number(回文)
难度:easy题目描述https://leetcode.com/problems/palindrome-number/description/ 算法分析首先得到x的反转数字,注意判断反转得到的数字是否在32为有符号int的范围内部,如果不在范围内,返回false,如果x是负数,返回false,最后判断x和反转后的整数是否相等即可代码实现class Solution {public: bo原创 2017-12-13 22:19:03 · 145 阅读 · 0 评论 -
66-PlusOne
难度:easy题目描述Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The di原创 2017-12-17 16:26:26 · 158 阅读 · 0 评论 -
67-add Binary
难度:easy题目描述算法分析对都有字符的进行相加,并且注意carry的值,对于不同长度的部分,与carry进行相加即可。 char转int,ch - ‘0’ int转char,int + ‘0’代码实现class Solution {public: string addBinary(string a, string b) { int n1 = a.length(),原创 2017-12-17 17:08:53 · 309 阅读 · 0 评论 -
207-Course Schedule
类别:graph(拓扑)难度:medium题目描述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原创 2018-01-09 13:53:43 · 278 阅读 · 0 评论 -
718-Maximum Length of Repeated Array
类别:dynamic programming难度:medium题目描述Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.Example 1:Input:A: [1,2,3,2,1]B: [3,原创 2018-01-09 00:10:31 · 337 阅读 · 0 评论 -
523-Continuous Subarray Sum
类别:dynamic programming难度:medium题目描述Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that s原创 2018-01-08 15:25:26 · 261 阅读 · 0 评论 -
241-Different ways to add parentheses
类别:divide and conquer难度:medium题目描述Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The vali原创 2018-01-08 00:09:40 · 303 阅读 · 0 评论 -
322-Coin Change
类别:dynamic programming难度:medium动态规划01背包问题参考连接:http://blog.youkuaiyun.com/hearthougan/article/details/53749841http://blog.youkuaiyun.com/hearthougan/article/details/53869671题目描述:You are原创 2018-01-07 23:34:49 · 327 阅读 · 0 评论 -
01 matrix
类别:BFS难度:medium题目描述Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.The distance between two adjacent cells is 1.input;0 0 00 1 00 0 0outp原创 2018-01-07 00:01:28 · 558 阅读 · 0 评论 -
515-Find largest Value in Each Tree Low
类别:BFS难度:medium题目描述算法分析:因为需要确定是哪一行,所以将节点放到队列中的时候需要标识它的行号。首先,1进入队列,行号为0【】将队列中的对头节点取出,然后将其左右节点放到队列中,其左右节点的行号为父节点行号+1, 【】对于从队列中取出来的节点,记其行号为k,将该节点的数值与ans[k]比较,取较大者,当该行的节点全部遍历完即可得到该原创 2018-01-06 23:28:30 · 213 阅读 · 0 评论 -
659-Split Array into Consecutive Subsequences
类别:greedy难度:medium题目描述首先,需要正确理解题目的意思,最开始的时候误解为是只要将所有的数字分成两组,然后每一组中的数字含有三个以上连续的数字,后来发现并不是。题目的意思是:将数组划分为子数组,使得每个子数组中的数字都是连续无重复的并且每个子数组的长度需要大于等于三。判断是否能够进行这样的划分,可以的话返回true,否则返回false。算法分析原创 2018-01-06 09:27:47 · 257 阅读 · 0 评论 -
455-Assign Cookies
类别:greedy 难度:easy题目描述算法描述(1)对孩子们的最低要求进行排序,对所有的饼干进行排序 (2)将能够满足孩子要求的最小的饼干给相应的孩子,直到遍历完两个数组。代码实现class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { int n = g.si原创 2018-01-04 00:04:55 · 225 阅读 · 0 评论 -
743-Network Delay Time
类别:graph难度:medium题目描述There are N network nodes, labelled 1 to N.Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the t原创 2018-01-11 18:28:50 · 615 阅读 · 0 评论 -
113-Path Sum II
类别:DFS 难度:medium题目描述算法分析与112的算法基本一样,但是要注意在实现的过程中对于左子树和右子树path分别进行复制,因为if是先后进行的,第一个if判断后会在第二个if中使用path,而此时path已经发生改变代码实现class Solution {public: void findPath(vector<vector<int>>& result, vector<in原创 2017-12-18 21:49:45 · 187 阅读 · 0 评论 -
112-Path Sum
类别:DFS 难度:easy题目描述算法分析用递归的方式,分别对左子树和右子树存在的路径进行和进行判断。当当前节点的左右节点分别为NULL时,即路径已经到达叶子节点。代码实现bool hasPathSum(TreeNode* root, int sum) { if (root == NULL) return false; bool result = false; if (r原创 2017-12-18 21:42:19 · 208 阅读 · 0 评论 -
310-Minimum Height Trees
类别:graph难度:medium题目描述For a 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原创 2018-01-11 10:25:18 · 250 阅读 · 0 评论 -
111、257(递归,DFS)
类别:recursive, DFS111-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 near原创 2017-12-17 21:13:15 · 224 阅读 · 0 评论 -
100、101、104-递归
类别:depth first search 难度:easy100-Same Tree给定两棵树的根节点,判断两棵树是否相同 只需要对空节点进行判断,然后分别递归判断左子树和右子树即可class Solution {public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL && q == NULL)原创 2017-12-13 21:04:22 · 205 阅读 · 0 评论 -
70-Climbing Stairs
类别:dynamic programming 难度:easy题目描述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?算法原创 2017-12-04 14:06:17 · 151 阅读 · 0 评论 -
最小和(sicily算法)
1.题目要求从数列A[0], A[1], A[2], …, A[N-1]中选若干个数,要求对于每个i(0<=i2.算法分析动态规划,对于n个数的最小和,需要比较三种情况:(A[i-1], a[i] + A[i-1], a[i] + A[i - 2]),取三者中最小的一个,但是对于A[i], A[i] = min(A[i] + A[i - 1], A[i] + A[i - 2]);3.代码实现#inc原创 2017-12-03 20:40:36 · 813 阅读 · 0 评论 -
237-delete node in single list
难度:easy 类别:linked list1.题目描述给定要删除的结点,要求将该结点删除2.实现分析因为是单向链表,并且没有给定head指针,所以要想办法处理next指针。将问题转换为将node->next给node并且删除node->next即可。3.代码class Solution {public: void deleteNode(ListNode* node) {原创 2017-10-24 23:19:23 · 200 阅读 · 0 评论 -
24-Swap Nodes In Pairs
难度:medium 类别:linked list1.题目描述Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use原创 2017-11-01 22:02:37 · 158 阅读 · 0 评论 -
19-Remove Nth node from end list
难度:medium 类别:linked list1.题目描述Given a linked list, remove the nth node from the end of list and return its head. Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the原创 2017-11-01 17:50:36 · 183 阅读 · 0 评论 -
445-Add Two Numbers II
[难度] medium [分类] linked list1.题目描述You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit原创 2017-10-13 13:38:18 · 204 阅读 · 0 评论 -
2-Add Two Numbers
1.题目描述You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and ret原创 2017-10-19 23:03:21 · 184 阅读 · 0 评论 -
690-Employee Importance
[难度] easy [分类] array1.题目描述You are given a data structure of employee information, which includes the employee’s unique id, his importance value and his direct subordinates’ id.For example, employee 1原创 2017-10-08 22:48:08 · 339 阅读 · 0 评论