码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode20200409

时间:2020-04-10 00:08:30      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:date   ant   asi   pat   view   cond   ati   out   tag   

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

标签:date   ant   asi   pat   view   cond   ati   out   tag   

原文地址:https://www.cnblogs.com/JuliaAI123/p/12670592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!