Leetcode #7

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;  
} 
测试结果

在这里插入图片描述

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的相互转换(含源码实现)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值