摘要:Like "Best Time to Buy Stock III" with "Longest continuous subarray"class Solution {public: /** * @param nums: A list of integers * @return...
阅读全文
09 2015 档案
摘要:Here is a BFS solution which is compatible with LeetCode binary tree format.class Solution {public: /** * This method will be invoked first, yo...
阅读全文
摘要:Catch the sparkle: if only one number is duplicated in [1..n], all following numbers will be "shifted" right.class Solution {public: int findDuplic...
阅读全文
摘要:DFS ended up with TLE. Only BFS works./** * Definition for Directed graph. * struct DirectedGraphNode { * int label; * vector neighbors; * ...
阅读全文
摘要:Simply a revise of a genius Greedy algorithm seen on LeetCode - linear walking.class Solution {public: /** * @param matrix: A list of lists of ...
阅读全文
摘要: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...
阅读全文
摘要:A typical swapping.class Solution {public: /** * @param chars: The letters array you should sort. */ bool isUpper(char c) { r...
阅读全文
摘要:class PeekingIterator : public Iterator { bool bPeeked; int val;public: PeekingIterator(const vector& nums) : Iterator(nums) { val = -...
阅读全文
摘要:A variation to Sort Colors I - we take care of 2 sides, recursively\iteratively.class Solution{public: /** * @param colors: A list of integer ...
阅读全文
摘要:class Solution {public: void moveZeroes(vector& nums) { size_t n = nums.size(); if(n < 2) return; int i = 0, j = 0; ...
阅读全文
摘要:class Solution {public: /** *@param n, m: Two integer *@param i, j: Two bit positions *return: An integer */ int updateBits(int ...
阅读全文
摘要:O(nlgn) with repeated numbers.. Please note the extra repeat count array:class Solution {public: /** * @param nums: The integer array * @re...
阅读全文
摘要: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...
阅读全文
摘要:Partial Sort.class Solution {public: /** * @param nums: A list of integers. * @return: An integer denotes the middle number of the array. ...
阅读全文
摘要:Capable to k-vector input too:class ZigzagIterator { int x; int i; int max_x; vector*> l; void moveon() { int oldi= i...
阅读全文
摘要:A variation to Binary Tree iteration.class Solution { bool isSame(TreeNode *p1, TreeNode *p2) { if (!p1 && !p2) return true; if( (...
阅读全文
摘要: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] -...
阅读全文
摘要:Using XOR on bits.class Solution {public: /* * @param a: The first integer * @param b: The second integer * @return: The sum of a and b...
阅读全文
摘要:An intuitive DP.class Solution {public: int numSquares(int n) { vector dp(n + 1, INT_MAX); dp[0] = 0; for(int i = 0; i...
阅读全文
摘要:One-pass Greedy solution. So beautiful.class Solution {public: void wiggleSort(vector& nums) { for(int i = 1; i nums[i]) ...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要:Just take care of corner cases.vector sec3 = { "", "Thousand", "Million", "Billion" };vector sig = { "", "One", "Two", "Three", "Four", "Five", "Six",...
阅读全文

浙公网安备 33010602011771号