
数据结构与算法
steelhe
这个作者很懒,什么都没留下…
展开
-
记录
1. (1)我们的算法需要测试用例的验证 (2)任何的优化都要建立在测试的基础之上 (3)测试和代码的编写要同步进行 (4)算法的成功运行时一步一步进行得,每一步的成功必须确立在原有的成功之上一个普通的函数,那么首先需要判断的就是参数的合法性:转载 2013-11-29 23:08:15 · 434 阅读 · 0 评论 -
leetcode--Majority Element II
题目:总共n个元素,求超过n/k个的元素(占一定概率的元素),k=2,3,...当k==2 时, moore voting algorithm:用ret=nums[i],count计数,遍历nums,当nums[i]==ret时,count加1,当nums[i]!=ret时,count减1,count==0时,ret重新赋值nums[i];假设超过n/2的数一定存在,则返回ret;原创 2015-07-28 16:17:21 · 364 阅读 · 0 评论 -
leetcode--Implement strStr()
题目:在字符串haystack中找needle第一次出现的地方。方法一:O(nm) runtime, O(1) space – Brute force:暴力解法,第一次自己写的程序没有通过int strStr(char* haystack, char* needle) { int i = 0; int j = 0; int slen = strle原创 2015-07-31 10:49:13 · 218 阅读 · 0 评论 -
leetcode--backtracking[0]
word search、subsets I / II、N-Queens I / II、Permutations I / II回溯算法,backtracking,就是穷举所有可能的结果,记录已经走过的路(如用vector等写入1),当此路走不通时,就擦除这一步的影响(写入0),回溯到上一步,重新选择。可以将所有的结果,写成树的形式。for循环中的递归很难理解,看到网友说:递归是将大问题不断变原创 2015-08-12 23:16:22 · 480 阅读 · 0 评论 -
leetcode--Clone Graph
Depth-first search 深度优先算法,举个例子:二叉树中的前序遍历,一般用递归实现;Breadth-first search 广度优先算法,举个例子:二叉树的层序遍历,需要用队列实现。method1:DFSUndirectedGraphNode *DFS(UndirectedGraphNode *node, unordered_map *existed){ if原创 2015-08-02 22:05:58 · 432 阅读 · 0 评论 -
leetcode--sort汇总
总结:①复杂度O(n): 桶排序 Maximum Gap 、计数排序、基数排序这三个的共同点是都对nums分类,另外声明数组或链表,计数排序声明 nums中(最大值-最小值)个数组,就像是最简单的hash table,空间占用可能很大,对于判断某个元素是否出现比较常用;基数排序是对 数组元素的每一位进行排序,采用最低有效位优先;桶排序先确定桶的个数len,nums[i]%len或nums...原创 2015-07-28 18:08:45 · 694 阅读 · 0 评论 -
leetcode--sum集合:2sum,3sum,4sum
k sum:在数组nums中找到k个数相加的和为target,不可重复这个问题有两个方法:① double pointer,可以得到O(n^(k-1))②hash table,得要具体分析一下2sum:方法一:struct num{ int number; int index;};int* twoSum(int* nums, int numsSize, int原创 2015-08-05 16:20:59 · 1482 阅读 · 0 评论 -
leetcode——Combinations
刚开始的思路是 一个大小是K的数组,最开始的值是(比如k=4)1 2 3 4,然后最末尾递增到n,然后倒数第二位再开始...可以用for循环,但是有k个嵌套for循环,不知道该怎么实现。还是用了最笨的方法:void ComBT(int n, int i, int k, int *flag, int** ret, int *returnSize,int *column){ if (i == n原创 2015-08-19 14:10:35 · 414 阅读 · 0 评论 -
leetcode——backtracking[1] Generate Parentheses ,Catalan数——卡特兰数
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()原创 2015-08-19 10:15:12 · 504 阅读 · 0 评论 -
leetcode——Binary Tree Level Order Traversal
树的遍历主要是四种:前序、中序遍历、后序、分层遍历。前三种一般用递归的方法,而递归方法都会用到堆栈;分层遍历需要用到队列,队列的实现也是不唯一的,根据需要建立自己的结构。这道题具体的方法是,先将根入栈,记录size=1(我的size在队列中),出栈生成需要的数组,size = 0;再将他的左右儿子入栈,size...一直循环到size=0,也就是不再有入栈且完全出栈的时候(描述不好。。)。原创 2015-07-06 09:18:13 · 385 阅读 · 0 评论 -
leetcode-- 动态规划 dynamic programming
首先要分析题目,判断第n步的答案是否建立在第n-1步或n-2步上?yes,那这道题应该属于动态规划。① 将子问题的答案系统地记录在一个表内② 分析状态和状态转移方程leetcode easy中的题目:house robber: F[i] = max(max(F[i - 2], F[i - 3]) + nums[i], F[i - 1]);Climbing Stairs:原创 2015-07-08 21:51:21 · 568 阅读 · 0 评论 -
抽象数据类型——表
基础名词:表、空表、前驱、后继原创 2014-11-08 19:44:02 · 814 阅读 · 0 评论 -
Leecode 记录——Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the原创 2015-05-10 11:49:41 · 384 阅读 · 0 评论 -
Leecode记录——Valid Palindrome
Palindrome,回文,就是字符串中的字母和数字是中心对称的,像“dad”。Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Pa原创 2015-05-10 19:44:34 · 383 阅读 · 0 评论 -
leetcode记录——Count and Say
char* countAndSay(int n) { char s1[10000]="1", s2[10000]="1";//动态数组不会,刚开始用的100,1000 提示run time,//char *s 指向常量字符串,内容不能修改,初始化后即确定大小,出现了stack around the variable 's' was corrupted数组越界问题;char s[] 指向栈原创 2015-05-10 21:25:15 · 334 阅读 · 0 评论 -
leetcode-tree1
1.Invert Binary Tree 二叉树的镜像2.Minimum Depth of Binary Tree 3.Maximum Depth of Binary Treestruct TreeNode { int val; struct TreeNode *left; struct TreeNode *right;};struct TreeNod原创 2015-06-16 17:10:05 · 374 阅读 · 0 评论 -
leetcode balanced binary tree(看不懂...)
http://www.cnblogs.com/Antech/p/3705928.html问题:给一个二叉树,写一个算法判断这个树是不是balanced。Solution #1.第一次遇到这个问题时我的解法,如下:public class Solution { public boolean isBalanced(TreeNode root) {转载 2015-06-18 09:26:46 · 388 阅读 · 0 评论 -
【leetcode 两个链表的交集点】Intersection of Two Linked Lists
Intersection of Two Linked Lists 1、题目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:转载 2015-06-03 22:29:10 · 390 阅读 · 0 评论 -
leetcode 50题总结
看到网友说,半小时刷了8道题,一天刷三十道,很不淡定,身为菜鸟还飞的太晚了,一个月只能刷50,后面的题目还会更难。希望每天都努力,三年的时间希望可以让编程成为我的工具,实现工作和生活中的问题,能够让我对各种语言融汇贯通,三年可能不够。题目中主要浪费时间的地方:①注意力不集中,经常犯低级错误:1==写成=,在if中的时候造成错误;2类似地代码,赋值黏贴时,忘了修改其中的变量;3 char转成原创 2015-07-08 21:42:23 · 759 阅读 · 0 评论 -
Leetcode 记录——Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2015-05-17 11:18:58 · 301 阅读 · 0 评论