HashMap
The principle of hashmap will not be described here. For those who don't know, you can read this article. Hash and HashMap
The introduction of the hashmap data structure can help us reduce the O(n) time complexity to O(1) time complexity, at the cost of using O(n) space complexity. This looks like half the effort. But if our original time complexity is O(n^2), after using hashmap, the time complexity becomes O(n), but only the space complexity becomes O(n), then it is still very cost-effective.
To deduct the first question, the sum of two numbers:
If we do it with a pure 2D traversal
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
return new int[0];
}
The reason for the high time of the first method is that for each i traversed at the first level, we need to traverse the array again to find target - i.
If we use the hashmap to store the array element value and the corresponding subscript into the hashmap, we can directly obtain the subscript value corresponding to target - i without the need for a second traversal.
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
The actual problem encountered is the sum of three numbers, give an array and a target value, find three numbers in this array and add them to the target value, if found, return true, if not, return false. The sum of three numbers can use hashmap to reduce the three-level loop to two-level loop, and the others are similar to the sum of two numbers.
dynamic programming
The basic idea is to decompose the problem to be solved into several sub-problems, solve the sub-problems first, and then obtain the solution of the original problem from the solutions of these sub-problems.
Written test questions of a certain sound company, adapted from the original question
The idea is to sequentially determine whether the i-th position can be reached. The requirement to reach the i-th position is to be able to reach the j-th position (i>j) and to jump directly from the j-th position to the i-th position.
public boolean canJump(int[] nums) {
// 边界判断
if (nums.length == 1) {
return true;
}
boolean[] dp = new boolean[nums.length];
int i;
// 从第1个下标可以直接到达的地方标记为可到达。
for (i = 0; i < nums[0] + 1 && i < nums.length; i++) {
dp[i] = true;
}
// 其余标记为不可达
for (; i < nums.length; i++) {
dp[i] = false;
}
// 对于每一个位置i,确定是否能能到达第j个位置(i>j)并能从第j个位置直接跳跃至第i个位置
for (i = nums[0] + 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && j + nums[j] >= i) {
dp[i] = true;
break;
}
}
}
return dp[nums.length -1];
}
Of course, this is just a way of thinking, and there are faster and better ways to solve this problem.
Likou Question 139: Word Splitting
The same idea when encountering the problem, but you can use the hashmap to judge whether a word is in the array.
This question can also be implemented using a dictionary tree, and interested friends can search what is a dictionary tree.
how are you
Basic idea: The greedy algorithm does not consider the overall optimality, and the choice it makes is only a local optimal solution in a sense.
A company interview question, adapted from the original question:
It seems very simple, but I didn't think of the optimal solution when I thought about it. Because the number of days to buy must be less than the number of days to sell, then the price of days to buy must be the minimum of all prices from days of sell to the first day. The idea of solving the problem is to assume that the i-th day is the day of selling, find the price of the i-th day's previous minimum price, and maintain the maximum profit.
public int maxProfit(int[] prices) {
if (prices.length <= 1)
return 0;
int min = prices[0], profit = 0;
for (int i = 0; i < prices.length; i++) {
min = Math.min(min, prices[i]);
profit = Math.max(profit, prices[i] - min);
}
return profit;
}
other
The core of a certain tone written test is to judge whether it is a double-ended queue, that is, a queue can only be entered from both ends. Put n numbers at a time, and give an array to determine whether it is a double-ended queue.
6 3 1 2 4 5 7 8
During the written test, I thought it was complicated. Later, I saw that it was actually finding the minimum number, and then judging whether the minimum number was an increasing sequence from the beginning to the end. In fact, I did similar multiple-choice questions when I was learning data structures, but no code implementation was required. It is important for the usual ideas to be implemented into the code.
Test-taking skills
I saw on the Internet that the pass rate of the algorithm question * the score of this question is the final score. For some questions that output True or False, you can directly output the result, or you can output 3 or 4 or 5 directly for the questions that output the minimum number of times.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。