上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 30 下一页
摘要: 题目描述:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。输入:每个输入文件包含一组测试案例。对于每个测试案例,第一行输入一个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) 编辑
摘要: 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 阅读(205) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(7015) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 题目描述:把只包含因子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 阅读(603) 评论(1) 推荐(0) 编辑
摘要: 找出两个单链表里交叉的第一个元素解法:分别遍历list1、list2,计算得到L1,L2;比较最后结点是否相等,相等则表明是两个链表是交叉的如果交叉:长的链表先移动|L1-L2|步,再逐个比较,等到第一个交点! 阅读全文
posted @ 2014-04-03 16:11 Eason Liu 阅读(313) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(184) 评论(0) 推荐(0) 编辑
上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 30 下一页