摘要: 原题地址罗马数字的问题可以参考这篇博文,讲的很清楚代码: 1 int transfer(char c) { 2 switch (c) { 3 case 'I': return 1; 4 case 'V': return 5; 5 case 'X': return 10; 6 ca... 阅读全文
posted @ 2015-02-02 19:52 李舜阳 阅读(431) 评论(0) 推荐(0)
摘要: 原题地址辅助栈代码: 1 bool isValid(string s) { 2 stack st; 3 4 for (auto c : s) { 5 if (st.empty()) 6 st.p... 阅读全文
posted @ 2015-02-02 19:21 李舜阳 阅读(191) 评论(0) 推荐(0)
摘要: 原题地址如果不使用额外的空间,可以先将A数组整体向后挪n个单位,然后开始常规的merge操作代码: 1 void merge(int A[], int m, int B[], int n) { 2 int i = m - 1; 3 int j = 0; 4 ... 阅读全文
posted @ 2015-02-02 19:05 李舜阳 阅读(140) 评论(0) 推荐(0)
摘要: 原题地址注意从1开始索引,所以记得+1代码: 1 int titleToNumber(string s) { 2 int res = 0; 3 4 for (int i = 0; i < s.length(); i++) { 5 ... 阅读全文
posted @ 2015-02-02 18:53 李舜阳 阅读(178) 评论(0) 推荐(0)
摘要: 原题地址26进制数注意,编号是从1开始的,所以取模取商之前要-1代码: 1 string convertToTitle(int n) { 2 string res; 3 4 while (n) { 5 res = (char)... 阅读全文
posted @ 2015-02-02 18:46 李舜阳 阅读(150) 评论(0) 推荐(0)
摘要: 原题地址先将A链末尾和B链头部接起来然后判断是否有环,如果无环,说明肯定不想交,如果有环,那么相交的位置就是环开始的位置第一遍做的时候没遇到什么问题,第二遍做的时候各种出错,后来发现原来在用快慢指针法的时候快慢指针要从起点开始,否则计算出来的第一个相交位置不是环开始的位置。。代码: 1 ListNo... 阅读全文
posted @ 2015-02-02 18:22 李舜阳 阅读(202) 评论(0) 推荐(0)
摘要: 原题地址找规律题代码: 1 string convert(string s, int nRows) { 2 string res; 3 4 if (nRows <= 1) 5 return s; 6 7 ... 阅读全文
posted @ 2015-02-02 16:57 李舜阳 阅读(180) 评论(0) 推荐(0)
摘要: 原题地址非常恶心的一道题,又是处理溢出问题根据经验,处理溢出问题应该:优先在还没溢出的时候检测运算后的结果是否会溢出,而不是等到溢出以后再去判断结果是否溢出比如,为了判断F(x)是否会溢出,应该推算出x的合法范围,当x不在合法范围内时,判定溢出。而不是计算出F(x)的值之后再判断是否会溢出。有时候计... 阅读全文
posted @ 2015-02-02 16:26 李舜阳 阅读(223) 评论(0) 推荐(0)
摘要: 原题地址注意溢出问题代码: 1 int reverse(int x) { 2 int newx = 0; 3 4 while (x) { 5 if ((newx * 10 / 10) != newx) 6 ... 阅读全文
posted @ 2015-02-02 15:56 李舜阳 阅读(149) 评论(0) 推荐(0)
摘要: 原题地址方法I:传统的判断回文的方法,左右指针,不断收缩判断方法II:将数字翻转,判断翻转后的数字是否相等,溢出也没关系因为溢出以后更加不可能相等了代码(方法II): 1 bool isPalindrome(int x) { 2 int oldx = x; 3 int... 阅读全文
posted @ 2015-02-02 15:38 李舜阳 阅读(163) 评论(0) 推荐(0)
摘要: 原题地址isalnum:判断是否是字符或数字toupper:将字符转换成大写,非字符不变代码: 1 bool isPalindrome(string s) { 2 string news; 3 for (auto c : s) 4 if (is... 阅读全文
posted @ 2015-02-02 15:28 李舜阳 阅读(135) 评论(0) 推荐(0)
摘要: 原题地址从编号为0开始,不断递推到第k个如果用p[i][j]表示第i层,第j个数字,则有递推公式:p[i][j] = p[i-1][j-1] + p[i-1][j]因为只使用了相邻层,因此可以压缩状态空间代码: 1 vector getRow(int rowIndex) { 2 if... 阅读全文
posted @ 2015-02-02 15:20 李舜阳 阅读(125) 评论(0) 推荐(0)
摘要: 原题地址基本模拟题代码: 1 vector > generate(int numRows) { 2 vector > res; 3 4 if (numRows (1, 1)); 8 while (--numRows) { 9 ... 阅读全文
posted @ 2015-02-02 14:08 李舜阳 阅读(153) 评论(0) 推荐(0)
摘要: 原题地址基本模拟代码: 1 ListNode *removeNthFromEnd(ListNode *head, int n) { 2 ListNode *fast = head; 3 ListNode *slow = head; 4 ListNode... 阅读全文
posted @ 2015-02-02 14:01 李舜阳 阅读(133) 评论(0) 推荐(0)
摘要: 原题地址注意空树即使sum=0也不算代码: 1 bool solve(TreeNode *root, int sum) { 2 if (!root->left && !root->right) 3 return sum == root->val; 4 5 return (root-... 阅读全文
posted @ 2015-02-02 12:05 李舜阳 阅读(146) 评论(0) 推荐(0)
摘要: 原题地址基本二叉树遍历代码:1 int minDepth(TreeNode *root) {2 if (!root)3 return 0;4 5 int l = root->left ? minDepth(root->l... 阅读全文
posted @ 2015-02-02 11:47 李舜阳 阅读(140) 评论(0) 推荐(0)
摘要: 原题地址参考了这篇博文的想法代码: 1 int balancedp(TreeNode *root) { 2 if (!root) 3 return 0; 4 5 int l = balancedp(root->left); 6 int r = balancedp(root->r... 阅读全文
posted @ 2015-02-02 11:38 李舜阳 阅读(172) 评论(0) 推荐(0)
摘要: 原题地址二叉树层次遍历,最后把遍历结果翻转一下即可代码: 1 vector > levelOrderBottom(TreeNode *root) { 2 vector > res; 3 queue layer; 4 5 layer.p... 阅读全文
posted @ 2015-02-02 11:16 李舜阳 阅读(235) 评论(0) 推荐(0)
摘要: 原题地址二叉树的遍历代码:1 int maxDepth(TreeNode *root) {2 if (!root)3 return 0;4 return max(maxDepth(root->left), maxDepth(root->righ... 阅读全文
posted @ 2015-02-02 11:11 李舜阳 阅读(168) 评论(0) 推荐(0)
摘要: 原题地址二叉树的层次遍历代码: 1 vector > levelOrder(TreeNode *root) { 2 vector > res; 3 queue layer; 4 5 layer.push(root); 6 ... 阅读全文
posted @ 2015-02-02 11:09 李舜阳 阅读(159) 评论(0) 推荐(0)
摘要: 原题地址递归比较左儿子和右儿子是否对称,左儿子的左儿子跟右儿子的右儿子比,左儿子的右儿子跟右儿子的左儿子比!打完这一串文字突然发现"儿"字怎么这么奇怪!代码: 1 bool mirrorp(TreeNode *a, TreeNode *b) { 2 if (a && b) 3 retur... 阅读全文
posted @ 2015-02-02 11:04 李舜阳 阅读(228) 评论(0) 推荐(0)
摘要: 原题地址基本模拟题代码:1 bool isSameTree(TreeNode *p, TreeNode *q) {2 if (p && q)3 return p->val == q->val && isSameTree(p->left, q->left) &&... 阅读全文
posted @ 2015-02-02 10:55 李舜阳 阅读(152) 评论(0) 推荐(0)
摘要: 原题地址基本模拟题代码: 1 int removeDuplicates(int A[], int n) { 2 if (n <= 0) 3 return 0; 4 5 int i = 1; 6 int ... 阅读全文
posted @ 2015-02-02 10:51 李舜阳 阅读(160) 评论(0) 推荐(0)
摘要: 原题地址基本模拟题,双指针法代码: 1 int removeElement(int A[], int n, int elem) { 2 int i = 0; 3 int j = 0; 4 5 while (i < n) { 6 ... 阅读全文
posted @ 2015-02-02 10:47 李舜阳 阅读(153) 评论(0) 推荐(0)
摘要: 原题地址基本模拟,记得最后把尾节点next置为NULL...代码: 1 ListNode *deleteDuplicates(ListNode *head) { 2 ListNode *h = NULL; 3 ListNode *t = NULL; 4 ... 阅读全文
posted @ 2015-02-02 10:42 李舜阳 阅读(170) 评论(0) 推荐(0)
摘要: 原题地址基本动归题可以压缩状态空间代码: 1 int climbStairs(int n) { 2 if (n <= 0) 3 return 0; 4 5 int count = 1; 6 int tm... 阅读全文
posted @ 2015-02-02 10:33 李舜阳 阅读(175) 评论(0) 推荐(0)
摘要: 原题地址简单模拟代码: 1 vector plusOne(vector &digits) { 2 vector sum(digits.size(), 0); 3 int carry = 1; 4 5 for (int i = digi... 阅读全文
posted @ 2015-02-02 10:19 李舜阳 阅读(233) 评论(0) 推荐(0)
摘要: 原题地址按照数独规则判断即可代码: 1 bool isValidSudoku(vector > &board) { 2 unordered_set record; 3 4 for (int i = 0; i < 9; i++) { 5 ... 阅读全文
posted @ 2015-02-02 10:10 李舜阳 阅读(192) 评论(0) 推荐(0)
摘要: 原题地址简单模拟题代码: 1 string addBinary(string a, string b) { 2 int i = a.length() - 1; 3 int j = b.length() - 1; 4 int carry = 0; 5 ... 阅读全文
posted @ 2015-02-02 09:43 李舜阳 阅读(165) 评论(0) 推荐(0)
摘要: 原题地址链表归并排序的一个小函数代码: 1 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 2 ListNode *h = NULL; 3 ListNode *t = NULL; 4 5 ... 阅读全文
posted @ 2015-02-02 09:28 李舜阳 阅读(134) 评论(0) 推荐(0)
摘要: 原题地址简单模拟题代码: 1 int lengthOfLastWord(const char *s) { 2 char prev = ' '; 3 int length = 0; 4 5 while (*s) { 6 ... 阅读全文
posted @ 2015-02-02 09:23 李舜阳 阅读(183) 评论(0) 推荐(0)
摘要: 原题地址简单模拟题从1开始推即可。不知道有没有规律可以直接求出n代码: 1 string countAndSay(int n) { 2 if (n <= 0) 3 return ""; 4 5 string str =... 阅读全文
posted @ 2015-02-02 09:13 李舜阳 阅读(158) 评论(0) 推荐(0)