
leetcode
wishingweed
这个作者很懒,什么都没留下…
展开
-
Implement Queue using Stacks leetcode
This is really a classic problem, no more word need for this.class MyQueue { Stack stack1 = new Stack(); Stack stack2 = new Stack(); // Push element x to the back of queue. public void push原创 2015-10-30 15:39:30 · 314 阅读 · 0 评论 -
leetcode java Best Time to Buy and Sell Stock
public class Solution { public int maxProfit(int[] prices) { if(prices.length == 0) return 0; int min_price = prices[0]; int max_profit = 0; for(int i原创 2014-10-22 11:05:09 · 407 阅读 · 0 评论 -
leetcode c++ get combination sum
this problem is a little hard.we can use the dfs to solve this problem.when you push_back a num and if the sum is原创 2014-11-07 22:35:56 · 415 阅读 · 0 评论 -
leetcode c++ combination sumII
这个题目有几点值得要提的。第一,i++这个表达式的值时i并不是i原创 2014-11-08 00:31:29 · 586 阅读 · 0 评论 -
leetcode c++ jump game 2
I declare two para ; boundary start and boundary end and for each step I原创 2014-11-10 17:19:36 · 560 阅读 · 0 评论 -
leetcode subsets c++
dfs needs more practice.class Solution {public: vector > subsets(vector &S) { vector > answer; vector cur; answer.push_back(cur); if(S.empty()) return answer; sort(S.begin(),S.原创 2014-11-12 17:57:30 · 608 阅读 · 0 评论 -
c++ leetcode generate parentheses
we can just use dfs to solve this problem.原创 2014-11-17 13:53:48 · 463 阅读 · 0 评论 -
pow(x,n) leetcod c++
class Solution {public: double pow(double x, int n) { if(n == 0) return 1.0; if(n { if(n == INT_MIN) return 1.0/pow(x,INT_M原创 2014-11-17 22:03:56 · 412 阅读 · 0 评论 -
permutation sequence leetcode c++
这个问题有点困难,参考了网上的代码。。。还需要继续n转载 2014-11-18 17:08:02 · 519 阅读 · 0 评论 -
REMOVE NTH NODE FROM END OF LIST
DRAW A PICTURE AND Use two pointers.it is ea原创 2014-11-18 18:10:02 · 405 阅读 · 0 评论 -
Unique Binary Search Trees II latched
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *///the proble原创 2014-10-22 22:49:29 · 442 阅读 · 0 评论 -
count and say leetcode c++
pay attention to n and the string.it is easy to understand the code below.class Solution {public: string countAndSay(int n) { // Start typing your C/C++ solution below // DO原创 2014-12-02 23:40:41 · 517 阅读 · 0 评论 -
move zeros leetcode
Just use the thought of Bubble sort.I think it would have an O(n) algriothm, I would try it later.public class Solution { public void moveZeroes(int[] nums) { for(int j = 0;j < nums.length原创 2015-10-30 16:50:29 · 488 阅读 · 0 评论 -
dupicateII leetcode
map是一个接口而hashmap是它的一种实现方式for this problem, if you use eclipse,you will find that we need a () after the fucthion "new";another should be pay attention is when I use eclipse as the edcitior原创 2015-10-25 23:28:07 · 388 阅读 · 0 评论 -
Contains Duplicate leetcode
easy problempublic class Solution { public boolean containsDuplicate(int[] nums) { if(nums.length<2) return false; Arrays.sort(nums); for(int i = 1;i<nums.lengt原创 2015-10-25 22:55:31 · 345 阅读 · 0 评论 -
leetcode contains duplicate III
基本就是遍历的过程中不断的用hash表进行判断,然后不断的维持着这个仍然在范围内的集合。class Solution {public: bool containsNearbyAlmostDuplicate(vector& nums, int k, int t) { map m; int j = 0; for (int i = 0; i转载 2015-10-26 21:22:04 · 347 阅读 · 0 评论 -
construct binary tree from preorder and inorder
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2015-01-19 22:11:43 · 392 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2015-01-19 21:29:13 · 410 阅读 · 0 评论 -
pascal's triangle II leetcode c++
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?for this原创 2015-01-17 16:35:34 · 632 阅读 · 0 评论 -
leetcode triangle c++
class Solution {public: int minimumTotal(vector > &triangle) { if(triangle.size() == 0) return 0; vector f(triangle.size()); f[0] = triangle[0][0];原创 2015-01-17 16:07:38 · 501 阅读 · 0 评论 -
leetcode gas station
转载自felix博客园原题如下Gas StationThere are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[转载 2014-12-04 14:00:08 · 478 阅读 · 0 评论 -
intersection of two linked list|leetcode c++
about this problem,1.calculate the precise length of the two linked list.2.calculate the difference value of two length of the linked list.then move the head node difference value of steps behin原创 2014-12-01 14:14:32 · 437 阅读 · 0 评论 -
leetcode search a 2d matrix
this algorithm just use two times of binary search, first in the col direction, to原创 2014-11-12 16:59:30 · 358 阅读 · 0 评论 -
leetcode java Unique Paths II
LeetCode OJProblemsPick One!SubmissionsDiscussDonate hqluo11Unique Paths II Total Accepted: 17536 Total Submissions: 62770 My Submissions Question Solution Follow up for "Unique Paths":Now原创 2014-10-22 12:55:07 · 491 阅读 · 0 评论 -
Minimum Path Sum java
//注意下,二维数组长度的使用方法public class Solution { public int minPathSum(int[][] grid) { if(grid == null || grid.length == 0) return -1; for(int i = 1;i<grid.length;i++) grid[i][0]原创 2014-10-22 10:05:16 · 479 阅读 · 0 评论 -
Path sum II |leetcode c++
I use iterate method to solve this problem. 1.I use the h原创 2014-10-27 14:33:03 · 493 阅读 · 0 评论 -
Path sum
new 和直接定义的区别new在堆上,需要自己释放内存空间。原创 2014-10-26 00:32:27 · 405 阅读 · 0 评论 -
Recover Binary Search Tree leetcode c++
this problem have two situations.1.when mistake1 is not原创 2014-11-14 16:23:33 · 445 阅读 · 0 评论 -
leetcode valid sudoku c++
this problem just consider whether this matrix is the right matrix.we just consider each col,each row and ea原创 2014-11-14 22:59:12 · 426 阅读 · 0 评论 -
anagrams c++ leetcode
this problem uses map;1.map anagrams; then we can use anagram[s]原创 2014-11-15 11:40:29 · 496 阅读 · 0 评论 -
add two numbers
for save space, I write a really ugly code here./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} *原创 2014-11-17 16:58:35 · 376 阅读 · 0 评论 -
add binary
class Solution {public: string addBinary(string a, string b) { int lengtha = a.length(); int lengthb = b.length(); int length = 0; if(lengthb > lengtha) length = lengtha;原创 2014-11-17 23:48:16 · 421 阅读 · 0 评论 -
leetcode java unique binary search tree
#include class Solution {public: int numTrees(int n) { vector dp(n+1); dp[0] = dp[1] = 1; for(int i=2;i<=n;i++) { dp[i] = 0; for(int j=0;j原创 2014-10-22 13:08:39 · 427 阅读 · 0 评论 -
leetcode Binary Tree Level Order Traversal
这个题目里面出现了一个比较大的问题就是二维的vector没有初始化的时候你如果使用它就会出现原创 2014-10-23 16:29:14 · 435 阅读 · 0 评论 -
the latest update validate Binary Search leetcode c++
leetcode updates the test set of this problem.so that we must原创 2014-11-14 13:50:13 · 423 阅读 · 0 评论 -
linked list cycle II leetcode c++
we ues two pointers to solve this problem.the fast pointer moves two steps each timethe slow pointer moves 1 steps each t原创 2014-11-18 17:40:48 · 460 阅读 · 0 评论 -
leetcode partition List c++
要注意循环链表的情况,如果big在前,small在ho原创 2014-11-19 14:57:59 · 902 阅读 · 0 评论 -
valid Palindrome | Leetcode c++
for this problem , you just need to use two pointers to judge whether it is a palindorome原创 2014-11-20 13:50:22 · 435 阅读 · 0 评论 -
Binary Tree Maximum Path Sum Leetcod c++
I use the iterate function to solve this problem.原创 2014-10-25 20:53:19 · 805 阅读 · 0 评论 -
leetcode Sort Colors java
public class Solution { public void sortColors(int[] A) { int r = 0;int b = A.length-1;int temp = 0; for(int i = 0;i { if(A[i]==0 && r {原创 2014-10-22 00:25:27 · 470 阅读 · 0 评论