Leetcode 记录 #7
1. 使用C++
/*
LeetCode practice #7: Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
*/
class Solution {
public:
int reverse(int x) {
//chage type int into type string
string x_str = to_string(x);
cout<<x_str<<endl;
//chage type string into type char*
char *x_char = const_cast<char*>(x_str.c_str());
//reverse char*
int i, j, n=x_str.length();
for(int i=0,j=n-1;i<j;i++,j--){
char c=x_char[i];
x_char[i]=x_char[j];
x_char[j]=c;
}
cout<<x_char<<endl;
long long result;
result = atoll(x_char);
if(x<0)
result = -1*result;
cout<<result<<endl;
if(result>= INT_MIN && result <= INT_MAX)
return result;
else
return 0;
}
};
status: Accepted
Runtime: 20 ms, faster than 27.63% of C++ online submissions for Reverse Integer.
Memory Usage: 11.4 MB, less than 0.36% of C++ online submissions for Reverse Integer.
2.数字反转
源码
/*
Reverse Integer(默认不会产生溢出)
相关知识点:
int, char*(char[]) 与 string 互转
string和char*的reverse
*/
#include <iostream> // std::cout,std::atoi
#include <string> // std::string, std::to_string
#include <algorithm> //std::reverse
#include <string.h> //std::strrev
using namespace std;
/*** chage type int into type string ***/
int reverse_int1(int x) {
//chage type int into type string
string x_str = to_string(x);
cout<<x_str<<endl;
//reverse string
reverse(x_str.begin(),x_str.end());
cout<<x_str<<endl;
int result;
//chage type string into type int
result = atoi(x_str.c_str());
if(x>0)
return result;
else
return -1*result;
}
/*** chage type int into type string, then to type char* ***/
int reverse_int2(int x) {
//chage type int into type string
string x_str = to_string(x);
cout<<x_str<<endl;
//chage type string into type char*
char x_char[x_str.length()];
strcpy(x_char, x_str.c_str());
//reverse char*
strrev(x_char);
cout<<x_char<<endl;
int result;
result = atoi(x_char);
if(x>0)
return result;
else
return -1*result;
}
int reverse_int3(int x){
//chage type int into type string
string x_str = to_string(x);
cout<<x_str<<endl;
//chage type string into type char*
//1. may lead to dynamic-stack-buffer-overflow
//char x_char[x_str.length()];
//strcpy(x_char, x_str.data());
//2.
char *x_char = const_cast<char*>(x_str.c_str());
//3.
//char * x_char = new char[strlen(x_str.c_str())+1];
//strcpy(x_char, x_str.c_str());
//reverse char*
int i, j, n=x_str.length();
for(int i=0,j=n-1;i<j;i++,j--){
char c=x_char[i];
x_char[i]=x_char[j];
x_char[j]=c;
}
cout<<x_char<<endl;
int result;
result = atoi(x_char);
if(x>0)
return result;
else
return -1*result;
}
int main ()
{
int data = 123;
//cout<<"Please enter testing data:"<<endl;
//cin>>data;
int result1, result2, result3;
cout<<"Using reverse function reverse() in <<algorithm>>."<<endl;
result1 = reverse_int1(data);
cout<<result1<<endl<<endl<<endl;
cout<<"Using reverse function strrev() in <<string.h>>."<<endl;
result2 = reverse_int2(data);
cout<<result2<<endl<<endl<<endl;
cout<<"Using user-define reverse function."<<endl;
result3 = reverse_int3(data);
cout<<result3<<endl<<endl<<endl;
return 0;
}