![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 76
momo_mo520
这个作者很懒,什么都没留下…
展开
-
LeetCode(22)Generate Parentheses(输出可能的合法表达式)(递归进行,在不同的条件可以加左括号,右括号)(像二叉树中序遍历)
题目Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:“((()))”, “(()())”, “(())()”, “()(())”, “()()()”分析给...转载 2018-05-29 21:23:50 · 415 阅读 · 0 评论 -
【LeetCode】17. Letter Combinations of a Phone Number
Letter Combinations of a Phone NumberGiven a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is...转载 2018-05-29 20:44:12 · 103 阅读 · 0 评论 -
LeetCode 14 Longest Common Prefix(最长公共前缀)(String)
思路优化,本来是把第一个字符串的长度作为初始预估prefix值。后来看到别人写的,还是觉得提前循环一遍,找到最短的字符串的长度作为prefix的长度预估值。双层循环:外层循环,用下图横着做,开始都是str[j][0]---str[j][1]---str[j][minlen-1]内层循环,用下图竖着做,大家都是第一个字符,看看一样吗,不一样则break。 如果...原创 2018-05-29 19:38:31 · 298 阅读 · 0 评论 -
LeetCode 8:String to Integer (atoi)
题目的大概意思是:按照atoi函数的转换原则,将输入的字符串转换成整型数。其实就是自行实现atoi函数。这道题难度等级:简单需要注意的是以下几点:但是怎么觉得不止这几种情况啊,这些情况我都考虑且避免了。还是AC不掉,暂时没有找到一个详细的转换原则。网上找到的代码如下:class Solution {public: int myAtoi(string str) { ...转载 2018-05-29 09:59:40 · 106 阅读 · 0 评论 -
LeetCode - 371. Sum of Two Integers
在不准使用+和-的情况下实现两个整数的加法,那么肯定要用到位运算了。我们考虑位运算加法的四种情况:0 + 0 = 01 + 0 = 10 + 1 = 01 + 1 = 1(with carry)在学习位运算的时候,我们知道XOR的一个重要特性是不进位加法,那么只要再找到进位,将其和XOR的结果加起来,就是最后的答案。通过观察上面的四种情况我们可以发现,只有在两个加数的值都是1的时候才会产生进位,所...转载 2018-06-02 23:10:09 · 93 阅读 · 0 评论 -
[LeetCode] 236 Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点 235 二叉搜索树的最小共同父节点
Given 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 ancestor is defined between two nodes v an...转载 2018-06-02 18:23:54 · 129 阅读 · 0 评论 -
[LeetCode]230.Kth Smallest Element in a BST
题目Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Follow up: What if the BST is mod...转载 2018-06-02 18:04:01 · 102 阅读 · 0 评论 -
LeetCode127—Word Ladder(我觉得还挺难的,比如如何判断邻居节点,怎样记录路径长度,感觉有点像DP)
Word LadderGiven two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate...转载 2018-05-22 12:35:58 · 907 阅读 · 0 评论 -
LeetCode 102. Binary Tree Level Order Traversal 103二叉树分层Z字形遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7],3/ \9 20/ \15 7return its lev...原创 2018-05-22 10:13:16 · 84 阅读 · 0 评论 -
Leetcode 207 &210 Course Schedule I II 课程表
题目:课程表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 ...转载 2018-05-21 11:12:46 · 391 阅读 · 0 评论 -
LeetCode 116 Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }12345 Populate each next pointer to point to its next right node. If t...原创 2018-05-20 20:41:46 · 86 阅读 · 0 评论 -
leetcode 104 Maximum Depth of Binary Tree二叉树求深度
求二叉树深度递归方法:class Solution {public: int maxDepth(TreeNode* root) { if(NULL == root) return 0; int depth_l = maxDepth(root->left); int depth_r = maxDepth(...原创 2018-05-20 16:54:04 · 84 阅读 · 0 评论 -
【LeetCode】101. Symmetric Tree 中序遍历,分支遍历,二叉树
101. Symmetric TreeTotal Accepted: 103742 Total Submissions: 306773 Difficulty: EasyGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this bina...转载 2018-05-20 15:59:17 · 120 阅读 · 0 评论 -
LeetCode 98. Validate Binary Search Tree
方法1:class Solution {public: bool dfsleft(TreeNode *root,int value){ if(root==NULL) return true; if(root->val>=value) return false; return dfsleft(root->left,value...原创 2018-05-20 12:04:10 · 74 阅读 · 0 评论 -
leetcode 328 Odd Even Linked List(调整链表使得奇数位置的元素位于偶数位置元素之前)
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in...原创 2018-05-19 21:18:16 · 550 阅读 · 0 评论 -
LeetCode 237 : Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with ...原创 2018-05-19 20:53:42 · 183 阅读 · 0 评论 -
【LeetCode】28. Implement strStr()
首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回-1。然后我们开始遍历母字符串,我们并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符串相等的位置即可,这样可以提高运算效率。然后对于每一个字符,我们都遍历一遍子字符串,一个一个字符的对应比较,如果对应位置有不等的,则跳出循环,如果一直都没有跳出循环,则说明子字符串出现了,则返回起始位置即可,代码如下:cla...原创 2018-05-29 22:13:02 · 152 阅读 · 0 评论 -
LeetCode(49)Group Anagrams(查找同构字符串)(int->string 用方法 to_string())
题意理解:将给定字符串分组,要求同一组的字符串由相同字符组成,最终,将各组字符串按字典序输出;题目分析:1. 将字符串先进行排序,然后通过字符串比较来判定是否为同一组;2. 将分组后的字符串集合再进行排序,从而满足题目的字典序要求;3. 在分组的过程中需要进行查找,本题中使用了hash的思想,但C++的STL中没有hash容器,解题代码中使用map来完成hash功能(map由红黑数实现,效率比很实...原创 2018-05-30 10:23:56 · 139 阅读 · 0 评论 -
559. Maximum Depth of N-ary Tree
个人分类: C++OJLeetCodeSelf-Culture版权声明:仅供参考与学习交流 https://blog.csdn.net/lym940928/article/details/811772761,题目要求 Given a n-ary tree, find its maximum depth.The maximum depth is the number of nodes ...转载 2018-09-08 11:54:14 · 122 阅读 · 0 评论 -
【一直不知道for(Node* child : root->children)】C++for的几种方式
// 第五种用法:C++11新增加的(VS2012支持)for(auto item : vecNum){ strText.Format("%d", item); AfxMessageBox(strText);}#include <algorithm>#include <vector>/////////////////////////...转载 2018-09-08 11:52:20 · 1743 阅读 · 1 评论 -
leetcode179 Largest Number(把数组排成最大的数)
问题描述:输入一个正整数数组,把数组里的所有数字拼接起来排成一个数,打印能拼接出的所有数字中最大的一个。例如输入数组{3,32,321},则打印出332321.问题求解:这道题和剑指offer面试题33:把数组排成最小的数一样思路。就是要找到一种排序规则,数组根据这个规则排序之后能排成一个最大的数字。要确定这个规则,就要比较2个数字:给出m和n,我们需要确定规则来判断m和n那个应该排在前面,而不仅...转载 2018-06-02 15:35:36 · 550 阅读 · 0 评论 -
[leedcode 134] Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its ne...原创 2018-06-02 12:07:29 · 94 阅读 · 0 评论 -
LeetCode131:Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = “aab”, Return[ [“aa”,”b”], [“a”,”a”,”b”...转载 2018-06-02 11:17:26 · 129 阅读 · 0 评论 -
130 Surrounded Regions
题目链接:https://leetcode.com/problems/surrounded-regions/题目:Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that su...转载 2018-06-02 10:40:54 · 84 阅读 · 0 评论 -
LeetCode(46)Permutations
题目Given a collection of numbers, return all possible permutations.For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].分析求给定向量数组所有元素的全排列问题。我们知...转载 2018-06-01 21:17:50 · 79 阅读 · 0 评论 -
【LeetCode】36. Valid Sudoku
Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially filled ...原创 2018-06-01 20:32:47 · 72 阅读 · 0 评论 -
LeetCode 11 Container With Most Water(最大水容器)
摘要: 翻译 给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai)。 n个垂直线段例如线段的两个端点在(i,ai)和(i,0)。 找到两个线段,与x轴形成一个容器,使其包含最多的水。翻译给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai)。n个垂直线段例如线段的两个端点在(i,ai)和(i,0)。找到两个线段,与x轴形成一个容器,使其包含最多...转载 2018-06-01 17:44:51 · 120 阅读 · 0 评论 -
LeetCode 204:Count Primes(素数筛选)
厄拉多塞筛法,素数筛选法多看没办法了我就去Google了一下,于是知道了厄拉多塞筛法:西元前250年,希腊数学家厄拉多塞(Eeatosthese)想到了一个非常美妙的质数筛法,减少了逐一检查每个数的的步骤,可以比较简单的从一大堆数字之中,筛选出质数来,这方法被称作厄拉多塞筛法(Sieve of Eeatosthese)。具体操作:先将 2~n 的各个数放入表中,然后在2的上面画一个圆圈,然后划去2...原创 2018-06-01 12:28:44 · 173 阅读 · 0 评论 -
leetcode 166. Fraction to Recurring Decimal
题目:Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.For ...转载 2018-06-01 10:48:28 · 108 阅读 · 0 评论 -
Leetcode 50 Pow(x, n) 两种方式求解
原题链接: http://oj.leetcode.com/problems/powx-n/ 这道题是一道数值计算的题目,因为指数是可以使结果变大的运算,所以要注意越界的问题。如同我在Sqrt(x)这道题中提到的,一般来说数值计算的题目可以用两种方法来解,一种是以2为基进行位处理的方法,另一种是用二分法。这道题这两种方法都可以解,下面我们分别介绍。第一种方法在Divide Two Integers使...原创 2018-05-31 22:30:34 · 105 阅读 · 0 评论 -
Leetcode 50 Pow(x, n) 两种方式求解
原题链接: http://oj.leetcode.com/problems/powx-n/ 这道题是一道数值计算的题目,因为指数是可以使结果变大的运算,所以要注意越界的问题。如同我在Sqrt(x)这道题中提到的,一般来说数值计算的题目可以用两种方法来解,一种是以2为基进行位处理的方法,另一种是用二分法。这道题这两种方法都可以解,下面我们分别介绍。第一种方法在Divide Two Integers使...原创 2018-05-31 22:14:05 · 197 阅读 · 0 评论 -
LeetCode --- 29. Divide Two Integers(位运算)(异或)
题目链接:Divide Two IntegersDivide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.这道题的要求是在不使用乘法、除法、取模运算的前提下实现两个整数相除。如果溢出,返回MAX_INT。这道题的直接思路是用被除数不断减去...转载 2018-05-31 17:28:07 · 121 阅读 · 0 评论 -
leetcode 227: Basic Calculator II
class Solution {public: //利用堆栈实现基本运算,对给定的字符串进行轮训,如果是数字则进行循环累积,直到不为数字,则考虑如何将数字压入堆栈。 //如果之前记录的flag操作符是+或者-号,则将该数字压入到堆栈中,如果是操作符是*或者/号,则将栈顶元素取出之后与当前数字进行运算,将运算结果保存到堆栈中。最后堆栈中所保存的数字之间均为+运算关系,则只需要将堆...原创 2018-05-30 20:26:25 · 89 阅读 · 0 评论 -
LeetCode91:Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total n...转载 2018-05-30 10:34:58 · 95 阅读 · 0 评论 -
LeetCode 206. Reverse Linked List(单链表的逆置) LeetCode234_PalindromeLinkedList (判断是否为回文链表)
单链表的逆置class Solution {public: ListNode* reverseList(ListNode* head) { ListNode *p1,*p2,*p3; if(head==NULL||head->next==NULL) return head; p1=head; p2=p1->nex...原创 2018-05-19 19:00:15 · 136 阅读 · 0 评论 -
LeetCode 160 Intersection of Two Linked Lists 求两个链表的交点
A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3第一种:分别遍历两个链表,得到分别对应的长度。然后求长度的差值,把较长的那个链表向后移动这个差值的个数,然后一一比较即可。/**...原创 2018-05-19 18:47:38 · 80 阅读 · 0 评论 -
【很优秀的分析】LeetCode169:Majority Element 229. Majority Element II
转载的链接点击打开链接 帮助很大十分感谢。Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the ma...转载 2018-04-19 16:34:13 · 184 阅读 · 0 评论 -
leetcode_442. Find All Duplicates in an Array 找数组中出现两次的数
三种方法在小本本上。法1(不满足条件用的hash)class Solution {public: vector<int> findDuplicates(vector<int>& nums) { int len=nums.size(); vector<int> ans; if(len<=1) ...原创 2018-04-23 21:33:51 · 97 阅读 · 0 评论 -
LeetCode 62. Unique Paths LeetCode 63 Unique Paths II 不同的路径之二
初识动态规划。格点(i,j)。由于只能从上格点(i-1,j)或左格点(i,j-1)到达,并且两者路径是不重复的因此path[i][j] = path[i-1][j]+path[i][j-1]。最近要开始看动态规划了。class Solution {public: int uniquePaths(int m, int n) { vector<vector<int...原创 2018-04-13 19:35:25 · 147 阅读 · 0 评论 -
279. Perfect Squares
我的方法,感觉思路不够清晰,不过还是有进步的。对DP有种莫名的恐惧,下次加油。class Solution {public: int numSquares(int n) { // n=8; if(n<=3) return n; vector <int> dp(n+1,INT_MAX);//初始值是多少 //d...原创 2018-04-30 18:08:21 · 207 阅读 · 0 评论