自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 leetcode 321: Create Maximum Number

The algorithm is quite difficult, you need to think step by step to come up with the idea. I learned the algorithm here.class Solution {public: vector maxNumber(vector& nums1, vector& nums2, in

2016-01-18 11:25:57 414

原创 leetcode 273: Integer to English Words

Be careful about many corner cases.class Solution {public: string numberToWords(int num) { string res; vector mp(10); vector mp1(10); mp[0]=""; mp[1]="On

2015-10-12 06:32:48 312

原创 leetcode 109: Convert Sorted List to Binary Search Tree

Use the fast slow method to find the mid point of the list and make it the root. Then do it recursively on both sides./** * Definition for singly-linked list. * struct ListNode { * int val;

2015-09-22 09:51:44 422

原创 leetcode 57: Insert Interval

Loop the array once and merge the newInterval with every intervals. If it cannot be merged into any interval, just insert./** * Definition for an interval. * struct Interval { * int start; *

2015-09-15 08:04:52 245

原创 leetcode 9: Palindrome Number

Reverse the number and compare it with the original one.class Solution {public: bool isPalindrome(int x) { int old_x=x; int new_x=0; while(x>0) { if(

2015-09-02 21:00:20 218

原创 leetcode 8: Sting to Integer (atoi)

class Solution {public: int myAtoi(string str) { int len=str.length(); int i=0; while(i<len&&str[i]==' ') i++; str=str.substr(i);//erase all white spac

2015-09-02 20:41:12 418

原创 leetcode 7: Reverse Integer

class Solution {public: int reverse(int x) { int sign=1; if(x<0) { sign=-1; x=-x; } string s; while(x) {

2015-09-02 12:34:45 239

原创 leetcode 6: ZigZag Conversion

In one line, the first character is s[i], the second is s[i+2*numRows-2-2*i] and the third is s[i+2*numRows-2]. Just be careful with the test cases that numRows==1 and numRows>len.class Solution {p

2015-09-02 12:05:19 267

原创 leetcode 5: Longest Palindrome Substring

Use DP. dp[i][j] means whether the substring i to j is a palindrome.class Solution {public: string longestPalindrome(string s) { int len=s.length(); bool dp[len][len]; i

2015-09-01 22:57:23 319

原创 leetcode 4: Median of Two Sorted Arrays

Use the idea of merge sort.class Solution {public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int m=nums1.size(); int n=nums2.size(); int i=0,j=0,curr

2015-09-01 22:20:55 276

原创 leetcode 3: Longest Substring Without Repeating Characters

Use a window to get a substring. The end of the window keeps forward and once it meets a character that is already in the window, update the start of the window. The unordered map can be replaced by a

2015-09-01 19:04:06 236

原创 leetcode 2: Add Two Numbers

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* ad

2015-09-01 18:41:42 253

原创 leetcode 1: Two Sum

Use a unordered map to save all visited number. While scan the array, find out whether target-nums[i] is already visited.class Solution {public: vector twoSum(vector& nums, int target) {

2015-09-01 18:00:47 235

原创 leetcode 241: Different Ways to Add Parentheses

Learn from https://leetcode.com/discuss/48488/c-4ms-recursive-%26-dp-solution-with-brief-explanation.Recursion method, find every operation character and divide the string according to them.class

2015-09-01 13:37:49 229

原创 leetcode 240: Search a 2D Matirx II

Use binary search for those rows that might contains the target.class Solution {public: bool searchMatrix(vector>& matrix, int target) { if(matrix.empty()) return false;

2015-09-01 11:24:56 282

原创 leetcode 239: Sliding Window Maximum

Use the a deque to save numbers like a waitlist. If the window size equals to k, start the pop operation of the deque and push the front number into the res vector. If nums[i-k] equals to the front nu

2015-09-01 00:19:17 236

原创 leetcode 238: Product of Array Except Self

Learn the idea from https://leetcode.com/discuss/49667/o-n-time-and-o-1-space-c-solution-with-explanation.O(n) time and O(n) space method:Take [1,2,3,4] as an example, fromBegin will be [1,1*1,1*1

2015-08-31 21:39:01 270

原创 leetcode 237: Delete Node in a Linked List

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: void deleteN

2015-08-31 20:47:55 241

原创 leetcode 236: Lowest Common Ancestor of a Binary Tree

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas

2015-08-31 18:21:17 248

原创 leetcode 235: Lowest Common Ancestor of a Binary Search Tree

Use recursion. if none of root, root->left and root->right equal to p or q, return NULL. If left and right are both not NULL, this root is the ancestor. If root equals to p or q, return the root to re

2015-08-31 00:05:41 218

原创 leetcode 234: Palindrome Linked List

To solve it with only O(1) space, you can only find the mid point of the linked list and do some reverse operation, so that you are able to traverse back. To find the mid point, use the fast and slow

2015-08-30 23:27:04 221

原创 leetcode 233: Number of Digit One

This problem is a little hard to understand. You can write some numbers and calculate the number of Digit Ones by hand to find the law. I learned the method from http://www.07net01.com/2015/07/88666

2015-08-30 20:29:48 235

原创 leetcode 232: Implement Queue using Stacks

Use two stacks. Every time I need to pop, I push all numbers in st1 into st2 except the last one, then I pop that one. After that, I push back all numbers into st1. Don't forget to update the front va

2015-08-30 15:10:13 211

原创 leetcode 231: Power of Two

Use shift to determine. Be careful when n is 1 or a negative number.class Solution {public: bool isPowerOfTwo(int n) { if(n<=0) return false; while(n) {

2015-08-30 14:57:27 182

原创 leetcode 230: Kth Smallest Element in a BST

Use the inorder traversal and get all numbers in the BST in order. Then return the kth number./** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; *

2015-08-30 14:13:25 241 1

原创 leetcode 229: Majority Element

Extended from Majority Element. Now there are at most two majority element, so I need four variables to implement the moore voting algorithm.class Solution {public: vector majorityElement(vecto

2015-08-30 12:54:19 200

原创 leetcode 228: Summary Ranges

Use a window to keep track of all continuous numbers.class Solution {public: vector summaryRanges(vector& nums) { vector res; int n=nums.size(); if(n==0) ret

2015-08-30 12:06:54 173

原创 leetcode 227: Basic Calculator II

Use two deques to calculate from both sides.class Solution {public: int calculate(string s) { deque nums; deque op; int len=s.length(); int i=0; while(i<

2015-08-30 11:40:52 198

原创 leetcode 226: Invert Binary Tree

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas

2015-08-30 10:50:33 185

原创 leetcode 224: Basic Calculator

My method is a little verbose. The hardest part is how to handle if the '-' is minus or a negative sign.class Solution {public: int calculate(string s) { stack st; int len=s.le

2015-08-29 23:47:09 249

原创 leetcode 223: Rectangle Area

There are just two conditions that two rectangles can intersect or not. Draw out all possibilities and you can find out the solution.class Solution {public: int computeArea(int A, int B, int C,

2015-08-29 19:21:10 217

原创 Leetcode 222: Count Complete Tree Nodes

Do only the recursion will cause TLE. Thus, at each node, count the heights of its left subtree and right subtree, if left height equals to the right height, the number of nodes of this root can be ca

2015-08-29 16:51:50 213

原创 leetcode 221: Maximal Square

Use DP, and dp[i][j] means at matrix[i][j] the maximal square's size. First, initialize the first row and column which is obvious. Then you need to find the minimum number among dp[i-1][j-1], dp[i-1][

2015-08-29 16:15:59 202

原创 leetcode 220: Contains Duplicate III

Use set to find numbers with time complexity of O(n*logk). Learned this from http://www.cnblogs.com/easonliu/p/4544073.html.class Solution {public: bool containsNearbyAlmostDuplicate(vector& nu

2015-08-29 15:04:14 262

原创 leetcode 219: Contains Duplicate II

Use the unordered map to save the number and its index. And the rest is easy.class Solution {public: bool containsNearbyDuplicate(vector& nums, int k) { unordered_map mp; int n=

2015-08-29 00:29:16 212

原创 leetcode 217: Contains Duplicate

class Solution {public: bool containsDuplicate(vector& nums) { unordered_set set; int n=nums.size(); for(int i=0;i<n;i++) { if(set.find(nums[i])!=set.e

2015-08-28 16:10:23 210

原创 leetcode 216: Combination Sum III

class Solution {public: vector> combinationSum3(int k, int n) { vector > res; vector set; helper(k,n,1,set,res); return res; } void helper(int k,int n,int

2015-08-28 15:58:08 217

原创 leetcode 215: Kth Largest Element in an Array

Use quick sort. I am not sure why the run time of my recursive quick sort is 8ms. Maybe if I do it iteratively, the run time can drop to 4ms.class Solution {public: int findKthLargest(vector& n

2015-08-28 11:05:45 170

转载 leetcode 214: Shortest Palindrome

Use KMP algorithm. First, get the reverse of the string s called new_s, and add s, a special character ('#') and new_s together. Word out the next array on this new_s. Then next[len] will give the lon

2015-08-27 23:35:07 265

原创 leetcode 213: House Robber II

Do the DP two times, one time starts from house 0 to house n-2 and one time starts from house 1 to house n-1. Return the maximum result.class Solution {public: int rob(vector& nums) { i

2015-08-27 17:14:35 234

空空如也

空空如也

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

TA关注的人

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