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;  
} 

测试结果

Leetcode #7

3.参考资料:

  1. C++中string、char *、char[]的转换
    https://www.cnblogs.com/Pillar/p/4206452.html
    http://www.cnblogs.com/devilmaycry812839668/p/6353807.html
    https://www.cnblogs.com/mdumpling/p/8179167.html
  2. c++中字符串反转的3种方法
  3. C++ int与string的相互转换(含源码实现)

相关文章:

  • 2021-12-16
  • 2021-08-05
  • 2022-01-08
  • 2021-10-03
  • 2021-06-19
  • 2022-03-08
  • 2021-10-31
猜你喜欢
  • 2021-07-12
  • 2021-06-20
  • 2021-04-05
  • 2021-10-27
  • 2021-11-01
  • 2021-12-01
  • 2022-01-03
相关资源
相似解决方案