04 2014 档案
摘要:题目链接Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.首...
阅读全文
摘要:比如对于数组[1,-2,3,5,-1,2] 最大子数组和是sum[3,5,-1,2] = 9, 我们要求函数输出子数组和的最大值,并且返回子数组的左右边界(下面函数的left和right参数).本文我们规定当数组中所有数都小于0时,返回数组中最大的数(也可以规定返回0,只要让以下代码中maxsum初...
阅读全文
摘要:求数组全排列(不包含或者包含重复元素) 求数组所有组合(相当于求集合的所有子集,不包含或者包含重复元素) 从n个数中选择k个的组合(不包含重复元素) 从n个数中选择k个的组合(包含重复元素) 【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3695472.html
阅读全文
摘要:LeetCode:Combinations这篇博客中给出了不包含重复元素求组合的5种解法。我们在这些解法的基础上修改以支持包含重复元素的情况。对于这种情况,首先肯定要对数组排序,以下不再强调修改算法1:按照求包含重复元素集合子集的方法LeetCode:Subsets II算法1的解释,我们知道:若当...
阅读全文
摘要:定义二进制变量: 一般是以八进制或者十六进制来定义,八进制数以0开头,十六进制数以0x开头 例如int a = 0x80, 这里的80只能表示8个二进制位,它表示的是int的低8位,前面的24个二进制位补0,所以a = 128;也可以 a = –0x80, 此时a = -128;8进制同理 需要注意的是:如果0x…能够在整形内表示,则其默认是int,否则再看unsigned int能否表示...
阅读全文
摘要:题目链接Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see bel...
阅读全文
摘要:Search Insert PositionGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if ...
阅读全文
摘要:题目链接Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.For example:Input: ["tea","and","...
阅读全文
摘要:题目链接Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are th...
阅读全文
摘要:题目链接Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique lo...
阅读全文
摘要:题目链接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...
阅读全文
摘要:题目链接Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.合并k个有序的链表,我们假设每个链表的平均长度是n。这一题需要用到合并两个有序的链表子过程算法1...
阅读全文
摘要:题目链接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为了操作...
阅读全文
摘要:题目链接Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. Afte...
阅读全文
摘要:这篇文章分析一下链表的各种排序方法。以下排序算法的正确性都可以在LeetCode的链表排序这一题检测。本文用到的链表结构如下(排序算法都是传入链表头指针作为参数,返回排序后的头指针)struct ListNode { int val; ListNode *next; ListNode(int x) ...
阅读全文
摘要:这里不详细说明快速排序的原理,具体可参考here快速排序主要是partition的过程,partition最常用有以下两种写法第一种: int mypartition(vector&arr, int low, int high) { int pivot = arr[low];//选第一个元素...
阅读全文
摘要:PermutationsGiven a collection of numbers, return all possible permutations.For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2]...
阅读全文
摘要:3Sum ClosestGiven an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is clo...
阅读全文
摘要:题目链接Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the tw...
阅读全文
摘要:对于无向图算法1我们知道对于环1-2-3-4-1,每个节点的度都是2,基于此我们有如下算法(这是类似于有向图的拓扑排序):求出图中所有顶点的度,删除图中所有度 >&graph, int node, vector&visit, vector&father){ int n = graph.size(); visit[node] = 1; //cout"; tmp = father[tmp]; } cout >&graph){ int n = grap...
阅读全文
摘要:要求是该类不能被继承,但是能够像正常的类一样使用。那么一下方法就不符合题目要求:1、构造函数和析构函数设置为private。这样就不能定义一个类的实例2、类似于singleton模式那样,定义一个静态函数(或友元函数)来生成类的实例。这样只能通过new在堆上创建类可以如下设计这个类class Base{private: Base() {} ~Base() {} friend class FinalClass;};class FinalClass : virtual public Base{};int main(){ FinalClass *p = new Final...
阅读全文
摘要:单例模式,也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如在某个服务器程序中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息。这种方式简化了在复杂环境下的配置管理。实现单例模式的思路是:一个类能返回对象一个引用(永远是同一个)和一个获得该实例的方法(必须是静态方法,通常使用getInstance这个名称);当我们调用这个方法时,如果类持有的引用不为空就返回这个引用,如果类保持的引用为空
阅读全文