This series has no flower head, is pure LeetCode problem breakdown analysis, not with a line or a small group of clever solution, but with clear code and simple enough ideas to help you clarify the meaning of the problem. Let you in the interview no longer afraid of algorithms written tests.
18. Remove duplicates from sort arrays (remove-duplicates-from-sorted-array)
The label
- Double pointer
- How fast a pointer
- simple
The title
Leetcode portal
Let’s just open leetcode. Let’s just open leetcode.
Given an ordered array nums, the elements in the array are deduplicated so that each element in the original array has only one element. Finally, return the length of the array after the reduplication.
The basic idea
- Once the array is sorted, we can place two Pointers
I and j.
Where I is a slow pointer and j is a fast pointer. As long asnums[i] === nums[j]
We increasej
Skip duplicates. - When we meet
nums[j] ! == nums[i]
When the run of skipping duplicates has ended, so we must putnums[j]
Is copied tonums[i + 1]
. - Then incrementing I, and then we’re going to repeat the same process again until j reaches the end of the array.
Writing implement
/ * * *@param {number[]} nums
* @return {number}* /
var removeDuplicates = function(nums) {
/ / sentence
if (nums.length === 0) {
return 0
}
let i = 0
for (j = 1; j < nums.length; j++) {
if(nums[j] ! == nums[i]) { i++ nums[i] = nums[j] } }return i + 1
};
Copy the code
19. Remove-element
The label
- Double pointer
- How fast a pointer
- simple
The title
Leetcode portal
Let’s just open leetcode. Let’s just open leetcode.
Given an array nums and a number val, remove all elements equal to val from the array and return the number of remaining elements. It’s basically the same idea as the last problem.
The basic idea
- I’m going to use two Pointers again
i
和j
, includingi
是Slow pointer.j
是Quick pointer. - So the last problem was skipping the repeating element, and this is an easier one, to see if phi is equal to phi
val
Will do. whennums[j] === val
, increasing by jSkip this element. - As long as
nums[j] ! == val
So let’s just copynums[j]
到nums[i]
并Increments both indexes simultaneously. - Repeat this process until j reaches the end of the array, which has a new length of I.
Writing implement
/ * * *@param {number[]} nums
* @param {number} val
* @return {number}* /
var removeElement = function(nums, val) {
let i = 0
for (j = 0; j < nums.length; j++) {
if(nums[j] ! == val) { nums[i] = nums[j] i++ } }return i
};
Copy the code
20. Division-two-integers
The label
- mathematics
- medium
The title
Leetcode portal
Let’s just open leetcode. Let’s just open leetcode.
Given two integers, dividend and divisor. Divide two numbers without the multiplication, division, and mod operators. Returns the quotient of dividend divided by divisor.
The basic idea
Related information, using the idea of fast power, this is relatively complex to talk about, this is just comb, not much to say. Be interested in reading the documents for yourself.
Writing implement
var divide = function(dividend, divisor) {
/ / symbol
let sign = true
if (dividend < 0) {
dividend = 0- dividend sign = ! sign }if (divisor < 0) {
divisor = 0- divisor sign = ! sign }if (divisor > dividend) {
return 0
}
let [res, mul_divisor] = [1, divisor]
while (dividend > mul_divisor + mul_divisor) {
res += res
mul_divisor += mul_divisor
}
res = res + divide(dividend - mul_divisor, divisor)
if(! sign) { res =0 - res
}
res = res >= 2147483648 ? 2147483647 : res
res = res <= -2147483648 ? -2147483648 : res
return res
};
Copy the code
That’s all for today. If you want to brush the questions with me, you can add my wechat to search my wechat id, infinity_9368. You can chat and add my password “Tianwang Gai Tigeri” in English. Please send me the verification message presious Tower shock the Rever Monster. I will pass it if I see it
reference
- Leetcode-cn.com/problems/re…