![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
不爱敲代码
这个作者很懒,什么都没留下…
展开
-
69. Sqrt(x)
class Solution {public: int mySqrt(int x) { int result=int(sqrt(x)); return result; }};二分法class Solution { public: int mySqrt(int x) {long long i = 0;long long j = x / 2...原创 2018-05-26 02:16:59 · 84 阅读 · 0 评论 -
520. Detect Capital
class Solution {public: bool detectCapitalUse(string word) { int firstbig=0; int allbig=1; int alllittle=1; for(int i=0;i<word.size();i++) if(islowe...原创 2018-05-30 15:15:15 · 94 阅读 · 0 评论 -
844. Backspace String Compare
class Solution {public: bool backspaceCompare(string S, string T) { vector<char> res1; vector<char> res2; for(auto c:S) {if(c=='#'&&!res1.empty...原创 2018-06-06 18:39:41 · 498 阅读 · 0 评论 -
89. Gray Code
百度百科上有:递归生成码表这种方法基于格雷码是反射码的事实,利用递归的如下规则来构造:1位格雷码有两个码字(n+1)位格雷码中的前2n个码字等于n位格雷码的码字,按顺序书写,加前缀0(n+1)位格雷码中的后2n个码字等于n位格雷码的码字,按逆序书写,加前缀1n+1位格雷码的集合 = n位格雷码集合(顺序)加前缀0 + n位格雷码集合(逆序)加前缀1pow(x,y);//其作用是计算x的y次方前2^...翻译 2018-06-06 20:26:42 · 132 阅读 · 0 评论 -
455. Assign Cookies
class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { int count=0;//贪心,先排序,遇见合适的就匹配 sort(g.begin(),g.end()); sort(s.begin()...原创 2018-06-06 23:40:01 · 146 阅读 · 0 评论 -
134. Gas Station
class Solution {public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int begin=0; int gasv=0; for(int i=0;i<gas.size();i++) ...原创 2018-06-07 01:11:31 · 221 阅读 · 0 评论 -
136. Single Number
class Solution{public: int singleNumber(vector<int> &nums) { int n = 0; for (int i=0;i<nums.size();i++) { n ^= nums[i]; } ret...转载 2018-06-07 01:38:42 · 90 阅读 · 0 评论 -
39. Combination Sum
class Solution {public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { sort(candidates.begin(), candidates.end()); vector<vecto...原创 2018-06-07 21:07:49 · 111 阅读 · 0 评论 -
40. Combination Sum II
class Solution {public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { sort(candidates.begin(), candidates.end()); vector<vect...原创 2018-06-08 00:19:58 · 102 阅读 · 0 评论 -
2. Add Two Numbers
class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode prehead(0),*p=&prehead; int carry=0; while(l1||l2||carry){ int sum=(...原创 2018-06-01 15:09:58 · 94 阅读 · 0 评论 -
78. Subsets
class Solution {public: vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> res; vector<int> temp; f(0,res,nums,te...原创 2018-06-08 23:37:37 · 95 阅读 · 0 评论 -
90. Subsets II
class Solution {public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>> res; sort(nums.begin(),nums.end()); v...原创 2018-06-09 00:31:50 · 124 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
class Solution {public://不能开辟新的空间 int removeDuplicates(vector<int>& nums) { //sort(nums.begin(),nums.end());//已经排好序 if(nums.empty()) return 0; int count=1; ...原创 2018-06-09 15:01:53 · 119 阅读 · 0 评论 -
48. Rotate Image
class Solution {public: void rotate(vector<vector<int>>& matrix) { int n=matrix.size(); //先求转置矩阵 for(int i=0;i<n;i++) for(int j=i+1;j<n;j...原创 2018-06-09 16:33:08 · 134 阅读 · 0 评论 -
51. N-Queens
class Solution {public: vector<vector<string>> solveNQueens(int n) { vector<vector<string>> res; vector<string> board(n,string(n,'.'));//棋盘 f(0,r...原创 2018-06-09 23:04:39 · 300 阅读 · 0 评论 -
leetcode 130. Surrounded Regions 被围绕的区域 c++
给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。示例:X X X XX O O XX X O XX O X X运行你的函数后,矩阵变为:X X X XX X X XX X X XX O X X 关键:与边界‘O’有联系的区域都不会填充,其他都会被填充第一步:...原创 2018-08-06 16:09:57 · 691 阅读 · 0 评论 -
796. Rotate String
class Solution {public: bool rotateString(string A, string B) { int flag=0; if((A=="")&&(B=="")) return true; for(int i=0;i<A.size();i++) if((A.subs...原创 2018-05-30 14:30:05 · 130 阅读 · 0 评论 -
682. Baseball Game
class Solution {public: int getnum(string &s) { int num; stringstream ss; ss<<s; ss>>num; return num; } int calPoints(vector<...原创 2018-05-29 22:45:14 · 122 阅读 · 0 评论 -
119. Pascal's Triangle II
class Solution {public: vector<int> getRow(int rowIndex) { vector<int> v(rowIndex+1); v[0]=1; for(int i=1;i<=rowIndex;i++) for(int j=i+1;j>0;j-...原创 2018-06-03 01:41:37 · 85 阅读 · 0 评论 -
46. Permutations
思路:递归解决,按平时排列的习惯,先从首元素相同,然后下一位相同。类似于 [1,2,3] ,[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]定义一个递归函数f, 传入nums,result和递归的层数(即从nums的第几个元素开始),第一步设置递归的终点,当递归层数大于或等于nums中元素个数时,结束。并且将此阶段得到的nums 加入result中,然后退出。第二步分...原创 2018-06-03 22:46:32 · 105 阅读 · 0 评论 -
67. Add Binary
class Solution{public: string addBinary(string a, string b) { string s = ""; int c = 0, i = a.size() - 1, j = b.size() - 1; while(i >= 0 || j >= 0 || c...原创 2018-05-23 15:39:32 · 112 阅读 · 0 评论 -
47. Permutations II
和46题类似,只需要改动一个地方,避免相同序列的排列这里要用set加入一个判断,如果set存在当前序列,跳过本次循环(用continue),每遍历一个排列,便把它加入set中。class Solution {public: vector<vector<int>> permuteUnique(vector<int>& nums) { ...原创 2018-06-04 11:09:25 · 82 阅读 · 0 评论 -
22. Generate Parentheses
n=1, f(v,"",1,1) -> f(v,"(",0,1)->f(v,"()",0,0)->return 此时不调用f(v,")",1,0)n=2,f(v,"",2,2) -> f(v,"(",1,2) ->f(v,"((",0,2)-&原创 2018-06-04 15:05:43 · 86 阅读 · 0 评论 -
100. Same Tree
class Solution {public: bool isSameTree(TreeNode* p, TreeNode* q) { if(!p&&!q) return true; else if(p&&!q) return false; else if(!p&&q) return fals...原创 2018-05-28 23:09:54 · 97 阅读 · 0 评论 -
101. Symmetric Tree
class Solution {public: bool isSymmetric2(TreeNode* lr,TreeNode* rr) { if(!lr&&!rr) return true; else if(!lr&&rr) return false; else if(lr&&!rr) re...原创 2018-05-28 23:25:39 · 64 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
class Solution {public: int maxDepth(TreeNode* root) { if(!root) return 0; int leftdepth=maxDepth(root->left); int rightdepth=maxDepth(root->right); return l...原创 2018-05-28 23:30:06 · 77 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
class Solution {public: int minDepth(TreeNode* root) { if(!root) return 0; int leftdepth=minDepth(root->left); int rightdepth=minDepth(root->right); if(!root...原创 2018-05-28 23:46:42 · 141 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
class Solution {public: vector<int> v;//中序遍历左子树 --> 显示结点数据,或其他对结点的操作 --> 中序遍历右子树。 vector<int> inorderTraversal(TreeNode* root) { if(root == NULL) return v; ...原创 2018-05-29 01:01:01 · 94 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
class Solution {public: vector<string>v={"","","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string>原创 2018-06-05 00:33:20 · 91 阅读 · 0 评论 -
66. Plus One
class Solution {public: vector<int> plusOne(vector<int>& digits) {//i表示索引 int i= digits.size()-1;//carry表示进位//题目要求+1 故carry初始值为1 int carry=1; while(i>=0...原创 2018-05-24 14:03:41 · 104 阅读 · 0 评论 -
96. Unique Binary Search Trees
class Solution { public: int numTrees(int n) { vector<int> v(n+1,0); v[0] = 1; v[1] = 1; for (int i = 2; i <= n; ++i){ for (int k ...原创 2018-05-29 12:46:24 · 97 阅读 · 0 评论 -
7. Reverse Integer
会溢出int型的数值范围是 -2147483648~2147483647, 那么如果我们要翻转 1000000009 这个在范围内的数得到 9000000001,而翻转后的数就超过了范围。class Solution {public: int reverse(int x) { int result=0;int flag=0; if(x<0) {x=-...原创 2018-05-29 20:14:50 · 147 阅读 · 0 评论 -
110. Balanced Binary Tree
class Solution {public: int getdepth(TreeNode* root){ if(root==NULL) return 0; int left=getdepth(root->left); int right=getdepth(root->right); return left>...原创 2018-05-29 21:45:51 · 85 阅读 · 0 评论 -
leetcode 128. Longest Consecutive Sequence 并查集
1.题目给定一个未排序的整数数组,找出最长连续序列的长度。2.思路将相邻的数字union核心是并查集的思想,用一个map来作为一个工具 遍历给出的样例,将每个元素依次加入哈希map中,<元素的值,索引>如果有相同元素,忽略如果有相邻元素(比当前元素大1或小1),union两个元素的map索引值find函数和union函数没有变化新添加了一个计算最长长...原创 2019-02-01 01:07:58 · 582 阅读 · 0 评论