自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(34)
  • 收藏
  • 关注

原创 leetcode 128. Longest Consecutive Sequence 并查集

1.题目给定一个未排序的整数数组,找出最长连续序列的长度。2.思路将相邻的数字union核心是并查集的思想,用一个map来作为一个工具 遍历给出的样例,将每个元素依次加入哈希map中,<元素的值,索引>如果有相同元素,忽略如果有相邻元素(比当前元素大1或小1),union两个元素的map索引值find函数和union函数没有变化新添加了一个计算最长长...

2019-02-01 01:07:58 544

原创 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 675

原创 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 275

原创 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 107

原创 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 110

原创 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 110

原创 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 85

原创 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 92

原创 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 100

转载 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 81

原创 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 195

原创 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 137

翻译 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 123

原创 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 475

原创 17. Letter Combinations of a Phone Number

class Solution {public: vector<string>v={"","","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string&gt

2018-06-05 00:33:20 80

原创 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)-&amp

2018-06-04 15:05:43 76

原创 47. Permutations II

和46题类似,只需要改动一个地方,避免相同序列的排列这里要用set加入一个判断,如果set存在当前序列,跳过本次循环(用continue),每遍历一个排列,便把它加入set中。class Solution {public: vector<vector<int>> permuteUnique(vector<int>& nums) { ...

2018-06-04 11:09:25 71

原创 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 97

原创 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 72

原创 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 83

原创 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 85

原创 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 124

原创 682. Baseball Game

class Solution {public: int getnum(string &s) { int num; stringstream ss; ss<<s; ss>>num; return num; } int calPoints(vector&lt...

2018-05-29 22:45:14 114

原创 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&gt...

2018-05-29 21:45:51 74

原创 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 138

原创 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 86

原创 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 85

原创 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 127

原创 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 65

原创 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 55

原创 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 88

原创 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 73

原创 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 80

原创 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 101

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除