随笔分类 -  数据结构与算法

摘要:在此以走迷宫为例:给定迷宫起点和终点,看能否到达:(xt,yt)void f(int x,int y){ if(x21||y21){//判断是否超出迷宫 return; } for(i=0;i<4;i++){ if(ch[x][y]=='.'){ ch[... 阅读全文
posted @ 2015-05-05 23:13 SevenForever 阅读(214) 评论(0) 推荐(0)
摘要:二分查找,应用的场景是在一个排序的序列中,查找指定的数的位置。复杂度为O(logn).常用的包括递归的方法,如下:def binarySearch(data, x, start, end): if start>end: return -1 mid = start + (end-start)/2 #... 阅读全文
posted @ 2015-04-27 20:10 SevenForever 阅读(193) 评论(0) 推荐(0)
摘要:深度优先和广度优先遍历在树和图中应用最为普遍,思想也类似,因此放到一起来总结。二叉树的深度优先广度优先遍历:一棵二叉树(2,(2(3,4),3(4,5))),这是一棵满二叉树,一共有7个节点,根节点2,深度为3数据结构定义如下:class Node: def __init__(self, value... 阅读全文
posted @ 2015-04-27 19:56 SevenForever 阅读(658) 评论(0) 推荐(0)
摘要:快速排序应该是数据结构中排序中最重要的一个,包括其中的patition思想,以及后面的整体的分治思想,都对于解决实际问题有很大的借鉴。快速排序是一种交换排序的方法,不稳定,也就是说如果两个相同的数,快排之后二者可能交换位置。1.首先来看partition函数,函数名partition(data, l... 阅读全文
posted @ 2015-04-27 19:19 SevenForever 阅读(262) 评论(0) 推荐(0)
摘要:题目描述:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra spac... 阅读全文
posted @ 2015-04-10 17:25 SevenForever 阅读(134) 评论(0) 推荐(0)
摘要:题目描述:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:此题目要求为判断一个链表是否有环,这应该说是一个经典的面试题目。只需... 阅读全文
posted @ 2015-04-07 19:19 SevenForever 阅读(126) 评论(0) 推荐(0)
摘要:题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the de... 阅读全文
posted @ 2015-03-25 15:35 SevenForever 阅读(147) 评论(0) 推荐(0)
摘要:题目描述:mplement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not pos... 阅读全文
posted @ 2015-03-24 23:17 SevenForever 阅读(177) 评论(0) 推荐(0)
摘要:题目描述:Implement pow(x, n).思路:要实现pow函数,当然我们可以递归实现return x*pow(x,n-1). 但是这样存在的问题就是递归的计算中不够优化的问题。更简单的方法是每次二分来实现。这样大大减少了乘法的次数。中间需要注意以下几个问题:1.指数是负数2.溢出的问题cl... 阅读全文
posted @ 2015-03-24 20:09 SevenForever 阅读(139) 评论(0) 推荐(0)
摘要:提交了格灵深瞳的简历后,收到需要先进行一个简单的技术测试的通知,临时抱佛脚,先刷刷上面几道题:题目要求A zero-indexed array A consisting of N integers is given. Anequilibrium indexof this array is any i... 阅读全文
posted @ 2015-03-24 15:30 SevenForever 阅读(424) 评论(0) 推荐(0)
摘要:题目描述:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.思路:题目的意思呢,就是寻找一组字符串中,字符串使用相同数量的相... 阅读全文
posted @ 2015-03-23 23:31 SevenForever 阅读(122) 评论(0) 推荐(0)
摘要:题目描述:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating lette... 阅读全文
posted @ 2015-03-23 00:02 SevenForever 阅读(151) 评论(0) 推荐(0)
摘要:题目描述:Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest form... 阅读全文
posted @ 2015-03-22 01:16 SevenForever 阅读(182) 评论(0) 推荐(0)
摘要:题目描述: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 t... 阅读全文
posted @ 2015-03-19 17:12 SevenForever 阅读(386) 评论(0) 推荐(0)
摘要:题目描述:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as00000010100101000001111010011100), re... 阅读全文
posted @ 2015-03-19 16:31 SevenForever 阅读(132) 评论(0) 推荐(0)
摘要:题目描述Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321数字x不停对10取整,变量result*10加余数,直至数字x==0,那么result就是x的翻转几个特殊情况:1.负... 阅读全文
posted @ 2015-03-19 16:26 SevenForever 阅读(114) 评论(0) 推荐(0)
摘要:题目描述:Determine whether an integer is a palindrome. Do this without extra space.思路:对数字不停对10取余,并将余数乘10累加,比较剩下的数字与余数的大小,直至相等或者数字对10取余后相等,返回true,否则返回false... 阅读全文
posted @ 2015-03-19 16:23 SevenForever 阅读(123) 评论(0) 推荐(0)
摘要:题目描述:Rotate an array ofnelements to the right byksteps.For example, withn= 7 andk= 3, the array[1,2,3,4,5,6,7]is rotated to[5,6,7,1,2,3,4].Note:Try to... 阅读全文
posted @ 2015-03-19 11:30 SevenForever 阅读(469) 评论(0) 推荐(0)