上一页 1 ··· 19 20 21 22 23 24 25 26 27 ··· 30 下一页
摘要: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.最开始的想法就是暴力复制,时间复杂度为O(n^2),写的时候就感觉要出现事,果不其然,超时了,后来网上看到一个O(n)的算法,非常巧妙的利用了原来链表的信息:该算法更为巧妙,不用保存原始链表的映射关系,构建新节点时,指针做如下变化,即把新节点插入到相应的旧节点后面:同理分两步 阅读全文
posted @ 2014-04-05 13:34 Eason Liu 阅读(4298) 评论(3) 推荐(0) 编辑
摘要: Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened t... 阅读全文
posted @ 2014-04-05 01:11 Eason Liu 阅读(1361) 评论(0) 推荐(0) 编辑
摘要: Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", &q 阅读全文
posted @ 2014-04-05 00:33 Eason Liu 阅读(215) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(223) 评论(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 阅读(144) 评论(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 阅读(140) 评论(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 阅读(245) 评论(0) 推荐(0) 编辑
上一页 1 ··· 19 20 21 22 23 24 25 26 27 ··· 30 下一页