leecode保存 简单题到ZY转换
2020-01-19 09:27 kk20161206 阅读(173) 评论(0) 编辑 收藏 举报//#include "stdafx.h" #include <iostream> #include <cstdlib> #include <stdio.h> #include <string> #include <vector> #include <stack> #include <queue> #include <sstream> using namespace std; class ListNode { public: int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; static void print(ListNode* node) { if (node == nullptr) { return; } std::cout << node->val << " " << std::endl; while(node->next != nullptr) { node = node->next; std::cout << node->val << " " << std::endl; } } struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; static void print(TreeNode* node) { if (node == nullptr) { std::cout << " null " << std::endl; } std::cout << node->val << " " << std::endl; /*if (node->left != nullptr) { node = node->next; std::cout << node->val << " " << std::endl; }*/ } static void print(vector<vector<int>> vec) { for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < vec[i].size(); j++) { cout << vec[i][j] << " " << endl; } cout << " 第 " << i << " 行 " << endl; cout << endl; } } static void print(vector<int> vec) { for (int j = 0; j < vec.size(); j++) { cout << vec[j] << " " << endl; } } class Solution { public: #pragma region RomanToInt /*给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1 : 输入: "abcabcbb" 输出 : 3 解释 : 因为无重复字符的最长子串是 "abc",所以其长度为 3。 I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。 X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。 */ /*int lengthOfLongestSubstring(string s) { for (int i = 0; i < s.length() - 1; i++) { for (int j = 1; j < s.length() - 2; j++) { string subStr = s.substr(i, j - i + 1); } } }*/ int romanToInt(string s) { int value = 0; int currentValue = 0; for (int i = 0; i < s.length(); i++) { char ch = s[i]; switch (ch) { case 'I': if ((i + 1) <= s.length() - 1) { switch (s[i + 1]) { case 'V': i++; currentValue = 4; break; case 'X': i++; currentValue = 9; break; default: currentValue = 1; break; } } else { currentValue = 1; } break; case 'V': currentValue = 5; break; case 'X': if ((i + 1) <= s.length() - 1) { switch (s[i + 1]) { case 'L': i++; currentValue = 40; break; case 'C': currentValue = 90; i++; break; default: currentValue = 10; break; } } else { currentValue = 10; } break; case 'L': currentValue = 50; break; case 'C': if ((i + 1) <= s.length() - 1) { switch (s[i + 1]) { case 'D': i++; currentValue = 400; break; case 'M': currentValue = 900; i++; break; default: currentValue = 100; break; } } else { currentValue = 100; } break; case 'D': currentValue = 500; break; case 'M': currentValue = 1000; break; } value += currentValue; } return value; } #pragma endregion ListNode* deleteDuplicates(ListNode* head) { /*if (head == nullptr) { return nullptr; }*/ ListNode* HeadNode = head; while (head != nullptr && head->next != nullptr) { if (head->next->val == head->val) { head->next = head->next->next; } else { head = head->next; } } return HeadNode; } void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { if (n == 0) { return; } if (m == 0) { nums1 = nums2; return; } int p1 = m - 1; int p2 = n - 1; int p = m + n - 1; while ((p1 >= 0) && (p2 >= 0)) { nums1[p--] = nums1[p1] >= nums2[p2] ? nums1[p1--] : nums2[p2--]; } while (p2 >= 0) { nums1[p--] = nums2[p2--]; } while (p1 >= 0) { nums1[p--] = nums1[p1--]; } } string longestCommonPrefix(vector<string>& strs) { } bool isSameTree(TreeNode* p, TreeNode* q) { if (p == nullptr && q == nullptr) { return true; } if (p == nullptr || q == nullptr) { return false; } if (p->val != q->val) { return false; } return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } bool isMirror(TreeNode* t1, TreeNode* t2) { if (t1 == nullptr && t2 == nullptr) { return true; } if (t1 == nullptr || t2 == nullptr) { return false; } return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left); } bool isSymmetric(TreeNode* root) { return isMirror(root, root); } int maxDepth(TreeNode* root) { if (root == nullptr) { return 0; } int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth); } string addBinary(string a, string b) { if (a.empty() && b.empty()) { return "0"; } if (a.empty()) { return b; } if (b.empty()) { return a; } int aLen = a.length(); int bLen = b.length(); int cLen = aLen >= bLen ? aLen : bLen; cLen += 1; string c(cLen, '0'); int sum = 0; int carry = 0; int curAIndex = 0; int curBIndex = 0; int curAValue = 0; int curBValue = 0; for (int i = 0; i < cLen - 1; i++) { curAIndex = aLen - 1 - i; curBIndex = bLen - 1 - i; curAValue = curAIndex < 0 ? 0 : a[curAIndex] - '0'; curBValue = curBIndex < 0 ? 0 : b[curBIndex] - '0'; sum = (curAValue + curBValue + carry) % 2; carry = (curAValue + curBValue + carry) / 2; c[cLen - 1 - i] = sum + '0'; } if (carry == 0) { c = c.substr(1); } else { c[0] = carry + '0'; } return c; } string addBinaryNew(string a, string b) { int aLen = a.length(); int bLen = b.length(); while (aLen < bLen) { a = '0' + a; aLen++; } while (bLen < aLen) { b = '0' + b; bLen++; } for (int i = aLen - 1; i > 0; i--) { a[i - 1] = ((a[i] - '0') + (b[i] - '0')) / 2 + a[i - 1]; a[i] = ((a[i] - '0') + (b[i] - '0')) % 2 + '0'; } a[0] = a[0] + b[0] - '0'; if (a[0] >= '2') { a[0] = (a[0] - '0') % 2 + '0'; a = '1' + a; } return a; } int mySqrt(int x) { if (x == 0) { return x; } else if (x == 2 || x == 3) { return 1; } else if (x == 4) { return 2; } else { int left = 0; int right = (x == 2147483647) ? (x/2+1) : (x + 1) / 2; int value = (left + right) / 2; value = (value > 46340) ? 46340 : value; int squre = value * value; while (left < right) { if (squre < x) { if (value >= 46340) { return 46340; } left = value; value = (left + right + 1) / 2; } else if (value * value > x) { right = value - 1; value = (left + right + 1) / 2; } else { return value; } squre = value * value; } return left; } } int climbStairs(int n) { if (n <= 2) { return n; } int first = 1; int second = 2; int value = 0; for (int i = 3; i <= n; i++) { value = first + second; first = second; second = value; } return second; } vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<int> vec; vector<vector<int>> result; if (root == nullptr) { return result; } queue<TreeNode*> queue; queue.push(root); while (!queue.empty()) { vector<int> oneLevel; int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode* node = queue.front(); oneLevel.push_back(node->val); queue.pop(); if (node->left != nullptr) { queue.push(node->left); } if (node->right != nullptr) { queue.push(node->right); } } result.insert(result.begin(), oneLevel); } return result; } TreeNode* sortedArrayToBST(vector<int>& nums) { if (nums.size() == 0) { return nullptr; } if (nums.size() == 1) { TreeNode* A = new TreeNode(nums[0]); return A; } int num = nums.size() / 2; std::vector<int> leftVector(num); std::copy(nums.begin(), nums.begin() + num, leftVector.begin()); std::vector<int> rightVector(nums.size() - num - 1); std::copy(nums.begin() + num + 1, nums.end(), rightVector.begin()); TreeNode* A = new TreeNode(nums[num]); A->left = sortedArrayToBST(leftVector); A->right = sortedArrayToBST(rightVector); return A; } bool isBalanced(TreeNode* root) { if (root == nullptr) { return true; } if (root->left == nullptr && root->right == nullptr) { return true; } int leftHeight = maxDepth(root->left); int rightHeight = maxDepth(root->right); int sub = leftHeight > rightHeight ? leftHeight - rightHeight : rightHeight - leftHeight; return sub <= 1 && isBalanced(root->left) && isBalanced(root->right); } bool isBalanced1(TreeNode* root) { bool balance = false; int h = 0; return Balance(root, h); } bool Balance(TreeNode* root, int &h) { if (root == NULL) { h = 0; return true; } if (root->left == NULL && root->right == NULL) { h = 1; return true; } int hr, hl; bool br = Balance(root->left, hl); bool bl = Balance(root->right, hr); h = (hl > hr ? hl : hr) + 1;//三目运算符 if (abs(hl - hr) < 2) return br & bl; return false; } int minDepth(TreeNode* root) { if (root == nullptr) { return 0; } if (root->left == nullptr && root->right == nullptr) { return 1; } if (root->left == nullptr && root->right != nullptr) { return minDepth(root->right) + 1; } if (root->left != nullptr && root->right == nullptr) { return minDepth(root->left) + 1; } int depthL = minDepth(root->left); int depthR = minDepth(root->right); int depth = depthL > depthR ? depthR : depthL; depth += 1; return depth; } bool hasPathSum(TreeNode* root, int sum) { if (root == nullptr) { return false; } if (root->left == nullptr && root->right == nullptr) { if (root->val == sum) return true; else return false; } if (root->left == nullptr && root->right != nullptr) { return hasPathSum(root->right, sum - root->val); } if (root->left != nullptr && root->right == nullptr) { return hasPathSum(root->left, sum - root->val); } if (root->left != nullptr && root->right != nullptr) { return hasPathSum(root->right, sum - root->val) || hasPathSum(root->left, sum - root->val); } } vector<vector<int>> generate(int numRows) { if (numRows == 0) { vector<vector<int>> result{}; return result; } if (numRows == 1) { vector<int> row{ 1 }; vector<vector<int>> result{ row }; return result; } if (numRows == 2) { vector<int> row0{ 1 }; vector<int> row1{ 1 , 1}; vector<vector<int>> result{ row0, row1 }; return result; } vector<vector<int>> resultRowM1 = generate(numRows - 1); vector<int> RowM(numRows, 1); vector<int> RowM1 = resultRowM1[numRows - 2]; for (int i = 1; i < (numRows + 1)/2; i++) { RowM[i] = RowM1[i - 1] + RowM1[i]; RowM[numRows - 1 - i] = RowM[i]; } resultRowM1.push_back(RowM); return resultRowM1; } vector<int> getRow(int rowIndex) { if (rowIndex == 0) { vector<int> row{ 1 }; return row; } if (rowIndex == 1) { vector<int> row1{ 1 , 1 }; return row1; } if (rowIndex == 2) { vector<int> row1{ 1 , 2, 1 }; return row1; } vector<int> RowM1 = getRow(rowIndex - 1); RowM1.push_back(1); for (int i = rowIndex / 2; i >= 1; i--) { RowM1[i] = RowM1[i - 1] + RowM1[i]; RowM1[rowIndex - i] = RowM1[i]; } return RowM1; } int maxProfit(vector<int>& prices) { int max = 0; for (int i = 0; i < prices.size(); i++) { for (int j = i + 1; j < prices.size(); j++) { if (prices[j] - prices[i] > max) { max = prices[j] - prices[i]; } } } return max; } int maxProfit1(vector<int>& prices) { if (prices.size() <= 0) return 0; int min = prices[0]; int sum = 0; for (int i = 1; i < prices.size(); i++) { if (prices[i] > min) { sum += prices[i] - min; min = prices[i]; } if (prices[i] < min) { min = prices[i]; } } return sum; } bool isNumOrCharacter(char& ch) { return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')); } void turnLowerCharacter(char& ch) { if (ch >= 'A' && ch <= 'Z') ch = ch + 'a' - 'A'; return; } bool isPalindrome(string s) { if (s.size() == 0) return true; for (int i = 0, j = s.size() - 1; i <= j;) { if (!isNumOrCharacter(s[i])) { i++; continue; } if (!isNumOrCharacter(s[j])) { j--; continue; } if (s[i] >= '0' && s[i] <= '9') { if (s[i] != s[j]) return false; else { i++; j--; } } else { turnLowerCharacter(s[i]); turnLowerCharacter(s[j]); if (s[i] != s[j]) return false; else { i++; j--; } } } return true; } int singleNumber(vector<int>& nums) { if (nums.size() == 0) { return 0; } if (nums.size() == 1) { return nums[0]; } int sum = nums[0]; for (int i = 1; i < nums.size(); i++) { sum ^= nums[i]; } return sum; } bool hasCycle(ListNode *head) { ListNode* head1 = head; while (head1 != nullptr && head->next != nullptr && head1->next != nullptr) { head = head->next; head1 = head1->next->next; if (head == head1) return true; } return false; } ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *headBPtr = headB; ListNode *headAPtr = headA; while (headAPtr != headBPtr) { headAPtr = headAPtr == nullptr ? headB : headAPtr->next; headBPtr = headBPtr == nullptr ? headA : headBPtr->next; } return headAPtr; } vector<int> twoSum(vector<int>& numbers, int target) { vector<int> sum; for (int i = 0, j = numbers.size() - 1; i <= j;) { if (numbers[i] + numbers[j] < target) { i++; } else if (numbers[i] + numbers[j] > target) { j--; } else { sum.push_back(i+1); sum.push_back(j+1); return sum; } } return sum; } string convertToTitle(int n) { string res; while (n) { char c = 'A' + --n % 26;; // 'Z'; res += c; n = n / 26; } reverse(res.begin(), res.end()); return res; } int titleToNumber(string s) { int sum = 0; for (int i = 0; i < s.size(); i++) { sum = sum * 26 + s[i] - 'A' + 1; } return sum; } }; class MinStack { public: /** initialize your data structure here. */ MinStack() { mList = NULL; } void push(int x) { mStack.push(x); if (mList == nullptr) { mList = new ListNode(x); mList->val = x; mList->next = nullptr; return; } if (mList->val >= x) { ListNode* node = new ListNode(x); node->next = mList; mList = node; return; } else { ListNode* prev = mList; ListNode* current = mList->next; while (prev->val <= x) { if (current == nullptr) { ListNode* node = new ListNode(x); prev->next = node; return; } else { if (current->val <= x) { current = current->next; prev = prev->next; } else { ListNode* node = new ListNode(x); node->next = current; prev->next = node; return; } } } } } void pop() { int top = mStack.top(); mStack.pop(); if (mList->val == top) { mList = mList->next; return; } ListNode * prev = mList; ListNode * current = mList; while (current != nullptr) { if (current->val == top) { prev->next = current->next; current = nullptr; delete current; return; } else { prev = prev->next; current = current->next; } } } int top() { return mStack.top(); } int getMin() { return mList->val; } private: stack<int> mStack; ListNode* mList; }; int main(int, char[]) { Solution *s = new Solution(); //string str = "MCMXCIV"; //int value = s->romanToInt(str); // III:3, IV:4, IX:9, LVIII:58, MCMXCIV:1994 //string value = s->addBinaryNew("1001", "1011"); //int sqrt = s->mySqrt(2147483647); //int stairs = s->climbStairs(3); //printf("Hello: %s!\n", value); //std::cout << "++++++ +++++: " << stairs; /*ListNode* node = new ListNode(1); ListNode* node1 = new ListNode(1); ListNode* node2 = new ListNode(2); ListNode* node3 = new ListNode(3); ListNode* node4 = new ListNode(3); node->next = node1; node1->next = node2; node2->next = node3; node3->next = node4; ListNode* nodeResult = s->deleteDuplicates(node); print(nodeResult);*/ /*vector<int> a = { 1, 2, 3, 0, 0, 0 }; vector<int> b = { 2, 5, 6 }; s->merge(a, 3, b, 3); for (int i = 0; i < a.size(); i++) { std::cout << "++++++ +++++: " << a[i] << std::endl; }*/ /*TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}*/ //TreeNode* A = new TreeNode(1); //TreeNode* B1 = new TreeNode(2); //TreeNode* B2 = new TreeNode(2); //TreeNode* C1 = new TreeNode(3); //TreeNode* C2 = new TreeNode(4); //TreeNode* C3 = new TreeNode(4); //TreeNode* C4 = new TreeNode(3); //B1->left = C1; //B1->right = C2; //B2->left = C3; //B2->right = nullptr; //A->left = B1; //A->right = B2; ///*bool isSame = s->isSameTree(A, B); //std::cout << isSame << std::endl;*/ //bool isSymmetric = s->isSymmetric(A); //std::cout << isSymmetric << std::endl; //TreeNode* A = new TreeNode(3); //TreeNode* B1 = new TreeNode(9); //TreeNode* B2 = new TreeNode(20); //TreeNode* C1 = new TreeNode(15); //TreeNode* C2 = new TreeNode(7); //A->left = B1; //A->right = B2; //B1->left = nullptr; //B1->right = nullptr; //B2->left = C1; //B2->right = C2; ///*int depth = s->maxDepth(A); //std::cout << depth << std::endl;*/ //vector<vector<int>> vector = s->levelOrderBottom(A); //for (int i = 0; i < vector.size(); i++) //{ // for (int j = 0; j < vector[i].size(); j++) // { // cout << "第 " << i << " 层, 第 " << j << " 列 : " << vector[i][j] << std::endl; // } // cout << " " << std::endl; //} /*vector<int> vec = { -10, -3, 0, 5, 9 }; TreeNode* tree = s->sortedArrayToBST(vec); vector<vector<int>> vector = s->levelOrderBottom(tree); for (int i = 0; i < vector.size(); i++) { for (int j = 0; j < vector[i].size(); j++) { cout << "第 " << i << " 层, 第 " << j << " 列 : " << vector[i][j] << std::endl; } cout << " " << std::endl; }*/ /*TreeNode* A = new TreeNode(3); TreeNode* B1 = new TreeNode(9); TreeNode* B2 = new TreeNode(20); TreeNode* C1 = new TreeNode(15); TreeNode* C2 = new TreeNode(7); TreeNode* C3 = new TreeNode(13); A->left = B1; A->right = B2; B1->left = nullptr; B1->right = nullptr; B2->left = C1; B2->right = C2; bool isBalanced = s->isBalanced(A);*/ /*TreeNode* A = new TreeNode(1); TreeNode* B1 = new TreeNode(2); TreeNode* B2 = new TreeNode(2); TreeNode* C1 = new TreeNode(3); TreeNode* C2 = new TreeNode(3); TreeNode* D1 = new TreeNode(4); TreeNode* D2 = new TreeNode(4); C1->left = D1; C1->right = D2; A->left = B1; A->right = B2; B1->left = C1; B1->right = C2;*/ /*TreeNode* A = new TreeNode(1); TreeNode* B = new TreeNode(2); TreeNode* C = new TreeNode(3); TreeNode* D = new TreeNode(4); TreeNode* E = new TreeNode(5); A->left = B; A->right = nullptr; B->left = C; C->left = D; D->right = E;*/ /*TreeNode* A = new TreeNode(5); TreeNode* B1 = new TreeNode(4); TreeNode* B2 = new TreeNode(8); TreeNode* C1 = new TreeNode(11); TreeNode* C3 = new TreeNode(13); TreeNode* C4 = new TreeNode(4); TreeNode* D1 = new TreeNode(7); TreeNode* D2 = new TreeNode(2); TreeNode* D3 = new TreeNode(1); A->left = B1; A->right = B2; B1->left = C1; B2->left = C3; B2->right = C4; C1->left = D1; C1->right = D2; C4->right = D3;*/ /*bool isBalanced = s->isBalanced1(A); cout << " " << isBalanced << std::endl;*/ /*int depth = s->minDepth(A); cout << " " << depth << std::endl;*/ /*bool isExist = s->hasPathSum(A, 22); cout << " " << isExist << std::endl;*/ /*vector<vector<int>> res = s->generate(5); print(res);*/ /*vector<int> res = s->getRow(4); print(res);*/ //vector<int> prices{ 1,2,3,4,5 }; ////int max = s->maxProfit(prices); //int max1 = s->maxProfit1(prices); //cout << max1 << endl; /*bool flag = s->isPalindrome("A man, a plan, a canal: Panama"); cout << flag << endl;*/ /*vector<int> vec{ 3, 2, 2 }; int x = s->singleNumber(vec); cout << x << endl;*/ /*ListNode *A0 = new ListNode(0); ListNode *A1 = new ListNode(1); ListNode *A2 = new ListNode(2); ListNode *A3 = new ListNode(3); A0->next = A1; A1->next = A2; A2->next = A3; A3->next = nullptr; bool flag = s->hasCycle(A0); cout << flag << endl;*/ /*MinStack* obj = new MinStack(); obj->push(2); obj->push(0); obj->push(3); obj->push(0); cout << obj->getMin() << " " << endl; obj->pop(); cout << obj->getMin() << " " << obj->top() << endl; obj->pop(); cout << obj->getMin() << " " << obj->top() << endl;*/ /*ListNode* A1 = new ListNode(1); ListNode* A2 = new ListNode(2); ListNode* B1 = new ListNode(4); ListNode* B2 = new ListNode(1); ListNode* B3 = new ListNode(8); ListNode* C1 = new ListNode(4); ListNode* C2 = new ListNode(3); A1->next = A2; A2->next = C1; B1->next = B2; B2->next = B3; B3->next = C1; C1->next = C2; ListNode* node = s->getIntersectionNode(A1, B1); print(node);*/ /*vector<int> vec{ 2, 7, 11, 15 }; vector<int> res = s->twoSum(vec, 9); print(res);*/ /*string res = s->convertToTitle(52); cout << res << endl;*/ int sum = s->titleToNumber("ZY"); cout << sum << endl; system("pause"); return 0; }
//#include "stdafx.h"#include <iostream>#include <cstdlib>#include <stdio.h>#include <string>#include <vector>#include <stack>#include <queue>#include <sstream>using namespace std;
class ListNode {public:int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};
static void print(ListNode* node){if (node == nullptr){return;}std::cout << node->val << " " << std::endl;while(node->next != nullptr){node = node->next;std::cout << node->val << " " << std::endl;}}
struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};
static void print(TreeNode* node){if (node == nullptr){std::cout << " null " << std::endl;}std::cout << node->val << " " << std::endl;/*if (node->left != nullptr){node = node->next;std::cout << node->val << " " << std::endl;}*/}
static void print(vector<vector<int>> vec){for (int i = 0; i < vec.size(); i++){for (int j = 0; j < vec[i].size(); j++){cout << vec[i][j] << " " << endl;}cout << " 第 " << i << " 行 " << endl;cout << endl;}}
static void print(vector<int> vec){for (int j = 0; j < vec.size(); j++){cout << vec[j] << " " << endl;}}
class Solution{public:#pragma region RomanToInt/*给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1 :
输入: "abcabcbb"输出 : 3解释 : 因为无重复字符的最长子串是 "abc",所以其长度为 3。
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
*/
/*int lengthOfLongestSubstring(string s){for (int i = 0; i < s.length() - 1; i++){for (int j = 1; j < s.length() - 2; j++){string subStr = s.substr(i, j - i + 1);}}}*/int romanToInt(string s){int value = 0;int currentValue = 0;for (int i = 0; i < s.length(); i++){char ch = s[i];switch (ch){case 'I':if ((i + 1) <= s.length() - 1){switch (s[i + 1]){case 'V':i++;currentValue = 4;break;case 'X':i++;currentValue = 9;break;default:currentValue = 1;break;}}else{currentValue = 1;}break;case 'V':currentValue = 5;break;case 'X':if ((i + 1) <= s.length() - 1){switch (s[i + 1]){case 'L':i++;currentValue = 40;break;case 'C':currentValue = 90;i++;break;default:currentValue = 10;break;}}else{currentValue = 10;}break;case 'L':currentValue = 50;break;case 'C':if ((i + 1) <= s.length() - 1){switch (s[i + 1]){case 'D':i++;currentValue = 400;break;case 'M':currentValue = 900;i++;break;default:currentValue = 100;break;}}else{currentValue = 100;}break;case 'D':currentValue = 500;break;case 'M':currentValue = 1000;break;}value += currentValue;}return value;}#pragma endregion
ListNode* deleteDuplicates(ListNode* head) {/*if (head == nullptr){return nullptr;}*/ListNode* HeadNode = head;while (head != nullptr && head->next != nullptr){if (head->next->val == head->val){head->next = head->next->next;}else{head = head->next;}}return HeadNode;}
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {if (n == 0){return;}if (m == 0){nums1 = nums2;return;}int p1 = m - 1;int p2 = n - 1;int p = m + n - 1;while ((p1 >= 0) && (p2 >= 0)){nums1[p--] = nums1[p1] >= nums2[p2] ? nums1[p1--] : nums2[p2--];}while (p2 >= 0){nums1[p--] = nums2[p2--];}while (p1 >= 0){nums1[p--] = nums1[p1--];}}
string longestCommonPrefix(vector<string>& strs) {
}
bool isSameTree(TreeNode* p, TreeNode* q) {if (p == nullptr && q == nullptr){return true;}if (p == nullptr || q == nullptr){return false;}if (p->val != q->val){return false;}return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);}
bool isMirror(TreeNode* t1, TreeNode* t2){if (t1 == nullptr && t2 == nullptr){return true;}if (t1 == nullptr || t2 == nullptr){return false;}
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);}
bool isSymmetric(TreeNode* root) {return isMirror(root, root);}
int maxDepth(TreeNode* root) {if (root == nullptr){return 0;}int leftDepth = maxDepth(root->left);int rightDepth = maxDepth(root->right);return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);}
string addBinary(string a, string b) {if (a.empty() && b.empty()){return "0";}if (a.empty()){return b;}if (b.empty()){return a;}int aLen = a.length();int bLen = b.length();int cLen = aLen >= bLen ? aLen : bLen;cLen += 1;string c(cLen, '0');int sum = 0;int carry = 0;int curAIndex = 0;int curBIndex = 0;int curAValue = 0;int curBValue = 0;for (int i = 0; i < cLen - 1; i++){curAIndex = aLen - 1 - i;curBIndex = bLen - 1 - i;curAValue = curAIndex < 0 ? 0 : a[curAIndex] - '0';curBValue = curBIndex < 0 ? 0 : b[curBIndex] - '0';sum = (curAValue + curBValue + carry) % 2;carry = (curAValue + curBValue + carry) / 2;c[cLen - 1 - i] = sum + '0';}if (carry == 0){c = c.substr(1);}else{c[0] = carry + '0';}return c;}
string addBinaryNew(string a, string b){int aLen = a.length();int bLen = b.length();while (aLen < bLen){a = '0' + a;aLen++;}while (bLen < aLen){b = '0' + b;bLen++;}for (int i = aLen - 1; i > 0; i--){a[i - 1] = ((a[i] - '0') + (b[i] - '0')) / 2 + a[i - 1];a[i] = ((a[i] - '0') + (b[i] - '0')) % 2 + '0';}a[0] = a[0] + b[0] - '0';if (a[0] >= '2'){a[0] = (a[0] - '0') % 2 + '0';a = '1' + a;}return a;}
int mySqrt(int x){
if (x == 0){return x;}else if (x == 2 || x == 3){return 1;}else if (x == 4){return 2;}else{int left = 0;int right = (x == 2147483647) ? (x/2+1) : (x + 1) / 2;int value = (left + right) / 2;value = (value > 46340) ? 46340 : value;int squre = value * value;while (left < right){if (squre < x){if (value >= 46340){return 46340;}left = value;value = (left + right + 1) / 2;}else if (value * value > x){right = value - 1;value = (left + right + 1) / 2;}else{return value;}squre = value * value;}return left;}}
int climbStairs(int n) {if (n <= 2){return n;}int first = 1;int second = 2;int value = 0;for (int i = 3; i <= n; i++){value = first + second;first = second;second = value;}return second;}
vector<vector<int>> levelOrderBottom(TreeNode* root) {vector<int> vec;vector<vector<int>> result;if (root == nullptr){return result;}
queue<TreeNode*> queue;queue.push(root);while (!queue.empty()){vector<int> oneLevel;int size = queue.size();for (int i = 0; i < size; i++){TreeNode* node = queue.front();oneLevel.push_back(node->val);queue.pop();if (node->left != nullptr){queue.push(node->left);}if (node->right != nullptr){queue.push(node->right);}}result.insert(result.begin(), oneLevel);}return result;}
TreeNode* sortedArrayToBST(vector<int>& nums) {if (nums.size() == 0){return nullptr;}if (nums.size() == 1){TreeNode* A = new TreeNode(nums[0]);return A;}int num = nums.size() / 2;std::vector<int> leftVector(num);std::copy(nums.begin(), nums.begin() + num, leftVector.begin());std::vector<int> rightVector(nums.size() - num - 1);std::copy(nums.begin() + num + 1, nums.end(), rightVector.begin());TreeNode* A = new TreeNode(nums[num]);A->left = sortedArrayToBST(leftVector);A->right = sortedArrayToBST(rightVector);return A;}
bool isBalanced(TreeNode* root) {if (root == nullptr){return true;}if (root->left == nullptr && root->right == nullptr){return true;}int leftHeight = maxDepth(root->left);int rightHeight = maxDepth(root->right);int sub = leftHeight > rightHeight ? leftHeight - rightHeight : rightHeight - leftHeight;return sub <= 1 && isBalanced(root->left) && isBalanced(root->right);}
bool isBalanced1(TreeNode* root) {bool balance = false;int h = 0;return Balance(root, h);}bool Balance(TreeNode* root, int &h){if (root == NULL){h = 0;return true;}if (root->left == NULL && root->right == NULL){h = 1;return true;}int hr, hl;bool br = Balance(root->left, hl);bool bl = Balance(root->right, hr);h = (hl > hr ? hl : hr) + 1;//三目运算符if (abs(hl - hr) < 2)return br & bl;return false;}
int minDepth(TreeNode* root) {if (root == nullptr){return 0;}if (root->left == nullptr && root->right == nullptr){return 1;}if (root->left == nullptr && root->right != nullptr){return minDepth(root->right) + 1;}if (root->left != nullptr && root->right == nullptr){return minDepth(root->left) + 1;}int depthL = minDepth(root->left);int depthR = minDepth(root->right);int depth = depthL > depthR ? depthR : depthL;depth += 1;return depth;}
bool hasPathSum(TreeNode* root, int sum) {if (root == nullptr){return false;}if (root->left == nullptr && root->right == nullptr){if (root->val == sum)return true;elsereturn false;}if (root->left == nullptr && root->right != nullptr){return hasPathSum(root->right, sum - root->val);}if (root->left != nullptr && root->right == nullptr){return hasPathSum(root->left, sum - root->val);}if (root->left != nullptr && root->right != nullptr){return hasPathSum(root->right, sum - root->val) || hasPathSum(root->left, sum - root->val);}}
vector<vector<int>> generate(int numRows) {if (numRows == 0){vector<vector<int>> result{};return result;}if (numRows == 1){vector<int> row{ 1 };vector<vector<int>> result{ row };return result;}if (numRows == 2){vector<int> row0{ 1 };vector<int> row1{ 1 , 1};vector<vector<int>> result{ row0, row1 };return result;}vector<vector<int>> resultRowM1 = generate(numRows - 1);vector<int> RowM(numRows, 1);vector<int> RowM1 = resultRowM1[numRows - 2];for (int i = 1; i < (numRows + 1)/2; i++){RowM[i] = RowM1[i - 1] + RowM1[i];RowM[numRows - 1 - i] = RowM[i];}resultRowM1.push_back(RowM);return resultRowM1;}
vector<int> getRow(int rowIndex) {if (rowIndex == 0){vector<int> row{ 1 };return row;}if (rowIndex == 1){vector<int> row1{ 1 , 1 };return row1;}if (rowIndex == 2){vector<int> row1{ 1 , 2, 1 };return row1;}vector<int> RowM1 = getRow(rowIndex - 1);RowM1.push_back(1);for (int i = rowIndex / 2; i >= 1; i--){RowM1[i] = RowM1[i - 1] + RowM1[i];RowM1[rowIndex - i] = RowM1[i];}return RowM1;}int maxProfit(vector<int>& prices) {int max = 0;for (int i = 0; i < prices.size(); i++){for (int j = i + 1; j < prices.size(); j++){if (prices[j] - prices[i] > max){max = prices[j] - prices[i];}}}return max;}
int maxProfit1(vector<int>& prices) {if (prices.size() <= 0)return 0;int min = prices[0];int sum = 0;for (int i = 1; i < prices.size(); i++){if (prices[i] > min){sum += prices[i] - min;min = prices[i];}if (prices[i] < min){min = prices[i];}}return sum;}
bool isNumOrCharacter(char& ch){return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'));}
void turnLowerCharacter(char& ch){if (ch >= 'A' && ch <= 'Z')ch = ch + 'a' - 'A';return;}
bool isPalindrome(string s) {if (s.size() == 0)return true;for (int i = 0, j = s.size() - 1; i <= j;){if (!isNumOrCharacter(s[i])){i++;continue;}if (!isNumOrCharacter(s[j])){j--;continue;}if (s[i] >= '0' && s[i] <= '9'){if (s[i] != s[j])return false;else {i++;j--;}}else {turnLowerCharacter(s[i]);turnLowerCharacter(s[j]);if (s[i] != s[j])return false;else {i++;j--;}}}return true;}
int singleNumber(vector<int>& nums) {if (nums.size() == 0){return 0;}if (nums.size() == 1){return nums[0];}int sum = nums[0];for (int i = 1; i < nums.size(); i++){sum ^= nums[i];}return sum;}
bool hasCycle(ListNode *head) {ListNode* head1 = head;while (head1 != nullptr && head->next != nullptr && head1->next != nullptr){head = head->next;head1 = head1->next->next;if (head == head1)return true;}return false;}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode *headBPtr = headB;ListNode *headAPtr = headA;while (headAPtr != headBPtr){headAPtr = headAPtr == nullptr ? headB : headAPtr->next;headBPtr = headBPtr == nullptr ? headA : headBPtr->next;}return headAPtr;}
vector<int> twoSum(vector<int>& numbers, int target) {vector<int> sum;for (int i = 0, j = numbers.size() - 1; i <= j;){if (numbers[i] + numbers[j] < target){i++;}else if (numbers[i] + numbers[j] > target){j--;}else{sum.push_back(i+1);sum.push_back(j+1);return sum;}}return sum;}
string convertToTitle(int n) {string res;while (n){char c = 'A' + --n % 26;; // 'Z';res += c;n = n / 26;}reverse(res.begin(), res.end());return res;}
int titleToNumber(string s) {int sum = 0;for (int i = 0; i < s.size(); i++){sum = sum * 26 + s[i] - 'A' + 1;}return sum;}
};
class MinStack {public:/** initialize your data structure here. */MinStack() {mList = NULL;}
void push(int x) {mStack.push(x);if (mList == nullptr){mList = new ListNode(x);mList->val = x;mList->next = nullptr;return;}if (mList->val >= x){ListNode* node = new ListNode(x);node->next = mList;mList = node;return;}else{ListNode* prev = mList;ListNode* current = mList->next;while (prev->val <= x){if (current == nullptr){ListNode* node = new ListNode(x);prev->next = node;return;}else{if (current->val <= x){current = current->next;prev = prev->next;}else{ListNode* node = new ListNode(x);node->next = current;prev->next = node;return;}}}}}
void pop() {int top = mStack.top();mStack.pop();if (mList->val == top){mList = mList->next;return;}ListNode * prev = mList;ListNode * current = mList;while (current != nullptr){if (current->val == top){prev->next = current->next;current = nullptr;delete current;return;}else{prev = prev->next;current = current->next;}}}
int top() {return mStack.top();}
int getMin() {return mList->val;}private:stack<int> mStack;ListNode* mList;};
int main(int, char[]){Solution *s = new Solution();//string str = "MCMXCIV";//int value = s->romanToInt(str); // III:3, IV:4, IX:9, LVIII:58, MCMXCIV:1994//string value = s->addBinaryNew("1001", "1011");//int sqrt = s->mySqrt(2147483647);//int stairs = s->climbStairs(3);//printf("Hello: %s!\n", value);//std::cout << "++++++ +++++: " << stairs;
/*ListNode* node = new ListNode(1);ListNode* node1 = new ListNode(1);ListNode* node2 = new ListNode(2);ListNode* node3 = new ListNode(3);ListNode* node4 = new ListNode(3);node->next = node1;node1->next = node2;node2->next = node3;node3->next = node4;ListNode* nodeResult = s->deleteDuplicates(node);print(nodeResult);*/
/*vector<int> a = { 1, 2, 3, 0, 0, 0 };vector<int> b = { 2, 5, 6 };s->merge(a, 3, b, 3);for (int i = 0; i < a.size(); i++){std::cout << "++++++ +++++: " << a[i] << std::endl;}*/
/*TreeNode{int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}*/
//TreeNode* A = new TreeNode(1);//TreeNode* B1 = new TreeNode(2);//TreeNode* B2 = new TreeNode(2);
//TreeNode* C1 = new TreeNode(3);//TreeNode* C2 = new TreeNode(4);//TreeNode* C3 = new TreeNode(4);//TreeNode* C4 = new TreeNode(3);
//B1->left = C1;//B1->right = C2;
//B2->left = C3;//B2->right = nullptr;
//A->left = B1;//A->right = B2;///*bool isSame = s->isSameTree(A, B);//std::cout << isSame << std::endl;*/
//bool isSymmetric = s->isSymmetric(A);//std::cout << isSymmetric << std::endl;
//TreeNode* A = new TreeNode(3);//TreeNode* B1 = new TreeNode(9);//TreeNode* B2 = new TreeNode(20);
//TreeNode* C1 = new TreeNode(15);//TreeNode* C2 = new TreeNode(7);
//A->left = B1;//A->right = B2;
//B1->left = nullptr;//B1->right = nullptr;
//B2->left = C1;//B2->right = C2;
///*int depth = s->maxDepth(A);//std::cout << depth << std::endl;*/
//vector<vector<int>> vector = s->levelOrderBottom(A);//for (int i = 0; i < vector.size(); i++)//{//for (int j = 0; j < vector[i].size(); j++)//{//cout << "第 " << i << " 层, 第 " << j << " 列 : " << vector[i][j] << std::endl;//}//cout << " " << std::endl;//}
/*vector<int> vec = { -10, -3, 0, 5, 9 };TreeNode* tree = s->sortedArrayToBST(vec);
vector<vector<int>> vector = s->levelOrderBottom(tree);for (int i = 0; i < vector.size(); i++){for (int j = 0; j < vector[i].size(); j++){cout << "第 " << i << " 层, 第 " << j << " 列 : " << vector[i][j] << std::endl;}cout << " " << std::endl;}*/
/*TreeNode* A = new TreeNode(3);TreeNode* B1 = new TreeNode(9);TreeNode* B2 = new TreeNode(20);
TreeNode* C1 = new TreeNode(15);TreeNode* C2 = new TreeNode(7);
TreeNode* C3 = new TreeNode(13);A->left = B1;A->right = B2;
B1->left = nullptr;B1->right = nullptr;
B2->left = C1;B2->right = C2;bool isBalanced = s->isBalanced(A);*/
/*TreeNode* A = new TreeNode(1);TreeNode* B1 = new TreeNode(2);TreeNode* B2 = new TreeNode(2);
TreeNode* C1 = new TreeNode(3);TreeNode* C2 = new TreeNode(3);
TreeNode* D1 = new TreeNode(4);TreeNode* D2 = new TreeNode(4);
C1->left = D1;C1->right = D2;
A->left = B1;A->right = B2;
B1->left = C1;B1->right = C2;*/
/*TreeNode* A = new TreeNode(1);
TreeNode* B = new TreeNode(2);TreeNode* C = new TreeNode(3);TreeNode* D = new TreeNode(4);TreeNode* E = new TreeNode(5);A->left = B;A->right = nullptr;
B->left = C;
C->left = D;D->right = E;*/
/*TreeNode* A = new TreeNode(5);
TreeNode* B1 = new TreeNode(4);TreeNode* B2 = new TreeNode(8);
TreeNode* C1 = new TreeNode(11);TreeNode* C3 = new TreeNode(13);TreeNode* C4 = new TreeNode(4);
TreeNode* D1 = new TreeNode(7);TreeNode* D2 = new TreeNode(2);TreeNode* D3 = new TreeNode(1);
A->left = B1;A->right = B2;
B1->left = C1;
B2->left = C3;B2->right = C4;
C1->left = D1;C1->right = D2;
C4->right = D3;*/
/*bool isBalanced = s->isBalanced1(A);cout << " " << isBalanced << std::endl;*/
/*int depth = s->minDepth(A);cout << " " << depth << std::endl;*//*bool isExist = s->hasPathSum(A, 22);cout << " " << isExist << std::endl;*/
/*vector<vector<int>> res = s->generate(5);print(res);*/
/*vector<int> res = s->getRow(4);print(res);*/
//vector<int> prices{ 1,2,3,4,5 };////int max = s->maxProfit(prices);//int max1 = s->maxProfit1(prices);//cout << max1 << endl;
/*bool flag = s->isPalindrome("A man, a plan, a canal: Panama");cout << flag << endl;*/
/*vector<int> vec{ 3, 2, 2 };int x = s->singleNumber(vec);cout << x << endl;*/
/*ListNode *A0 = new ListNode(0);ListNode *A1 = new ListNode(1);ListNode *A2 = new ListNode(2);ListNode *A3 = new ListNode(3);A0->next = A1;A1->next = A2;A2->next = A3;A3->next = nullptr;bool flag = s->hasCycle(A0);cout << flag << endl;*/
/*MinStack* obj = new MinStack();obj->push(2);obj->push(0);obj->push(3);obj->push(0);cout << obj->getMin() << " " << endl;obj->pop();cout << obj->getMin() << " " << obj->top() << endl;obj->pop();cout << obj->getMin() << " " << obj->top() << endl;*/
/*ListNode* A1 = new ListNode(1);ListNode* A2 = new ListNode(2);ListNode* B1 = new ListNode(4);ListNode* B2 = new ListNode(1);ListNode* B3 = new ListNode(8);ListNode* C1 = new ListNode(4);ListNode* C2 = new ListNode(3);A1->next = A2;A2->next = C1;B1->next = B2;B2->next = B3;B3->next = C1;C1->next = C2;ListNode* node = s->getIntersectionNode(A1, B1);print(node);*/
/*vector<int> vec{ 2, 7, 11, 15 };vector<int> res = s->twoSum(vec, 9);print(res);*/
/*string res = s->convertToTitle(52);cout << res << endl;*/
int sum = s->titleToNumber("ZY");cout << sum << endl;system("pause");return 0;}