随笔分类 -  Algorithm

上一页 1 ··· 8 9 10 11 12 13 14 下一页

[Leetcode] Swap Nodes in Pairs
摘要: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 阅读(150) 评论(0) 推荐(0)

[Jobdu] 题目1369:字符串的排列
摘要:题目描述:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串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 阅读(544) 评论(0) 推荐(0)

[Jobdu] 题目1283:第一个只出现一次的字符
摘要:题目描述:在一个字符串(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 阅读(256) 评论(0) 推荐(0)

[Jobdu] 题目1516:调整数组顺序使奇数位于偶数前面
摘要:题目描述:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。输入:每个输入文件包含一组测试案例。对于每个测试案例,第一行输入一个n,代表该数组中数字的个数。接下来的一行输入n个整数。代表数组中的n个数。输出:对应每个测试案例,输入一行n个数字,代表调整后的数组。注意,数字和数字之间用一个空格隔开,最后一个数字后面没有空格。样例输入:51 2 3 4 5样例输出:1 3 5 2 4对于普通的交换顺序,只要设两个指针分别从头跟尾扫描,当分别遇到不符合条件的位置时停止,然后交换位 阅读全文

posted @ 2014-04-04 15:09 Eason Liu 阅读(2983) 评论(4) 推荐(0)

[Leetcode] Convert Sorted List to Binary Search Tree
摘要: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 阅读(176) 评论(0) 推荐(0)

[Leetcode] Convert Sorted Array to Binary Search Tree
摘要: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 阅读(153) 评论(0) 推荐(0)

[Leetcode] Recover Binary Search Tree
摘要: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 阅读(265) 评论(0) 推荐(0)

[Leetcode] Jump Game II
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximu... 阅读全文

posted @ 2014-04-03 19:05 Eason Liu 阅读(215) 评论(0) 推荐(0)

[Leetcode] Anagrams
摘要:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.这题先得知道啥叫Anagrams,知道后其实很简单。首先简单介绍一下Anagram(回文构词法)。Anagrams是指由颠倒字母顺序组成的单词,比如“dormitory”颠倒字母顺序会变成“dirty room”,“tea”会变成“eat”。回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序。For example:Input: [" 阅读全文

posted @ 2014-04-03 18:01 Eason Liu 阅读(7087) 评论(0) 推荐(0)

[面试题] Find next higher number with same digits
摘要:Find next higher number with same digits.Example 1 : if num = 25468, o/p = 25486Example 2 : if num = 21765, o/p = 25167Example 3 : If num = 54321, o/p = 54321 (cause it's not possible to gen a higher num than tiz with given digits ).Google的面试题,实际上就是next_permutation。 阅读全文

posted @ 2014-04-03 17:36 Eason Liu 阅读(222) 评论(0) 推荐(0)

[Jobdu] 题目1214:丑数
摘要:题目描述:把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。输入:输入包括一个整数N(1 2 #include 3 using namespace std; 4 5 int a[1501]; 6 7 int getMin(int a, int b, int c) 8 { 9 int tmp = a < b ? a : b;10 return c < tmp ? c : tmp;11 }12 13 void init()14 {15 ... 阅读全文

posted @ 2014-04-03 17:20 Eason Liu 阅读(614) 评论(1) 推荐(0)

[面试题] 两个单链表里交叉的第一个元素
摘要:找出两个单链表里交叉的第一个元素解法:分别遍历list1、list2,计算得到L1,L2;比较最后结点是否相等,相等则表明是两个链表是交叉的如果交叉:长的链表先移动|L1-L2|步,再逐个比较,等到第一个交点! 阅读全文

posted @ 2014-04-03 16:11 Eason Liu 阅读(322) 评论(0) 推荐(0)

[Leetcode] Minimum Window Substring
摘要:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S="ADOBECODEBANC"T="ABC"Minimum window is"BANC".Note:If there is no such window in S that covers all characters in T, return the emtpy 阅读全文

posted @ 2014-04-03 15:30 Eason Liu 阅读(194) 评论(0) 推荐(0)

[Leetcode] Binary Tree Maximum Path Sum
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return6.后序遍历! 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode... 阅读全文

posted @ 2014-04-03 14:27 Eason Liu 阅读(157) 评论(0) 推荐(0)

[Leetcode] Reorder List
摘要:Given a singly linked listL: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}.OH! MY GOD! I HATE LINKED LIST!看似简单,实现起来总会遇到各种指针错误,写程序之前最好先在纸上好好画画,把各种指针关系搞搞清楚。本题的想法就是先将列表平分成两份,后一份逆序,然 阅读全文

posted @ 2014-04-03 13:31 Eason Liu 阅读(804) 评论(0) 推荐(0)

[Leetcode] Search a 2D Matrix
摘要:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from l... 阅读全文

posted @ 2014-04-02 22:55 Eason Liu 阅读(205) 评论(0) 推荐(0)

[Leetcode] Combination Sum
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak 阅读全文

posted @ 2014-04-02 21:29 Eason Liu 阅读(142) 评论(0) 推荐(0)

[Leetcode] Median of Two Sorted Arrays
摘要:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).好难啊,总是会有各种边界问题。想法如下:最后从medianof two sorted arrays中看到了一种非常好的方法。原文用英文进行解释,在此我们将其翻译成汉语。该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n 阅读全文

posted @ 2014-04-02 18:48 Eason Liu 阅读(286) 评论(0) 推荐(0)

[Jobdu] 题目1463:招聘会
摘要:题目描述:又到毕业季,很多大公司来学校招聘,招聘会分散在不同时间段,小明想知道自己最多能完整的参加多少个招聘会(参加一个招聘会的时候不能中断或离开)。输入:第一行n,有n个招聘会,接下来n行每行两个整数表示起止时间,由从招聘会第一天0点开始的小时数表示。n 2 #include 3 #include 4 #include 5 using namespace std; 6 7 struct data { 8 int st; 9 int ed;10 };11 12 bool cmp(const data &a, const data &b) {13 return ... 阅读全文

posted @ 2014-04-02 01:27 Eason Liu 阅读(272) 评论(0) 推荐(0)

[Leetcode] Merge Two Sorted Lists
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.没啥好说的。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(... 阅读全文

posted @ 2014-04-02 00:18 Eason Liu 阅读(173) 评论(0) 推荐(0)

上一页 1 ··· 8 9 10 11 12 13 14 下一页