1 class Solution {
2 public:
3 string convert(string s, int numRows) {
4 vector<vector<char>> map;
5 map.resize(numRows);
6
7 for (int i = 0; i < numRows; i++)
8 map[i].resize(s.length());
9
10 int count = 0;
11 int j = 0;
12 while (count < s.length())
13 {
14 int i = 0;
15 while (count<s.length() && i<numRows)
16 map[i++][j]= s[count++];
17
18 j++; i--;
19 while (count<s.length() && i>1)
20 map[--i][j++] = s[count++];
21 }
22 string ans = "";
23 for (size_t i = 0; i < numRows;i++){
24 for (size_t j = 0; j < s.length(); j++){
25 if (map[i][j] != 0)
26 ans += map[i][j];
27 }
28 }
29 return ans;
30 }
31 };
32
1 class Solution {
2 public:
3 int romanToInt(string s) {
4 map<char, int>num;
5 num['I'] = 1;
6 num['V'] = 5;
7 num['X'] = 10;
8 num['L'] = 50;
9 num['C'] = 100;
10 num['D'] = 500;
11 num['M'] = 1000;
12 int sum = num[s[0]];
13 for (int i = 1; i < s.length(); i++)
14 {
15 if (num[s[i - 1]] >= num[s[i]])
16 sum += num[s[i]];
17 else
18 sum = sum + num[s[i]] -2* num[s[i - 1]];
19 }
20 return sum;
21 }
22 };
23
1 class Solution {
2 public:
3 bool isPalindrome(int x) {
4
5 if (x < 0)return false;
6
7 list<int> l;
8 while (x){
9 l.push_back(x % 10);
10 x /= 10;
11 }
12
13 while (!l.empty()) {
14 if (l.front() == l.back()) {
15 l.pop_front();
16 if(!l.empty())
17 l.pop_back();
18 }
19 else
20 return false;
21 }
22 return true;
23 }
24 };
1 class Solution {
2 public:
3 int reverse(int x) {
4 list<long long>num;
5 bool mark = false;
6 if (x < 0){
7 mark = true;
8 x = -x;
9 }
10
11 while (x){
12 num.push_back(x % 10);
13 x = x / 10;
14 }
15
16 long long sum = 0;
17 while (!num.empty()) {
18 sum = sum*10+num.front();
19 num.pop_front();
20 }
21
22 if (mark)
23 sum = -sum;
24
25 int ans = sum;
26
27 if (sum > 0X7fffffff || sum < -0X7fffffff-1)
28 ans = 0;
29
30 return ans;
31 }
32 };
1 class Solution {
2 public:
3 int lengthOfLongestSubstring(string s) {
4 int mark[256] = { 0 };
5 int begin = 0;
6 int end = 0;
7 int ret_max = 0;
8 while (begin<=end &&begin <s.length() && end<s.length())
9 {
10 char ch = s[end];
11 if (mark[ch] == 0){
12 mark[ch]++;
13 end++;
14 }else{
15 ch = s[begin];
16 mark[ch]--;
17 begin++;
18 }
19 ret_max = end - begin > ret_max ? end - begin : ret_max;
20 }
21 return ret_max;
22 }
23 };
24
1 class Solution {
2 public:
3 vector<int> twoSum(vector<int>& nums, int target)
4 {
5 bool mark = true;
6 vector<int>ret;
7 for (int i = 0; mark && i < nums.size(); i++)
8 {
9 for (int j = 0; mark && j < nums.size(); j++)
10 {
11 if (i == j)continue;
12 if (nums[j] == target - nums[i])
13 {
14 ret.push_back(i);
15 ret.push_back(j);
16 mark = false;
17 }
18 }
19 }
20 return ret;
21 }
22 };
1 class Solution {
2 public:
3 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
4 {
5 int sign = 0;
6 int sum;
7 ListNode* root = nullptr;
8 ListNode* p_root = nullptr;
9 while (l1!=NULL || l2 != NULL || sign)
10 {
11 int val1 = 0;
12 int val2 = 0;
13 if (l1 != NULL){
14 val1 = l1->val;
15 l1 = l1->next;
16 }
17 if (l2 != NULL) {
18 val2 = l2->val;
19 l2 = l2->next;
20 }
21
22 sum = ((val1 + val2)%10 + sign)%10;
23 sign = (val1 + val2 + sign) / 10;
24 if (root == nullptr)
25 {
26 root = new ListNode(sum);
27 p_root = root;
28 }
29 else
30 {
31 p_root->next = new ListNode(sum);
32 p_root = p_root->next;
33 }
34 }
35 return root;
36 }
37 };
1 class Solution {
2 public:
3 string longestPalindrome(string s) {
4 int max_len = 0;
5 string ans;
6 for (int i = 0; i < s.length(); i++)
7 {
8 for (int j = s.length() -1; j >= 0 ; j--)
9 {
10 if (s[j] != s[i])continue;
11 if (j - i + 1 <= max_len)break;
12
13 int ii = i;
14 int jj = j;
15 while (ii< s.length() && jj >=0 &&
16 s[ii]==s[jj]&& ii<=jj){
17 ii++;
18 jj--;
19 }
20 if (ii == jj + 1 || ii == jj + 2)
21 {
22 max_len = j - i + 1;
23 ans = s.substr(i, max_len);
24 }
25 }
26 }
27 return ans;
28 }
29 };
1 class Solution {
2 public:
3 int findComplement(int num) {
4 int sum = 0;
5 int n = 1;
6 while (num){
7 sum += ((num & 1) ^ 1)*n;
8 num >>= 1;
9 n *= 2;
10 }
11 return sum;
12 }
13 };
1 class Solution {
2 public:
3 int arrayPairSum(vector<int>& nums) {
4 sort(nums.begin(),nums.end());
5 int sum = 0;
6 for (int pos = 0; pos < nums.size(); pos+= 2)
7 sum += nums[pos];
8 return sum;
9 }
10 };