Leetcode số palindrome javascript

Cho một số nguyên x, trả về true nếu x là số nguyên palindrome. Một số nguyên là một palindrome khi nó đọc ngược như tiến

Ví dụ: 121 là một bảng màu trong khi 123 thì không

ví dụ 1

 Input: x = 121 
 Output: true
 Explanation: 121 reads as 121 from left to right and from right to left

ví dụ 2

 Input: x = -121
 Output: false
 Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

ví dụ 3

 Input: x = 10  
 Output: false  
 Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Hạn chế

  -231 <= x <= 231 - 1

Giải pháp 1 (Sử dụng Chuỗi gốc và Mảng)

var isPalindrome = function (x) {
    if(x < 0) return false;
    const tmp = parseInt(x.toString().split('').reverse().join(''))
    return x === tmp;
};

// To run Program
console.log(isPalindrome(121));

Phân tích độ phức tạp

    Time complexity: O(n)
    Space complexity: O(1)


Giải pháp 2 (Sắp xếp nhanh). ==> Tối ưu hóa

hãy nghĩ về cách hoàn nguyên nửa cuối của số. Đối với số 1221, nếu chúng ta làm 1221 % 10, chúng ta sẽ nhận được chữ số cuối cùng 

 Input: x = -121
 Output: false
 Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
0, để lấy chữ số thứ hai cho đến chữ số cuối cùng, chúng ta cần xóa chữ số cuối cùng khỏi 1221, chúng ta có thể làm như vậy bằng cách chia nó cho 10, 
 Input: x = -121
 Output: false
 Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
2. Sau đó, chúng ta có thể lấy lại chữ số cuối cùng bằng cách thực hiện một mô-đun cho 10, 
 Input: x = -121
 Output: false
 Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
3, và nếu chúng ta nhân chữ số cuối cùng với 10 và thêm chữ số cuối cùng thứ hai, 
 Input: x = -121
 Output: false
 Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
4, nó sẽ cho chúng ta số hoàn nguyên mà chúng ta muốn. Tiếp tục quá trình này sẽ cho chúng tôi số được hoàn nguyên với nhiều chữ số hơn

var isPalindrome = function(x) {
    if(x < 0 ) return false;       // Only Positive Number to allow
    if(x < 10)  return true;       // for 1-9 single digit cases
    if(x %10 === 0) return false;  // incase of last value 0 [220, 330]
    
    let reverse = 0;
    while(reverse < x){
        reverse = (reverse * 10) + (x % 10);
        x = Math.trunc(x/10);     // Math.trunc to avoid float values
    }
    return (x === reverse) || (Math.trunc(reverse/10) === x);
};

// To run Program
console.log(isPalindrome(1221));

Phân tích độ phức tạp

    Time complexity: O(log10(n)) 
    Space complexity: O(1)

Vì chúng ta chia số cho 10 và nhân số đảo ngược với 10, khi số ban đầu nhỏ hơn số đảo ngược, điều đó có nghĩa là chúng ta đã xử lý một nửa số chữ số của số. Vậy độ phức tạp về thời gian là O(log10​(n))

Nếu bạn có bất kỳ nghi ngờ nào, hãy thêm vào phần bình luận. Sẽ làm rõ ở mức tốt nhất của tôi. Ngoài ra, nếu bạn có giải pháp tốt hơn, vui lòng chia sẻ nó, sẽ cùng nhau phát triển để trở thành kỹ sư giỏi hơn

thuật toán


Sự cố - Số Palindrome

URL - https. //leetcode. com/problems/palindrome-number/

Cấp độ - Người mới bắt đầu

Thông tin chi tiết -

Cho một số nguyên x, trả về

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
0 nếu x là số nguyên đối xứng

Một số nguyên là một palindrome khi nó đọc ngược như tiến

  • Ví dụ,
    Input: x = -121
    Output: false
    Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
    
    2 là một palindrome trong khi
    Input: x = -121
    Output: false
    Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
    
    3 thì không

 

ví dụ 1

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

ví dụ 2

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

ví dụ 3

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

 

Hạn chế

  • Input: x = -121
    Output: false
    Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
    
    4

 

 

Ví dụ mã

#1 Code Ví dụ với Lập trình Javascript

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
5


