하루의 쉼터

[LeetCode] 9. Palindrome Number 본문

Coding Test/LeetCode

[LeetCode] 9. Palindrome Number

Changun An 2021. 10. 7. 15:56
반응형

Question :
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

Example 1:
Input : x = 121
Output : true

Example 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.

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

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

Constraints:
-231 <= x <= 231 - 1

Follow up: 
Could you solve it without converting the integer to a string?

Start Class:

class Solution {
public:
    bool isPalindrome(int x) {
    }
};

전체 FlowCharts: 

cl_solution.h 

#pragma once

#include <iostream>
#include <string>
#include <vector>
class cl_solution
{
	private : 
	bool fn_data_check(int x);
	int fn_user_input();

	public:
		bool isPalindrome(int x);
		bool fn_run();
};
함수명 반환형 매개변수 기능 설명
fn_data_check bool int x 데이터 크기 체크
fn_user_input int void  유저 데이터 입력
isPalindrome bool int x 데이터 처러 및 가공 알고리즘 구현 함수, true /  false
fn_run bool void 구동 함수

 

cl_solution.cpp

#include "cl_solution.h"

bool cl_solution::fn_data_check(int x) {
	return  (x <= -2147483648 || x >= 2147483647) ? false : true;
}

int cl_solution::fn_user_input(){
	int temp_data = 0;
	std::cout << "Plz User Data" << std::endl;
	std::cin >> temp_data;
	return fn_data_check(temp_data) ? temp_data : fn_user_input();
}

bool cl_solution::isPalindrome(int x){
	std::string st_temp;
	std::vector<char>*vec_temp=NULL;
	int start = 0;
	int end = 0;

	if (x < 0) 
		return false;
	st_temp = std::to_string(x);
	vec_temp = new std::vector<char>(st_temp.length());
	std::copy(st_temp.begin(), st_temp.end(), vec_temp->begin());
	end = vec_temp->size();
	while ((start != end-1)&&(start<=end-1)) {
		if ((*vec_temp)[start] != (*vec_temp)[end-1]){
			return false;
		}
		if (start >= end-1) {
			break;
		}
		else {
			start++;
			end--;
		}
	}
	return true;

}

bool cl_solution::fn_run(){
	std::cout<<isPalindrome(fn_user_input())<<std::endl;
	return true;
}

 

코드 분석 :

1. 데이터 체크

bool cl_solution::fn_data_check(int x) {
	return  (x <= -2147483648 || x >= 2147483647) ? false : true;
}

 

2. 양수 음수 체크

Why? 음수는 좌우 반전이 똑같을 수 없으므로 프로그램의 속도를 올리기 위하여 false 반환


3. 정수를 문자열로 변환

* 데이터분할 하여 저장하기 위하여 사용

	std::string st_temp;
	std::vector<char>*vec_temp=NULL;
	int start = 0;
	int end = 0;
	if (x < 0) 
		return false;
	st_temp = std::to_string(x);
	vec_temp = new std::vector<char>(st_temp.length());
	std::copy(st_temp.begin(), st_temp.end(), vec_temp->begin());
	end = vec_temp->size();


4. 배열을 반으로 나누거나 처음과 끝을 연산하며 비교

std::copy(st_temp.begin(), st_temp.end(), vec_temp->begin());
	end = vec_temp->size();
	while ((start != end-1)&&(start<=end-1)) {
		if ((*vec_temp)[start] != (*vec_temp)[end-1]){
			return false;
		}
		if (start >= end-1) {
			break;
		}
		else {
			start++;
			end--;
		}
	}


5. 대칭 비교하며 반복

* Start와 End가 교차 되거나 같으면 반복문 종료

while ((start != end-1)&&(start<=end-1)) {
		if ((*vec_temp)[start] != (*vec_temp)[end-1]){
			return false;
		}
		if (start >= end-1) {
			break;
		}
		else {
			start++;
			end--;
		}
	}


Result :

GitHub:

https://github.com/Anchangun/LeetCode/tree/main/Cpp/9.%20Palindrome%20Number

 

GitHub - Anchangun/LeetCode: 문제풀이

문제풀이. Contribute to Anchangun/LeetCode development by creating an account on GitHub.

github.com

LeetCode:

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

 

Palindrome Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

반응형

'Coding Test > LeetCode' 카테고리의 다른 글

[LeetCode] 7. Reverse Integer  (0) 2021.10.05
[LeetCode] 206. Reverse Linked List  (0) 2020.12.06
[LeetCode] 53. Maximum Subarray  (0) 2020.11.30
[LeetCode] 21. Merge Two Sorted Lists  (0) 2020.11.24
[LeetCode] 136. Single Number  (0) 2020.11.23
Comments