LeetCode20200409
Posted Julia_AI_123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode20200409相关的知识,希望对你有一定的参考价值。
CanChen
ggchen@mail.ustc.edu.cn
Max Consecutive Ones II
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0.
Most cases will be two consecutive 1s gapped by one 0. For me, I preprocess the binary array and make every consecutive 1s and 0s a single number. To distinguish between 1s and 0s, I make 1s positive and 0s negative. Then I think some corner cases--not the traditional situations, are important. For example, what if we only have 1s or 0s?.
One simple mistake I keep making is not knowing the situation well when breaking in a for loop. I need to pay more attention to whether it is due to for loop or for the breaking condition.
Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all
elements of the matrix in spiral order.
For all coding problems I think the most important thing is to follow my intuition and then simplify it.
In this specific case, just get the outermost rectangle as the first path. Then erase the outermost rectangle, we have a smaller rectangle and do the same thing until we get a point or a line.
Here I define l1,l2,l3 and l4 as four paths. It is worth mentioning that l2 and l4 do not contain the start point and end point. As a result, in the last loop, I need to add the two points.
Be aware of every variable‘s meaning in the whole coding process.
Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself.
This problem is a simple addition method. What I learn from this question is that we can maintain the form of code at the cost of some time.
To be specific, we should put the critical point in the loop if possible. This could avoid some problems sometimes since you do not need to determine whether the length is zero or not.
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
Like I said before, the most important thing is intuition. Here for this problem we should analyse that whether the water at some point could be stored or not is determined the max height of the left and right. This is common sense and with this we can solve the problem easily.
Add Bold Tag in String
Given a string s and a list of strings dict, you need to add a closed pair of bold tag and to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold tag. Also, if two substrings wrapped by bold tags are consecutive, you need to combine them.
My first intution is that we need to determine whether two words in the dict is overlapped or not. Then I find it difficult since three or more words may be overlapped and being overlapped or not also depends on the position.
Finally I found my focus on the words wrong and I should focus the target string. There will be less variants and we just need to get the corresponding intervals and then tells how they are overlapped.
As a result, this becomes the traditional merged interval problem.
At last, we get the merged intervals and the insertion is also kind of tricky since every insertion will change the intervals‘ values. We need to update it.
Read N Characters Given Read4 II - Call multiple times
Given a file and assume that you can only read the file using a given method read4, implement a method read to read n characters. Your method read may be called multiple times.
This problem is hard to understand first and you have to consider many corner cases.
The key to solve this problem is to use cache to store the unconsumed chars from read4. We should be aware of every variable‘s meaning and update the variable when some actions happen.
以上是关于LeetCode20200409的主要内容,如果未能解决你的问题,请参考以下文章
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段
LeetCode810. 黑板异或游戏/455. 分发饼干/剑指Offer 53 - I. 在排序数组中查找数字 I/53 - II. 0~n-1中缺失的数字/54. 二叉搜索树的第k大节点(代码片段