/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
      /**
       * Solution 1 : JavaScript way
       */
      //   const reverseNumber = x.toString().split("").reverse().join("");
      //   return x == reverseNumber;

      /**
       * Solution 2: Traditional Programming way
       */
      let initialX = x,
        reverseNumber = 0;

      // Reverse the number.
      while (x != 0) {
        reverseNumber = parseInt(reverseNumber * 10 + parseInt(x % 10));
        x = parseInt(x / 10);
      }

      // Handle for negative numbers.
      if (initialX < 0) {
        return initialX === -1 * reverseNumber;
      }

      return initialX === reverseNumber;
};
Sao chép mã và thử với Trình chỉnh sửa trực tiếp

Đầu vào

cmd

121

đầu ra

cmd

ĐÚNG VẬY

#2 Ví dụ về mã với lập trình C

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
6


bool isPalindrome(int x) {
    int n, d, k = 0;
    
    if (x < 0) return false;
    
    n = x;
    while (n) {
        d = n % 10;
        if (k > (0x7fffffff - d) / 10) return false;    // overflow
        k = k * 10 + d;
        n = n / 10;
    }
    
    return (k == x);
}

Sao chép mã và thử với Trình chỉnh sửa trực tiếp

Đầu vào

cmd

-121

đầu ra

cmd

sai

#3 Ví dụ về mã với lập trình C++

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
7


class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0 || (x != 0 && x%10 == 0)) return false;
        int y = 0;
        while (x > y){
    	    y = y*10 + x%10;
    	    x = x/10;
        }
        return (x==y || x==y/10); 
    }
};
Sao chép mã và thử với Trình chỉnh sửa trực tiếp

Đầu vào

cmd

10

đầu ra

cmd

sai

#4 Ví dụ về mã với lập trình C#

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
8


namespace LeetCode
{
    public class _009_PalindromeNumber
    {
        public bool IsPalindrome(int x)
        {
            if (x < 0) { return false; }
            if (x < 10) { return true; }

            var temp = x;
            var y = 0;
            var digit = 0;
            while (temp != 0)
            {
                digit = temp % 10;
                y = y * 10 + digit;
                temp /= 10;
            }

            return x == y;
        }
    }
}

Sao chép mã và thử với Trình chỉnh sửa trực tiếp

Đầu vào

cmd

10

đầu ra

cmd

sai

#5 Ví dụ về mã với lập trình Java

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
9


class Solution {
  public boolean isPalindrome(int x) {
    if (x < 0) {
      return false;
    }
    int reversedNum = 0;
    int xCopy = x;
    while (x > 0) {
      int rem = x % 10;
      reversedNum = reversedNum * 10 + rem;
      x /= 10;
    }
    return reversedNum == xCopy;
  }
}
Sao chép mã và thử với Trình chỉnh sửa trực tiếp

Đầu vào

cmd

121

đầu ra

cmd

ĐÚNG VẬY

#6 Ví dụ về mã với lập trình Python

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
0


class Solution:
    def isPalindrome(self, x):
        return str(x) == str(x)[::-1]    
Sao chép mã và thử với Trình chỉnh sửa trực tiếp

Đầu vào

cmd

121

đầu ra

cmd

ĐÚNG VẬY

trình diễn


Leetcode - Giải quyết vấn đề về số Palindrome trong Javascript Leetcode

Trước
#08 Giải pháp chuỗi Leetcode thành số nguyên (atoi) trong C, C++, Java, JavaScript, Python, C# Leetcode

Kế tiếp
#10 Giải pháp khớp biểu thức chính quy Leetcode trong C, C++, Java, JavaScript, Python, C# Leetcode

Thử với Trình chỉnh sửa trực tiếp


Thể loại - Leetcode Thẩm phán trực tuyến   Maniruzzaman Akash   4 tháng trước   401   0

Làm cách nào để tìm số palindrome trong JavaScript?

Thuật toán đối xứng. .
Lấy đầu vào từ người dùng
Sử dụng một biến tạm thời (giả sử temp) để lưu trữ đầu vào
Tìm mặt trái của nhiệt độ
So sánh đầu vào đã cho với temp
Nếu temp và đầu vào giống nhau, trả về True
Nếu chúng không giống nhau, trả về Sai

Palindrome có phải là số nguyên không?

Một số nguyên là số đối xứng nếu đảo ngược của số đó bằng số ban đầu .

Số nào là số palindrom?

Số palindromic là một số (trong một số cơ số ) giống nhau khi viết xuôi hoặc viết ngược , i. e. , có dạng. . Do đó, một vài số xuôi ngược đầu tiên là 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, . (OEIS A002113).