leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 6180f2a95850449bc760ac49e4de127bfab4aa22
parent 24f468179b387fe3e222ed9d6367e8ee7826969d
Author: AndrewLockVI <andrew.laack@gmail.com>
Date:   Thu,  6 Apr 2023 13:15:53 -0500

Palindrome number question completed

Diffstat:
Apalindrome-number/a.out | 0
Apalindrome-number/palindrome-number.cpp | 36++++++++++++++++++++++++++++++++++++
Apalindrome-number/palindrome-numberV2.cpp | 42++++++++++++++++++++++++++++++++++++++++++
3 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/palindrome-number/a.out b/palindrome-number/a.out Binary files differ. diff --git a/palindrome-number/palindrome-number.cpp b/palindrome-number/palindrome-number.cpp @@ -0,0 +1,36 @@ +#include <list> +#include <iostream> + +//Leet Ratings +// Speed Memory +//Total 27ms 6.1MB +//Beats 15.5% 14.79% +// + + +using namespace std; + +bool isPalindrome(int x) { + list<char> list1; + string temp = to_string(x); + for (int i = 0; i < temp.length() / 2; ++i){ + if(temp[i] != temp[temp.length() - i - 1]){ + return false; + } + } + return true; +} + + + + +int main(){ + + + int input; + cout << "Enter a number: "; + cin >> input; + cout << boolalpha << isPalindrome(input) << endl; + + +} diff --git a/palindrome-number/palindrome-numberV2.cpp b/palindrome-number/palindrome-numberV2.cpp @@ -0,0 +1,42 @@ +#include <list> +#include <iostream> + +//Leet Ratings +// Speed Memory +//Total 4ms 6MB +//Beats 98.16% 14.79% +// +//Added check to see if number is negative to the start to not have to deal with +//those issues. + + +using namespace std; + +bool isPalindrome(int x) { + if(x >= 0) + { + list<char> list1; + string temp = to_string(x); + for (int i = 0; i < temp.length() / 2; ++i){ + if(temp[i] != temp[temp.length() - i - 1]){ + return false; + } + } + return true; + } + return false; +} + + + + +int main(){ + + + int input; + cout << "Enter a number: "; + cin >> input; + cout << boolalpha << isPalindrome(input) << endl; + + +}