04 2015 档案
摘要:问题:最长公共子序列就是寻找两个给定序列的子序列,该子序列在两个序列中以相同的顺序出现,但是不必要是连续的。例如序列X=ABCBDAB,Y=BDCABA。序列BCA是X和Y的一个公共子序列,但是不是X和Y的最长公共子序列,子序列BCBA是X和Y的一个LCS,序列BDAB也是。思路:1、最简单的方法就...
阅读全文
摘要:题目要求:给字符数组,要求删除其中的某个字符,并将某个字符替换。假设将空格替换为%20,并且原数组大小足够大,只能在原数组操作。解题思路:删除操作:遍历数组,如果不是删除的字符,则依次写入数组,遇到要删除的字符,则跳过;复制/替换操作:复制是指将数组中某个字符复制n次,如b变成bb;是指将字符替换成...
阅读全文
摘要:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2-...
阅读全文
摘要:主要内容:1、什么是回文?2、字符子串和字符子序列的区别3、最长回文子序列的思路和代码4、回文子序列个数的思路和代码1、什么是回文palindrome?回文指的是正读和反读都一样的字符串,如aba,abba等2、字符子串和字符子序列的区别字符字串指的是字符串中连续的n个字符;如palindrome中...
阅读全文
摘要:Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题目要求: 给一有序链表,删除重复的结点,使得每个元素只出现一...
阅读全文
摘要:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of th...
阅读全文
摘要:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a...
阅读全文
摘要:1、Given a linked list, determine if it has a cycle in it.2、Given a linked list, return the node where the cycle begins. If there is no cycle, return n...
阅读全文
摘要:题目:假设张三的mp3里有1000首歌,现在希望设计一种随机算法来随机播放。与普通随机模式不同的是,张三希望每首歌被随机抽到的概率是与一首歌的豆瓣评分(0~10分)成正比的,如朴树的《平凡之路》评分为8.9分,逃跑计划的《夜空中最亮的星》评分为9.5分,则希望听《平凡之路》的概率与《夜空中最亮的星》...
阅读全文
摘要:题目:在一个N个整数数组里面,有多个奇数和偶数,设计一个排序算法,令所有的奇数都在左边。请完成sort的代码实现(C++或Java)C++:void sort(int N, int[]a){ …}例如: 当输入a = {8,4,1,6,7,4,9,6,4},a = {1,7,9,8,4,6,4,6...
阅读全文
摘要:题目:给定一个字符串,请写一段代码找出这个字符串中首先出现两次的那个字符。 例如字符串为"qywyer23tdd",输出为y。思路:1、从头到尾遍历字符串str,如果str[i]为首先出现两次的字符,则满足的条件是str[i]==str[j](0<=j<i);2、遍历字符串时,通过类似hash数组来...
阅读全文
摘要:题目:以下关于头文件,说法正确的是(B)A、#include,编译器寻找头文件时,会从当前编译的源文件所在的目录去找B、#include“filename.h”,编译器寻找头文件时,会从通过编译选项指定的目录去找C、多个源文件同时用到的全局整数变量,它的声明和定义都放在头文件中,是好的编程习惯D、在...
阅读全文
摘要:Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 –> 5 题目要求: 删除链表中包含val的元素结点 解题思路: 重点在于找到...
阅读全文
摘要:Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following co...
阅读全文
摘要:逆转链表是简单而又简单的链表问题,其问题的方法之一可以设置三个指针,一个指向当前结点,一个指向前驱结点,一个指向后继指针代码如下:class Solution {public: ListNode* ReverseList(ListNode* pHead) {// if(pHead==N...
阅读全文
摘要: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.题目要求:合并两个...
阅读全文
摘要:题目:一头刚出生的小母牛,4年后生一头小母牛,以后每年生一头,现有一头刚出生的小母牛,问20年后共有多少头牛?思路:列举前n年的情况:1、1、1、2、3、4、6、8、11。。。将规律抽象成公式:F(1)=1F(2)=1F(3)=1F(n)=F(n-1)+F(n-3)F(n):表示第n年共有多少头牛F...
阅读全文
摘要:Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not c...
阅读全文
摘要:题目:利用递归算法输出正整数和为n的所有不增的正整数和式。例如当n=5时,不增的和式如下:5=55=4+15=3+25=3+1+15=2+2+15=2+1+1+15=1+1+1+1+1解题思路:形如这种求子集的问题都可以采用回溯法来解决,回溯法即一种加上剪枝判断的递归算法。解决问题的关键词:不增代码...
阅读全文
摘要:主要内容:1、冒泡排序2、冒泡排序实现13、冒泡排序实现24、冒泡排序实现3一、冒泡排序的原理冒泡排序是简单的一种排序方法,效率低下,复杂度为O(n^2),其具体的算法流程如下:1、算法需要对数组遍历n-1遍;2、在每一次遍历中,比较前后相邻元素的大小,如果第一个比第二个大,则交换他们,这样第一次遍...
阅读全文