Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

09 2015 档案

摘要:Like "Best Time to Buy Stock III" with "Longest continuous subarray"class Solution {public: /** * @param nums: A list of integers * @return... 阅读全文
posted @ 2015-09-30 14:41 Tonix 阅读(179) 评论(0) 推荐(0)

摘要:Here is a BFS solution which is compatible with LeetCode binary tree format.class Solution {public: /** * This method will be invoked first, yo... 阅读全文
posted @ 2015-09-30 13:45 Tonix 阅读(179) 评论(0) 推荐(0)

摘要:Catch the sparkle: if only one number is duplicated in [1..n], all following numbers will be "shifted" right.class Solution {public: int findDuplic... 阅读全文
posted @ 2015-09-29 23:51 Tonix 阅读(138) 评论(0) 推荐(0)

摘要:DFS ended up with TLE. Only BFS works./** * Definition for Directed graph. * struct DirectedGraphNode { * int label; * vector neighbors; * ... 阅读全文
posted @ 2015-09-24 05:14 Tonix 阅读(249) 评论(0) 推荐(0)

摘要:Simply a revise of a genius Greedy algorithm seen on LeetCode - linear walking.class Solution {public: /** * @param matrix: A list of lists of ... 阅读全文
posted @ 2015-09-23 05:19 Tonix 阅读(139) 评论(0) 推荐(0)

摘要:Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known equation: sum[i..j] = sum[0..j] - sum[0..i]. And pls... 阅读全文
posted @ 2015-09-23 04:34 Tonix 阅读(239) 评论(0) 推荐(0)

摘要:A typical swapping.class Solution {public: /** * @param chars: The letters array you should sort. */ bool isUpper(char c) { r... 阅读全文
posted @ 2015-09-22 13:35 Tonix 阅读(134) 评论(0) 推荐(0)

摘要:class PeekingIterator : public Iterator { bool bPeeked; int val;public: PeekingIterator(const vector& nums) : Iterator(nums) { val = -... 阅读全文
posted @ 2015-09-21 15:27 Tonix 阅读(139) 评论(0) 推荐(0)

摘要:A variation to Sort Colors I - we take care of 2 sides, recursively\iteratively.class Solution{public: /** * @param colors: A list of integer ... 阅读全文
posted @ 2015-09-20 15:48 Tonix 阅读(155) 评论(0) 推荐(0)

摘要:class Solution {public: void moveZeroes(vector& nums) { size_t n = nums.size(); if(n < 2) return; int i = 0, j = 0; ... 阅读全文
posted @ 2015-09-20 04:31 Tonix 阅读(105) 评论(0) 推荐(0)

摘要:class Solution {public: /** *@param n, m: Two integer *@param i, j: Two bit positions *return: An integer */ int updateBits(int ... 阅读全文
posted @ 2015-09-18 14:02 Tonix 阅读(173) 评论(0) 推荐(0)

摘要:O(nlgn) with repeated numbers.. Please note the extra repeat count array:class Solution {public: /** * @param nums: The integer array * @re... 阅读全文
posted @ 2015-09-18 03:12 Tonix 阅读(146) 评论(0) 推荐(0)

摘要:A simple variation to 0-1 Knapsack.class Solution {public: /** * @param m: An integer m denotes the size of a backpack * @param A: Given n it... 阅读全文
posted @ 2015-09-15 10:47 Tonix 阅读(132) 评论(0) 推荐(0)

摘要:Partial Sort.class Solution {public: /** * @param nums: A list of integers. * @return: An integer denotes the middle number of the array. ... 阅读全文
posted @ 2015-09-15 05:24 Tonix 阅读(214) 评论(0) 推荐(0)

摘要:Capable to k-vector input too:class ZigzagIterator { int x; int i; int max_x; vector*> l; void moveon() { int oldi= i... 阅读全文
posted @ 2015-09-15 03:55 Tonix 阅读(155) 评论(0) 推荐(0)

摘要:A variation to Binary Tree iteration.class Solution { bool isSame(TreeNode *p1, TreeNode *p2) { if (!p1 && !p2) return true; if( (... 阅读全文
posted @ 2015-09-13 14:07 Tonix 阅读(201) 评论(0) 推荐(0)

摘要:Typical solution: convert it to Maximum Array problem.And here is my solution: O(n) by using Greedy strategy on this equation: sum[i..j] = sum[0..j] -... 阅读全文
posted @ 2015-09-13 13:47 Tonix 阅读(185) 评论(0) 推荐(0)

摘要:Using XOR on bits.class Solution {public: /* * @param a: The first integer * @param b: The second integer * @return: The sum of a and b... 阅读全文
posted @ 2015-09-13 13:00 Tonix 阅读(164) 评论(0) 推荐(0)

摘要:An intuitive DP.class Solution {public: int numSquares(int n) { vector dp(n + 1, INT_MAX); dp[0] = 0; for(int i = 0; i... 阅读全文
posted @ 2015-09-12 08:43 Tonix 阅读(143) 评论(0) 推荐(0)

摘要:One-pass Greedy solution. So beautiful.class Solution {public: void wiggleSort(vector& nums) { for(int i = 1; i nums[i]) ... 阅读全文
posted @ 2015-09-11 02:08 Tonix 阅读(243) 评论(0) 推荐(0)

摘要:Lesson learnt: Get rid of XX algorithm routines clogging your mind and focus\enjoy the problem itself!// Forward declaration of the knows API.bool kno... 阅读全文
posted @ 2015-09-11 00:48 Tonix 阅读(130) 评论(0) 推荐(0)

摘要:sum[i..j] = sum[0..j] - sum[0..i-1]. We use a hashmap to check a previous matching index with a given number.class Solution {public: vector subarra... 阅读全文
posted @ 2015-09-04 06:02 Tonix 阅读(178) 评论(0) 推荐(0)

摘要:Sorting is a natural solution. But, you don't have to run O(nlgn) sorting for all the time. Counting sort is O(n)!class Solution {public: int hInde... 阅读全文
posted @ 2015-09-04 04:58 Tonix 阅读(180) 评论(0) 推荐(0)

摘要:I made it too complicated first.. It is really simply if full-tree in-order traversal is allowed.class Solution { int inx, sofar; // closest boo... 阅读全文
posted @ 2015-09-02 11:24 Tonix 阅读(258) 评论(0) 推荐(0)

摘要:Just take care of corner cases.vector sec3 = { "", "Thousand", "Million", "Billion" };vector sig = { "", "One", "Two", "Three", "Four", "Five", "Six",... 阅读全文
posted @ 2015-09-01 13:45 Tonix 阅读(159) 评论(0) 推荐(0)