自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 [LeetCode] Pow(x,n)

Increase by base square. Note: When n is negative! class Solution { public: double pow(double x, int n) { double ans=1.0; double y=x; bool neg=n<0; unsigned int m=

2014-02-27 12:41:09 335

原创 [LeetCode] Permutations II

Use unordered_set to record if a certain number has been used. class Solution { public: vector > permuteUnique(vector &num) { vector > r; doPermute(num, 0, r); return r;

2014-02-27 10:48:42 386

原创 [LeetCode] Permutations

Recursion with backtracking. class Solution { public: void helper(vector > &result, vector &num, vector cur, vector used, int n) { if(n==num.size()) { result.push_back(cur);

2014-02-27 09:46:39 322

原创 [LeetCode] Generate Parentheses

If there's "(" quota, adding "(" is safe; If there are more "(" than ")", which means quota["("] class Solution { public: void helper(int l, int r, int n, string &sol, vector &result) {

2014-02-27 08:18:34 358

原创 [LeetCode] Binary Tree Level Order Traversal

/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Soluti

2014-02-27 01:32:41 301

转载 [LeetCode] Palindrome Partitioning II

Use DFS will result in TLE! DP: dp[i] denotes the minimum number of palindromic substrings from i->n, isPal[i][j] denotes if string(i,j) is palindromic. Thus we have:  isPal[i][j] = (s[i]==s[j] &&

2014-02-26 20:04:04 369

原创 [LeetCode] Longest Palindromic Substring

Iterate each character as the center, expand to two ends to find the longest palindromic string. class Solution { public: string longestPalindrome(string s) { string result=""; fo

2014-02-26 19:38:46 424

原创 [LeetCode] Palindrome Partition

Recursion with backtracking. class Solution { public: vector> partition(string s) { vector> result; if(s.length()==0) return result; vector cur; parHelper(s,0,resu

2014-02-26 19:18:20 415

原创 [LeetCode] Convert Sorted Array to Binary Search Tree

Each time choose the middle element as the root, recursively build the BST. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *

2014-02-26 12:22:21 367

原创 [LeetCode] Container With Most Water

Scan from two ends to the middle, Area = (j-i)*min(a_i, a_j). class Solution { public: int maxArea(vector &height) { int i=0; int maxA=0; int j=height.size()-1; wh

2014-02-26 12:12:01 340

原创 [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

1. The last element in postorder is the root; 2. Find the root in inorder, the left side belongs to left subtree, the right side belongs to right subtree; 3. Repeat step 1 and 2 on the left side ele

2014-02-26 11:57:08 368

原创 [LeetCode] Merge Intervals

Sort the intervals in ascending order of start. Let the tmp denote the first interval, iterate the later intervals to compare: If start>tmp.start: push tmp into result, replace tmp with the current i

2014-02-26 07:55:28 350

原创 [LeetCode] Median of Two Sorted Arrays

This problem equals to finding the Kth maximum element in two sorted arrays. In order to reach the complexity of O(log(m+n)), we can use binary search recursively. Compare the K/2th elements of A and

2014-02-26 07:18:56 458

原创 [LeetCode] Balanced Binary Tree

The difference of two sub-trees are no greater than 1. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v

2014-02-26 06:45:12 371

原创 [LeetCode] Anagrams

Sort each string as the key to the map.  Bucket sort: class Solution { public: vector anagrams(vector &strs) { map > dict; vector result; for(int i=0;i<strs.size();i++) {

2014-02-26 06:22:48 428

原创 [LeetCode] Two Sum

Very simple. Note the index starts from 1. class Solution { public: vector twoSum(vector &numbers, int target) { vector result; vector num(numbers); int a,b; if(nu

2014-02-26 03:50:52 360

原创 [LeetCode] Add Two Numbers

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

2014-02-26 03:41:05 316

原创 [LeetCode] Add Binary

Add from tail to head class Solution { public: string addBinary(string a, string b) { if(a.length()<b.length()) { string t = a; a = b; b = t; } int carry = 0;

2014-02-26 02:09:16 331

原创 [LeetCode] 4Sum

Instead of iterative way, here I used recursive solution which can be applied to any N Sum problems. class Solution { public: vector > fourSum(vector &num, int target) { vector > res;

2014-02-26 01:26:20 361

原创 [LeetCode] 3Sum Closest

Sort the array, for each a, search the sum of b,c from two ends to the middle, while updating the minimum distance between the target. class Solution { public: int threeSumClosest(vector &num, in

2014-02-26 00:52:48 539

转载 [LeetCode] Sudoku Solver

Backtracking: Similar to Valid Sudoku, iterate each possible value for every blank, check if valid and recursively call the function to check next blank. class Solution { public: bool isValid(vec

2014-02-25 22:02:16 362

转载 [LeetCode] Restore IP Addresses

Use 3 pointers to iterate each possible starting position for each part, then check if each part is valid. NOTE: The definition of "valid IP address"!! class Solution { public: vector restoreIpA

2014-02-25 20:59:06 330

转载 [LeetCode] Max Points on a Line

Build a map for each point to count each different slope will pass how many points. NOTE: There are duplicate points!!! /** * Definition for a point. * struct Point { * int x; * int y;

2014-02-25 11:23:35 398

原创 [LeetCode] Reverse Nodes in k-Group

Reverse two adjacent lists in k elements for k times to reverse the k list nodes. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode

2014-02-25 09:41:16 490

原创 [LeetCode] Next Permutation

Search backwards: when we find the first decending pair of adjacent elements (i.e. num[i-1] class Solution { public: void nextPermutation(vector &num) { if(num.size()<=1) return;

2014-02-25 08:15:04 357

原创 [LeetCode] Valid Sudoku

Check each filled spot if there's collision. class Solution { public: bool isValid(vector > &board, int x, int y) { for(int i=0;i<9;i++) { if(board[x][i]==board[x][y] && i!=y)

2014-02-25 07:31:51 397

原创 [LeetCode] Insertion Sort List

Simply simulate the process of insertion sort. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} *

2014-02-25 06:56:40 484

原创 [LeetCode] Reorder List

Reverse the last(right) half of the linked list and interweave(merge) into the left half. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *

2014-02-25 06:35:57 391

原创 [LeetCode] Evaluate Reverse Polish Notation

Calculate the value using stacks. class Solution { public: int evalRPN(vector &tokens) { stack integers; stack symbols; for(vector::iterator i=tokens.begin();i!=tokens.end

2014-02-25 06:07:30 337

原创 [LeetCode] Sort List

Merge Sort

2014-02-25 05:49:16 607

空空如也

空空如也

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

TA关注的人

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