随笔分类 - LeetCode Solutions
Solutions to problems on https://leetcode.com/problemset/algorithms/.
摘要:Again, a classic interview question of linked list. Similar to Linked List Cycle, we maintain two pointers fast and slow: fastmove two steps at one ti...
阅读全文
摘要:A classic interview question of linked list. The idea is to maintain two pointers, one move one step at a time and the other move two steps at a time....
阅读全文
摘要:This problem has a long story. There are just too many solutions on the web and it can be studied for a long time before you fully grasp it. Morever, ...
阅读全文
摘要:This is a classic problem for hash table. The basic idea is to maintain a hash table for each element innums, using the element as key and its index (...
阅读全文
摘要:Well, this problem looks easy at first glance. However, to get a bug-free code may be not that easy.The total square is simply equal to the sum of the...
阅读全文
摘要:Well, there many ways to solve this problem. Let's first look at a naive solution.The basic idea is simple. Starting from the first character of the s...
阅读全文
摘要:The idea is to usetwo stacks.Forpush, the first stack records the pushed elements and the second stack recordsallthe minimum (including duplicates) th...
阅读全文
摘要:This problem is an application of the Trie data structure. In the following, it is assumed that you have solvedImplement Trie (Prefix Tree).Now, let's...
阅读全文
摘要:Well, the problem does not aim for an advanced algorithm like KMP but only a clean brute-force algorithm. So we can traverse all the possible starting...
阅读全文
摘要:In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations. So, what else ca...
阅读全文
摘要:To readncharacters, we first callread4forn / 4times. For example, if we want to read10characters, we will read them in the8 (4 * 2) + 2manner by first...
阅读全文
摘要:Problem Description:Design and implement a TwoSum class. It should support the following operations:addandfind.add- Add the number to an internal data...
阅读全文
摘要:The problem has a nice structure that backtracking naturally fits in. The structure is, given a starting positionidx, we search fromidxtill the end of...
阅读全文
摘要:This problem is somewhat tricky at first glance. However, the final implementation is fairly simple using recursion.The basic idea is, visiting every ...
阅读全文
摘要:Well, this problem desires for the use of dynamic programming. They key to any DP problem is to come up with the state equation. In this problem, we d...
阅读全文
摘要:Recursive (Backtracking)This is a typical problem that can be tackled by backtracking.Since backtracking has a more-or-less similar template, so I do ...
阅读全文
摘要:This problem is similar toBest Time to Buy and Sell Stock. Givenprices, we find the day (denoted asbuy) of the first local minimum and the day (denote...
阅读全文
摘要:This post shares a very simple solution, whose code is rewritten below, just 5 lines :-) 1 class Solution { 2 public: 3 int maxProfit(vector& pric...
阅读全文
摘要:This problem seems to be tricky at first glance. However, if you know Morris traversal, it is just the preorder case of Morris traversal and the code ...
阅读全文
摘要:There are many merge-sort solutions at the forum, but very few quicksort solutions. So I post my accepted quicksort solution here.Well, after reading ...
阅读全文

浙公网安备 33010602011771号