- 博客(141)
- 收藏
- 关注
原创 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 261
原创 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 233
原创 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 238
原创 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 207
原创 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 217
原创 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 231
原创 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 208
原创 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 179
原创 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 232 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 196
原创 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 170
原创 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 195
原创 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 181
原创 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 245
原创 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 212
原创 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 210
原创 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 198
原创 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 258
原创 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 209
原创 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 205
原创 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 214
原创 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 167
转载 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 258
原创 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 229
原创 leetcode 212: Word Search II
Use the data structure Trie to do pruning while DFS. But you need to be careful about the way you implement the Trie. I tried the map to save all connections and got a TLE. So I then used TrieNode* ne
2015-08-27 13:42:33 283
原创 leetcode 211: Add and Search Word - Data Structure Design
Use the method in Implement Trie, and modify the search function into the DFS method to check '.' character.struct DictNode { char c; bool isword; unordered_map next; DictNode() {
2015-08-27 12:02:26 318
原创 leetcode 210: Course Schedule II
The algorithm is the same with Course Schedule. Just to save all courses in the topological order.class Solution {public: vector findOrder(int numCourses, vector>& prerequisites) { int
2015-08-27 11:16:47 251
原创 leetcode 209: Minimum Size Subarray Sum
Setting a window on the array. Loop the end of the window, whenever a sum of all numbers in the window is found bigger than or equal to s, shrink that window from the start side to make the window siz
2015-08-26 23:13:39 255
原创 leetcode 208: Implement Trie (Prefix Tree)
A small trick is many words may be a substring of longer words. So I need a boolean variable to mark whether a node is the end of one word.class TrieNode {public: // Initialize your data struct
2015-08-26 22:04:09 216
原创 leetcode 207: Course Schedule
To solve the problem, we need to understand the problem in another way, which is whether there is a cycle in a directed graph. The algorithm we can use is Topological Sort.First we need to set up an
2015-08-26 20:31:17 189
原创 leetcode 205: Isomorphic Strings
Use two maps to save the relationships from s[i] to t[i] and from t[i] to s[i]. Make sure there is no duplicate.class Solution {public: bool isIsomorphic(string s, string t) { unordered
2015-08-26 15:31:52 196
原创 leetcode 204: Count Primes
The hint of the problem illustrate the solution very well.class Solution {public: int countPrimes(int n) { vector isPrime(n,1); for(int i=2;i*i<n;i++) for(int j=i*i;
2015-08-26 13:15:28 187
原创 leetcode 203: Remove Linked List Elements
Create a dummy head before head. The rest is easy./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {}
2015-08-26 12:26:48 225
原创 leetcode 202: Happy Number
Use a set to save all visited numbers. If the new number is already in the set, then it will not end. And if the new number is 1, return true.class Solution {public: bool isHappy(int n) {
2015-08-26 12:20:50 262
转载 leetcode 201: Bitwise AND of Numbers Range
Use n&n-1 to change the last "bit 1" of n to "bit 0". Keep doing it until the n is not in the original range. Learned from http://blog.csdn.net/lu597203933/article/details/44811049.class Solution {
2015-08-26 00:21:16 204
原创 leetcode 200: Number of Islands
Scan the whole graph, if a '1' is found, do BFS and change all '1's connected to that '1' to '0'.class Solution {public: int numIslands(vector>& grid) { if(grid.empty()) ret
2015-08-25 23:06:05 239
原创 leetcode 199: Binary Tree Right Side View
Use Binary Tree Level Order Traversal. During the traversal, update the right most number of that level./** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode
2015-08-25 22:33:02 204
原创 leetcode 198: House Robber
Use DP obviously. dp[i] means the maximum money robbed at house i. Since the adjacent houses cannot be robbed at the same time, dp[i] depends on dp[i-2] and dp[i-3]. Why dp[i-3] is concerned? Because
2015-08-25 21:50:00 195
原创 leetcode 190: Reverse Bits
At the beginning, I used queue to save all bits of the original number and then calculate the new number while popping. But then I realized it is not necessary to use the queue.class Solution {publ
2015-08-25 11:40:55 292
原创 leetcode 189: Rotate Array
Use queue to save the numbers need to be rotated. The problem means to rotate to the right direction, but I do it to the left direction, so I change k to n-(k%n).class Solution {public: void ro
2015-08-25 09:56:26 185
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人