随笔分类 - LeetCode Solutions
Solutions to problems on https://leetcode.com/problemset/algorithms/.
摘要:1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int val) { 8 * ...
阅读全文
摘要:Well, life gets difficult pretty soon whenever the same operation on array is transferred to linked list.First, a quick recap of insertion sort:Start ...
阅读全文
摘要:Well, to compute the number of trailing zeros, we need to first think clear about what will generate a trailing0? Obviously, a number multiplied by10w...
阅读全文
摘要:Well, the basic idea is very simple. Start from the tail ofsand move backwards to find the first non-space character. Then from this character, move b...
阅读全文
摘要:Well, this problem is spiritually similar to toCourse Schedule. You only need to store the nodes in the order you visit into a vector during BFS or DF...
阅读全文
摘要:As suggested by the hints, this problem is equivalent to detecting a cycle in the graph represented byprerequisites. Both BFS and DFS can be used to s...
阅读全文
摘要:Problem Description:Given two strings S and T, determine if they are both one edit distance apart.To solve this problem, you first need to know what i...
阅读全文
摘要:This problem is similar to Missing Ranges and easier than that one.The idea is to use two pointers to find the beginning and end of a range and then p...
阅读全文
摘要:Well, the key to this problem is on how to identify the recurring parts. After doing some examples using pen and paper, you may find that for the deci...
阅读全文
摘要:The problem statement has stated that there are bothO(n)andO(nlogn)solutions to this problem. Let's see theO(n)solution first (taken fromthis link), w...
阅读全文
摘要:Problem Description:Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string d...
阅读全文
摘要:This link has a great discussion about this problem. You may refer to it if you like. In fact, the idea and code in this passage is from the former li...
阅读全文
摘要:Well, this problem has a nice BFS structure.Let's see the example in the problem statement.start = "hit"end = "cog"dict = ["hot", "dot", "dog", "lot",...
阅读全文
摘要:This problem is not quite difficult (a typical BFS traversal of graph), though, its aceptance rate is relatively low. In fact, the key obstacle in pas...
阅读全文
摘要:This problem is an application of graph traversal, which has two systematic methods:Bread-First Search (BFS)andDepth-First Search (DFS). In the follow...
阅读全文
摘要:To solve this problem, some observations have to be made first.Let's first see two relatively easy observations.To maximize the probability that we ca...
阅读全文
摘要:This is a typical problem aboutsearching. In fact, you can use either BFS or DFS for it. Personally, I use BFS because I think it is more intuitive an...
阅读全文
摘要:The basic idea of is as follows:Maintain a dequeoperandsfor the numbers and another dequeoperationsfor the operators+, -, *,/`.Scan the expression fro...
阅读全文
摘要:Well, a typical backtracking problem. Make sure you are clear with the following three problems:What is a partial solution and when is it finished? --...
阅读全文
摘要:Well, a typical backtracking problem. Make sure you are clear with the following three problems:What is a partial solution and when is it finished? --...
阅读全文

浙公网安备 33010602011771号