Codelet Keep code simple stupid

Integer to Roman

Question Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Solution #include <string> using namespace std; const char* NUMS[4][10] = { // 0 1 2 3 4 5 6 7 8 9 { "", "M", "MM", "MM...

Container With Most Water

Question Given n non-negative integers \(a_1, a_2, …, a_n\), where each represents a point at coordinate \((i, a_i)\). n vertical lines are drawn such that the two endpoints of line i is at \((i, a_i)\) and \((i, 0)\). Find two lines, which together with x-axis forms a container, such t...

Palindrome Number

Question Determine whether an integer is a palindrome. Do this without extra space. 今天又看到了这个问题,有下面几点需要注意,再回顾一下吧 不准使用额外空间 针对负数的处理 整数的溢出问题 Solution class Solution { public: bool isPalindrome(int x) { if (x < 0) { return false; } int digits[10]; int length ...

String to Integer (atoi)

Question Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be specified vaguely (ie, no...

Reverse Integer

Question Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Solution #include <climits> class Solution { public: int reverse(int x) { const int threshold = INT_MAX / 10; bool negtive = x < 0; x = negtive ? -x : x; ...