- 博客(39)
- 收藏
- 关注
原创 LeetCode - 565. Array Nesting
解题代码:classSolution {public: int arrayNesting(vector&nums) { int n=nums.size(); int res=0; for(int i=0;i if(nums[i]!=n){ int count=0;
2017-06-08 16:34:05 680
原创 LeetCode - 600. Non-negative Integers without Consecutive Ones
解题代码:classSolution {public: int findIntegers(int num) { if(num==0) return 1; if(num==1) return 2; if(num return 3; i
2017-06-01 21:11:19 768
原创 LeetCode - 337. House Robber III
解题代码:classSolution {public: void res(TreeNode* root){ if(root->left!=NULL) res(root->left); if(root->right!=NULL) res(root->right); int
2017-05-24 20:14:54 229
原创 LeetCode - 583. Delete Operation for Two Strings
解题代码:classSolution {public: int minDistance(string word1, string word2){ int m=word1.size(),n=word2.size(); int dp[m+1][n+1]={}; for(int i=1;i for(int
2017-05-22 20:36:18 1121
原创 LeetCode - 438. Find All Anagrams in a String & 567. Permutation in String
解题代码:438:classSolution {public: vector findAnagrams(string s,string p) { vector res; vector pp(26,0),ss(26,0); if(p.size()>s.size()) return res;
2017-05-19 17:26:30 232
原创 LeetCode - 18. 4Sum
解题代码:classSolution {public: vector>fourSum(vector& nums, int target) { vector> res; if(nums.size() return res; sort(nums.begin(),nums.end());
2017-05-12 16:16:18 178
原创 LeetCode - 16. 3Sum Closest
解题代码:classSolution {public: int threeSumClosest(vector&nums, int target) { int abc=INT_MAX; int res; sort(nums.begin(),nums.end()); for(int i=0;i
2017-05-12 14:49:27 149
原创 LeetCode - 15. 3Sum
解题代码:classSolution {public: vector>threeSum(vector& nums) { vector> res; if(nums.size() return res; sort(nums.begin(),nums.end()); for(int i=0
2017-05-10 16:41:17 153
原创 LeetCode - 263. Ugly Number & 264. Ugly Number II & 313. Super Ugly Number
解题代码:263. Ugly Number:class Solution {public: bool isUgly(int num) { if(num return false; while(num%2==0) num/=2; while(num%3==0)
2017-05-02 09:36:21 209
原创 LeetCode - 74. Search a 2D Matrix & 240. Search a 2D Matrix II
解题代码:74. Search a 2D MatrixclassSolution {public: boolsearchMatrix(vector>& matrix, int target) { if(matrix.size()==0||matrix[0].size()==0) return false; int m
2017-04-21 21:02:43 288
原创 LeetCode - 198. House Robber &213. House Robber II
解题代码:198. House Robber:classSolution {public: int rob(vector& nums) { if(nums.size()==0) return 0; if(nums.size()==1) return nums[0]; if
2017-04-21 17:13:41 229
原创 LeetCode - 96. Unique Binary Search Trees
解题代码:classSolution {public: int numTrees(int n) { if(n return 0; if(n==1) return 1; vector res(n+1,0); res[1]=1; int i=2
2017-04-21 16:48:41 214
原创 LeetCode - 62. Unique Paths
解题代码:(1)classSolution {public: int uniquePaths(int m, int n) { int res[m+1][n+1]; for(int i=1;i res[i][1]=1; for(int i=1;i res[1][i]=1;
2017-04-21 16:33:03 174
原创 LeetCode - 423. Reconstruct Original Digits from English
解题代码:classSolution {public: string originalDigits(string s) { vector count(26,0); string res; int a[10]; for(int i=0;i count[s[i]-'a']++;
2017-04-09 12:24:37 289
原创 LeetCode - 22. Generate Parentheses
解题代码:classSolution {public: void com(string str,int a,intb,vector& res){ if(a==0&&b==0){ res.push_back(str); return; } if(b>0)
2017-04-09 11:49:07 648
原创 LeetCode - 238. Product of Array Except Self
解题代码:classSolution {public: vectorproductExceptSelf(vector& nums) { vector a(nums.size()); vector b(nums.size()); a[0]=1; b[0]=1; for(int i=1;i
2017-04-05 12:23:25 196
原创 LeetCode - 547. Friend Circles
解题代码:classSolution {public: voidfind(vector>& M,vector& num,int i){ num[i]=1; for(int j=0;j if(j!=i&&M[i][j]==1&&num[j]!=1) find(M,num,j);
2017-04-05 11:40:21 239
原创 LeetCode - 273. Integer to English Words
解题代码:classSolution {public: string find(int n){ switch(n){ case 1: return "One"; case 2: return "Two"; case 3: return "Three"; c
2017-04-01 21:48:38 154
原创 LeetCode - 343. Integer Break
解题代码:classSolution {public: int integerBreak(int n) { if(n==2) return 1; if(n==3) return 2; if(n==4) return 4; int r
2017-04-01 16:21:16 203
原创 LeetCode - 400. Nth Digit
解题代码:classSolution {public: int findNthDigit(int n) { long i=1,t=9; while(n>t*i){ n-=t*i; i++; t*=10; } int a=n/i,b=
2017-03-31 15:24:12 434
原创 LeetCode - 367. Valid Perfect Square
解题代码:classSolution {public: bool isPerfectSquare(int num) { if(num return false; while(num%2==0){ if(num%4!=0) return false;
2017-03-28 13:30:10 228
原创 LeetCode - 415. Add Strings
解题代码:classSolution {public: string addStrings(string num1, string num2){ if(num1.size() return addStrings(num2, num1); inti=num1.size()-1,j=num2.size()-1,k=0;
2017-03-26 11:25:54 139
原创 LeetCode - 112. Path Sum
解题代码:classSolution { int find=0;public: void cal(TreeNode* root, int sum,ints,int& find){ if(root==NULL) return; s+=root->val; if(root->left==NULL&
2017-03-22 14:03:37 125
原创 LeetCode - 231. Power of Two/326. Power of Three/342. Power of Four
解题代码:231:classSolution {public: bool isPowerOfTwo(int n) { returnn>0&&(n&(n-1))==0; }}; 326:classSolution {public: bool isPowerOfThree(int n) { if(n
2017-03-22 12:17:41 232
原创 LeetCode - 481. Magical String
解题代码:classSolution {public: int magicalString(int n) { if(n return 0; else if(n return 1; int count=1,j=3,last=2; string str="122
2017-03-19 21:31:22 244
原创 LeetCode - 383. Ransom Note
解题代码:classSolution {public: bool canConstruct(string ransomNote, stringmagazine) { for(inti=0;i int p=magazine.find(ransomNote[i]); if(p re
2017-03-19 18:22:49 157
原创 LeetCode - 455. Assign Cookies
解题代码:classSolution {public: intfindContentChildren(vector& g, vector& s) { int res=0,i=0,j=0; sort(g.begin(),g.end()); sort(s.begin(),s.end()); while(i
2017-03-19 18:06:04 166
原创 LeetCode - 167. Two Sum II - Input array is sorted
解题代码:classSolution {public: vectortwoSum(vector& numbers, int target) { vector result; int i=0,j=numbers.size()-1; while(i if(numbers[i]+numbers[j]==ta
2017-03-19 17:43:33 144
原创 LeetCode - 529. Minesweeper
解题代码:classSolution {public: boolin(vector>& board,int i,int j){ if(i>=0&&i=0&&j else return false; } voidcheck(vector>& board,int i,int j){ if(!in(board,i,
2017-03-15 17:19:10 292
原创 LeetCode - 389. Find the Difference
解题代码:classSolution {public: char findTheDifference(string s, string t){ char result; for(int i=0;i for(int j=0;j if(t[i]==s[j]){
2017-03-10 18:48:15 177
原创 LeetCode - 413. Arithmetic Slices
解题代码:(1)classSolution {public: int numberOfArithmeticSlices(vector&A) { if(A.size() return 0; int count=0; for(int i=0;i int dif=A[i+1]-
2017-03-10 18:38:34 218
原创 LeetCode - 338. Counting Bits
解题代码:classSolution {public: intislandPerimeter(vector>& grid) { int p=0,c=0; for(int i=0;i for(intj=0;j if(grid[i][j]==1){
2017-03-08 18:30:19 151
原创 LeetCode - 338. Counting Bits
解题代码:classSolution {public: vector countBits(int num) { vector result(num+1); result[0]=0; for(int i=1;i int j=i^(i-1); if(j==1){
2017-03-08 16:48:50 189
原创 LeetCode - 496. Next Greater Element I
解题代码:classSolution {public: vectornextGreaterElement(vector& findNums, vector&nums) { vectorresult(findNums.size()); int t,n; for(int i=0;i n=0;
2017-03-06 17:54:35 152
原创 LeetCode - 419. Battleships in a Board
解题代码:classSolution {public: intcountBattleships(vector>& board) { int num=0; int re=0; int i,j; if(board[0].empty()) return 0;
2017-03-03 17:42:30 213
原创 LeetCode - 520. Detect Capital
解题代码:classSolution {public: bool detectCapitalUse(string word) { if (isupper(word[0])){ if(word.size()>2){ if(isupper(word[1])){ fo
2017-03-03 15:01:31 172
原创 LeetCode - 412. Fizz Buzz
解题代码:classSolution {public: vector fizzBuzz(int n) { vector fb(n); for(int i=1;i if(i%3==0){ if(i%5==0) fb[i-1]+="FizzBuzz"
2017-03-03 10:45:15 299
原创 LeetCode - 476. Number Complement
解题代码:intfindComplement(int num) { int com=0; for(int i=0;num>0;num/=2){ if(num%2!=1){ com += pow(2,i); } i++; } return com;} 解题思路:
2017-02-24 17:32:28 496
原创 LeetCode - 461. Hamming Distance
解题代码:inthammingDistance(int x, int y) { int a=x^y; int b=0; for(;a>0;){ if(a%2==1) b++; a/=2; } return b;} 解题思路:题目要求计算两个数字的Hamming Distanc
2017-02-24 16:39:36 218
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人