The sum of two numbers (Question number 1)

The title

Given an integer array nums and an integer target, find the two integers in the array whose sum is the target value and return their array indices.

You can assume that there is only one answer for each input. However, you cannot use the same element in an array twice.

You can return the answers in any order.

Example 1: input: nums = [2,7,11,15], target = 9 output: [0,1] description: because nums[0] + nums[1] == 9, [0,1] is returned.

Example 2: input: nums = [3,2,4], target = 6 output: [1,2]

Example 3: input: nums = [3,3], target = 6 output: [0,1]

Tip: 2 <= nums.length <= 103-109 <= nums[I] <= 109-109 <= target <= 109 only one valid answer can exist

link

Leetcode-cn.com/problems/tw…

explain

To start looking at algorithms, LeetCode is essential.

To brush LeetCode, naturally start with the first question.

This is a simple problem, if you think of that method is very easy, if you think of it, you may not think of it.

If you want to find two numbers whose sum is target, you can find the difference between target and a number. If you have a number in the difference, you can find the difference index and the current index.

And pay attention to the description of the topic:

There can only be one valid answer

There is no such thing as no answer, no special case, very simple.

Your own answers (array)

var twoSum = function(nums, target) {
  var arr = []
  for (let i = 0; i < nums.length; i++) {
    var res = arr.findIndex(item => item === nums[i])
    if (res > -1) return [res, i]
    arr[i] = target - nums[i]    
  }
};
Copy the code

Here we use the array’s findIndex() method, because we are not sensitive to the actual value, we only care about its index.

Your own answers (Map)

Maps perform better, since they can read and write faster than arrays and require no write criteria.

var twoSum = function(nums, target) {
  var group = new Map()
  for (let i = 0; i < nums.length; i++) {
    if (group.has(nums[i])) return [group.get(nums[i]), i]
    group.set(target - nums[i], i)
  }
};
Copy the code

That’s the answer. It’s very clear and easy to understand

The first step is to store the hash table in an array or Map, and then look up the correct answer in the table. I looked at some other answers, JavaScript is basically this solution, if there is a better way to feel welcome to the comments section

A better way

There is no

Integer inversion (Question Number 7)

The title

Give you a 32-bit signed integer x and return the result of reversing the numeric portion of x.

If the reversed integer exceeds the range of 32 bit signed integers [−231, 231 − 1], 0 is returned.

Assume that the environment does not allow the storage of 64-bit integers (signed or unsigned).

Example 1: Input: x = 123 Output: 321

Example 2: Input: x = -123 Output: -321

Example 3: Input: x = 120 Output: 21

Example 4: Input: x = 0 Output: 0

Tip: -231 <= x <= 231 -1

link

Leetcode-cn.com/problems/re…

explain

The first time I looked at this problem, I figured out how to do it, and yes, it works, but there’s another way to do it.

Your own answers (classic array)

var reverse = function(x) {
  var num = +Math.abs(x).toString().split('').reverse().join('')
  if (num > Math.pow(2, 31) - 1 || num < Math.pow(-2, 31)) return 0
  return x < 0 ? 0 - +num : +num
};
Copy the code

To be honest, the performance is not worse than the other one, can be said to be on par.

Execution time: 88 ms, beating 96.37% of all JavaScript submissions

Memory consumption: 39 MB, beating 96.66% of all JavaScript submissions

Better method (bit operation)

Var reverse = function(x) {var reverse = 0 while (x) { If (math.abs (res) > math.pow (2, 31)) return 0 if (math.abs (res) > math.pow (2, 31)) Math.floor() x = ~~(x/10)} return res};Copy the code

It’s really just the sum of 10 bits, which is a little bit similar to the classic operation of carrying 10 bits into one, but a little bit different.

In this way, x is iterated over and over again, and finally x is dismantled to 0. When the dismantling is complete, res is returned directly before the boundary is crossed.

Palindrome number (Question Number 9)

The title

Given an integer x, return true if x is a palindrome; Otherwise, return false.

Palindromes are integers that are read in the same order (from left to right) and in the same order (from right to left). For example, 121 is a palindrome, while 123 is not.

Example 1: Input: x = 121 Output: true

Example 2: Input: x = -121 Output: false Description: Reads -121 from left to right. Read from right to left: 121-. So it’s not a palindrome number.

Example 3: Input: x = 10 Output: false Description: Reads 01 from right to left. So it’s not a palindrome number.

Example 4: Input: x = -101 Output: false

Tip:

-231 <= x <= 231 – 1

Advanced: Can you solve this problem without converting integers to strings?

link

Leetcode-cn.com/problems/pa…

explain

In fact, it is similar to the above integer inversion code, which can filter out some results directly through some judgments.

For example, negative numbers are definitely not palindromes, or numbers that are divisible by 10, because numbers that are divisible by 10 have to end with a 0, but no numbers start with 0.

Your own answer (bit operation)

var isPalindrome = function(x) { if (x < 0 || (x > 0 && x % 10 === 0)) return false var xBefore = x xEnd = 0 while (x) {  xEnd = xEnd * 10 + x % 10 x = ~~(x / 10) } return xBefore === xEnd };Copy the code

Run is also good, when the wind can run to the following results, feel LeetCode time and memory consumption is very unstable, no spectrum of things.

Execution time: 196 ms, beating 92.80% of all JavaScript submissions

Memory consumption: 46.2MB, beating 97.63% of all JavaScript submissions

Better method (mathematical algorithm)

This piece is a little super class, feel this life can not write such code, post the address:

Leetcode-cn.com/problems/pa…




PS: To view past articles and titles, click on the link below:

Here’s 👇 by date

Front Brush path – Directory (Date classification)

After some friends remind, the feeling should also be classified according to the type of questions here is in accordance with the type of 👇

Front brush problem path – Catalogue (Question type classification)

Those who are interested can also check out my personal homepage 👇

Here is RZ