摘要: Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE" 阅读全文
posted @ 2014-04-04 19:57 Eason Liu 阅读(220) 评论(0) 推荐(0) 编辑
摘要: Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3- 阅读全文
posted @ 2014-04-04 18:37 Eason Liu 阅读(142) 评论(0) 推荐(0) 编辑
摘要: Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ≤m≤n≤ length of list.no words!!!! 1 /** 2 * Definition for singly-linked li 阅读全文
posted @ 2014-04-04 18:09 Eason Liu 阅读(203) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values in the list, only nodes itself can be changed.链表神马的完全是我的克星啊。没有算法,好好 阅读全文
posted @ 2014-04-04 17:29 Eason Liu 阅读(139) 评论(0) 推荐(0) 编辑
摘要: 题目描述:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:1 2 3 45 6 7 89 10 11 1213 14 15 16则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.输入:输入可能包含多个测试样例,对于每个测试案例,输入的第一行包括两个整数m和n(1 2 #include 3 using namespace std; 4 5 int a[1000][1000]; 6 7 void print(int x, int y, int m, int n) 8 { 9 for (int i ... 阅读全文
posted @ 2014-04-04 16:05 Eason Liu 阅读(235) 评论(0) 推荐(0) 编辑
摘要: 题目描述:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。输入:每个测试案例包括1行。输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。输出:对应每组数据,按字典序输出所有排列。样例输入:abcBCA样例输出:abcacbbacbcacabcbaABCACBBACBCACABCBA再复习一下next_permutation,最后一组测试数据9位,用cout超时,关键时候还是得printf啊。 1 #include 2 #include 3 #in.. 阅读全文
posted @ 2014-04-04 15:58 Eason Liu 阅读(532) 评论(0) 推荐(0) 编辑
摘要: 题目描述:在一个字符串(1 2 #include 3 #include 4 using namespace std; 5 6 int main() { 7 string s; 8 int a[256]; 9 while (cin >> s) {10 memset(a, 0, sizeof(a));11 int t = 0;12 for (int i = 0; i < s.length(); ++i) {13 ++a[s[i]];14 }15 bool flag... 阅读全文
posted @ 2014-04-04 15:32 Eason Liu 阅读(244) 评论(0) 推荐(0) 编辑
摘要: 题目描述:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。输入:每个输入文件包含一组测试案例。对于每个测试案例,第一行输入一个n,代表该数组中数字的个数。接下来的一行输入n个整数。代表数组中的n个数。输出:对应每个测试案例,输入一行n个数字,代表调整后的数组。注意,数字和数字之间用一个空格隔开,最后一个数字后面没有空格。样例输入:51 2 3 4 5样例输出:1 3 5 2 4对于普通的交换顺序,只要设两个指针分别从头跟尾扫描,当分别遇到不符合条件的位置时停止,然后交换位 阅读全文
posted @ 2014-04-04 15:09 Eason Liu 阅读(2966) 评论(4) 推荐(0) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.跟上一题一样,只是数据结构换成了链表,这里求链表的中位数还是乖乖用len先计算长度吧,用slow、fast指针还得先标记尾指针,反而更麻烦。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ... 阅读全文
posted @ 2014-04-04 01:22 Eason Liu 阅读(166) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.哎,又是被指针坑了,指针作为函数的参数传递时,在函数体内可以修改指针所指的内容,但是不有修改指针本身,所以一时是运行时错误,这里要用指针的引用TreeNode *&,问题得解。算法没什么好说的,递归求解,每次取传入列表的中位数为节点的值。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 *... 阅读全文
posted @ 2014-04-04 00:57 Eason Liu 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?中序遍历,对于BST,其中序序列是有序的,所以只要找出中序序列中的逆序对,但是要注意保存中间节点,可以使用全局变量,也可以使用引用。 1 /** 2 * Defi. 阅读全文
posted @ 2014-04-04 00:49 Eason Liu 阅读(252) 评论(0) 推荐(0) 编辑