Skip to content

Commit 688bc02

Browse files
committedJan 14, 2016
7
1 parent 4528c9d commit 688bc02

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
 

‎7_reverse_interger.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "test.h"
2+
3+
class Solution {
4+
public:
5+
int reverse(int x)
6+
{
7+
int64_t ret = 0;
8+
while(x != 0)
9+
{
10+
ret *= 10;
11+
ret += x % 10;
12+
13+
if(ret != (int)ret) //题目要求溢出时返回0
14+
return 0;
15+
16+
x = x / 10;
17+
18+
}
19+
return ret;
20+
}
21+
};
22+
23+
24+
int main()
25+
{
26+
Solution s;
27+
cout << s.reverse(-32465) << endl;
28+
cout << s.reverse(1534236469) << endl; //溢出测试
29+
}

‎Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ targets = \
55
4_median_of_two_sorted_arrays.exec\
66
5_longest_palindromic_substring.exec\
77
6_zigzag_conversion.exec\
8+
7_reverse_interger.exec\
89

910

1011
CC = g++

‎performance.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Author: LiZhaojia
3+
*
4+
* Last modified: 2014-09-29 22:01 +0800
5+
*
6+
* Description: 用于性能测试
7+
*/
8+
9+
#include <iostream>
10+
#include <memory>
11+
#include <functional>
12+
#include <chrono>
13+
using namespace std;
14+
15+
void performance ( void (*pfun)(), uint32_t times, std::string info = "" )
16+
{
17+
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
18+
19+
cout << "\n\n" << info << " start:" << endl;
20+
start = std::chrono::system_clock::now();
21+
for(int i = 0; i < times; ++i)
22+
(*pfun)();
23+
end = std::chrono::system_clock::now();
24+
25+
std::chrono::duration<double> elapsed_seconds = end-start;
26+
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
27+
28+
std::cout << std::dec << std::fixed
29+
<< info
30+
<< " finished computation at " << std::ctime(&end_time)
31+
<< "elapsed time: " << elapsed_seconds.count()
32+
<< "s; average time: " << elapsed_seconds.count() / times << endl;;
33+
}
34+
35+
#define PERFORMANCE(fun, times) performance(fun, times, #fun)

0 commit comments

Comments
 (0)