求子数组的最大和 题目:输入一个整型数组,数组里有正数和负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值,要求时间复杂度为O(n).例如输入数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18. 分析:如果不考虑时间复杂度,可以枚举出所有子数组并求其和.但是由于长度为n的数组有O(n2)个子数组:且求一个长度为n的数组的和的时间复杂度为O(n).因此这种思路的时间是O(…
求1+2+-+n 题目:求1+2+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A ? B : C). 分析:此题没多少实际意义,因为在软件开发中不会有这么变态的限制.但这道题能有效地考察发散思维能力,而发散思维能力能反映出对编程相关技术理解的深刻程度. 通常求1+2+-+n除了用公式n(n+1)/2之外,无外乎循环和递归两种思路.由于已经明确限制for和while的使用,循环已经不能再用了.同样,递归函数也需要用if语句或者条件…
哈希表(Hash Table)是一种特殊的数据结构,它最大的特点就是可以快速实现查找.插入和删除.因为它独有的特点,Hash表经常被用来解决大数据问题,也因此被广大的程序员所青睐.为了能够更加灵活地使用Hash来提高我们的代码效率,今天,我们就谈一谈Hash的那点事. 1. 哈希表的基本思想 我们知道,数组的最大特点就是:寻址容易,插入和删除困难:而链表正好相反,寻址困难,而插入和删除操作容易.那么如果能够结合两者的优点,做出一种寻址.插入和删除操作同样快速容易的数据结构,那该有多好.这就是哈希…
查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找.本文简单概括性的介绍了常见的七种查找算法,说是七种,其实二分查找.插值查找以及斐波那契查找都可以归为一类——插值查找.插值查找和斐波那契查找是在二分查找的基础上的优化查找算法.树表查找和哈希查找会在后续的博文中进行详细介绍. 查找定义:根据给定的某个值,在查找表中确定一个其关键字等于给定值的数据元素(或记录). 查找算法分类: 1)静态查找和动态查找: 注:静态或者动态都是针对查找表而言…
字符串全排列 题目:输入一个字符串,打印出该字符串的所有排列.例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab.cba. 分析:考察对递归的理解,以三个字符abc为例来分析一下求字符串排列的过程.首先固定一个字符a,求后面两个字符bc的排列.当两个字符bc的排列求好之后,我们把第一个字符a和后面的b交换,得到bac,接着固定第一个字符b,求后面两个字符ac的排列.现在是把c放到第一位置的时候了.记住前面已经把原先的第一个字符a和后面的b做…
在排序数组中查找和为定值的两个数 题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字,要求时间复杂度是O(n).如果有多对数字的和等于输入的数字,输出任意一对即可. 例如输入数组1, 2, 4,7, 11, 15和数字15.由于4+11=15,因此输出是4和11. 分析:如果不考虑时间复杂度,最简单想法莫过于先在数组中固定一个数字,再依次判断数组中剩下的n-1个数字与它的和是不是等于输入的数字,但是这种思路的时间复杂度是O(n2). 假设现在随便…
查找最小的k个元素 题目:输入n个整数,输出其中最小的k个. 例如输入1, 2, 3, 4, 5, 6, 7和8这八个数字,则最小的4个数字为1, 2, 3和4. 分析:这道题最简单的思路是把输入的n个整数排序,这样排在最前面的k个数就是最小的k个数.只是这种思路的时间复杂度为O(nlogn),这当然还有更快的思路. 可以开辟一个长度为k的数组,每次从输入的n个整数中读入一个数,如果数组中已经插入的元素个数少于k个,则将读入的整数直接放到数组中.否则长度为k的数组已经满了,不能再往数组里插入元素…
排序有内部排序和外部排序之分,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.我们这里说的八大排序算法均为内部排序. 下图为排序算法体系结构图: 常见的分类算法还可以根据排序方式分为两大类:比较排序和非比较排序.本文中前七种算法都是比较排序,非比较排序有三种,分别为: 1)计数排序(Count Sort)(复杂度O(n+k)(其中k是待排序的n个数字中最大值),参见<计数排序-Counting Sort>) 2)基数排序(…
二叉树中和为某值的所有路径 题目:输入一个整数和一个二叉树,从树的根节点开始往下访问一直到叶节点所经过的所有节点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数22和如下二叉树: 10 /   \ 5    12 /  \ 4  7 则打印出两套路径:10, 12和10, 5, 7. 二叉树节点的数据结构定义为 structBinaryTreeNode { int m_nValue; BinaryTreeNode *m_pLeft; BinaryTreeNode *m_pRight…
把二元查找树转变成排序的双向链表 题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表,要求不能创建任何新节点,只调整指针指向. 比如将二元查找树 10 /       \ 6        14 /  \        /     \ 4   8   12   16 转换成双向链表4=6=8=10=12=14=16 分析: 思路一:当到达某一节点准备调整以该节点为根节点的子树时,先调整其左子树将左子树转换成一个排好序的左子链表,再调整其右子树转换右子链表.最近链接左子链表的最右节点…
Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data…
Source, git Heap is a data structure that can fundamentally change the performance of fairly common algorithms in Computer Science. The heap data structure is called a heap because it satisfies the heap property. The heap property states, that if P i…
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structure to store the data and easy for search? We can use Trie data structure, it is a tree, but not a binary tree. The reuslts of constructing a tree by u…
A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. Each node in a graph may point to any other node in the graph. This is very useful for things like describing networks, creating related nonhierarchi…
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirected graph G with n vertices and m edges. Each vertex is attached with a weight wi. Let Gi be the graph after deleting the i-th vertex from graph G. Pro…
Design and implement a TwoSum class. It should support the following operations:add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example,add(1); add(…
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called…
Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/483 Description Data structure is a fundamental course of Computer Science, so that each contestant is highly likely to solve this data structu…
Question: Design and implement a TwoSum class. It should support the following operations: add and find. add(input) – Add the number input to an internal data structure. find(value) – Find if there exists any pair of numbers which sum is equal to the…
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 872    Accepted Submission(s): 236 Problem Description Mr. Frog learned a basic data structure recently, which is called stac…
11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. 2 Take out an element from the bag. Given…
11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations:1 x Throw an element x into the bag.2 Take out an element from the bag.Given a sequence of operations with return values, you’re going to guess the…
C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice id=27724" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-…
Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack: ∙∙ PUSH x: put x on the top of the stack, x must be 0 or 1. ∙∙ POP: throw the element which is on the top of the stack. Since it is too…
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should support the following operations:add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum…
Data Structure 题目描述 将一个非负整数序列划分为 \(K\) 段,分别计算出各段中的整数按位或的结果,然后再把这些结果按位与起来得到一个最终结果,把这个最终结果定义为这个序列的一个 \(K−or−and\) 值. 比如序列为 \([1,5,9,2],K=2\),如果划分为 \([1,5],[9,2]\),那么 \(K−or−and\) 值为 \((1or5)and(9or2)=1\).当然划分可能不止一种,所以 \(K−or−and\) 值也可能不止一个. 给定一个长度为 \(N…
Problem Description Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like probl…
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string. Dec(Key) - If Key's value is 1, remove it from the data structu…
Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter…
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a functional representation of persistent sequences supporting access to the ends in amortized constant time, and concatenation and splitting in time logarithmi…