摘要: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes a word?A sequence of non-space characters constitutes a word.Could the input string contain leading 阅读全文
posted @ 2014-04-01 12:59 懒猫欣 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 找规律题现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … 3/1 3/2 3/3 … 4/1 4/2 … 5/1 … … 我们以Z字形给上表的每一项编号。第一项是1/1,然后是1/2,2/1,3/1,2/2,…从图中我们可以看出,如果一个数是第i行的第j个数(从右上到左下)那么该数可以表示为j/i-j+1因此求出第n个数的i和j就可以得出这个数#include#includeusing namespace std;int main(){ int n;... 阅读全文
posted @ 2014-01-08 16:20 懒猫欣 阅读(118) 评论(0) 推荐(0) 编辑
摘要: ictclas使用http://blog.csdn.net/hellonlp/article/details/8792667在使用ictclas的文件分词功能时发现有乱码,原因是其文件读写功能有问题,通过字符串分词自己实现文件分词就可以了 阅读全文
posted @ 2013-12-11 11:20 懒猫欣 阅读(118) 评论(0) 推荐(0) 编辑
摘要: Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", " 阅读全文
posted @ 2013-12-06 19:50 懒猫欣 阅读(173) 评论(0) 推荐(0) 编辑
摘要: Sort a linked list in O(n log n) time using constant space complexity.由于链表的特性,不能快速查找,但是可以快速移动元素,通过归并排序可以实现O(nlogn)复杂度内的排序/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: L... 阅读全文
posted @ 2013-12-04 12:17 懒猫欣 阅读(209) 评论(0) 推荐(0) 编辑
摘要: Sort a linked list using insertion sort./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *insertionSortList(ListNode *head) { // IMPORTANT: Please reset an... 阅读全文
posted @ 2013-11-18 15:47 懒猫欣 阅读(247) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode... 阅读全文
posted @ 2013-11-13 11:03 懒猫欣 阅读(237) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial, could you do it iteratively?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode ... 阅读全文
posted @ 2013-11-13 10:57 懒猫欣 阅读(212) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4,2,3}.确定链表长度后将后半段反序再与前半段合并就可以了,记住前后两段一定要断开不然会产生环/** * Definition for singly-linked list. * struct ListNo 阅读全文
posted @ 2013-11-13 10:46 懒猫欣 阅读(155) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space?首先确定是否有环,并找到环上一点,通过该点得到环的长度,用两个差为环长度的指针来探测环的起点。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ... 阅读全文
posted @ 2013-11-13 10:14 懒猫欣 阅读(141) 评论(0) 推荐(0) 编辑