算法
undersky_chen
这个作者很懒,什么都没留下…
展开
-
查找单链表倒数第k个元素
查找单链表倒数第m个结点,要求时间复杂度为O(n).(提示,使用双指针)解题思路: 常规思路为先获取链表的长度N,然后返回N-k+1位置处的结点即可。但是中需要遍历两次链表。 我们使用另一种算法,设定两个指针p1,p2.将这两个指针都向第一个结点,让p1先走k步,然后两个指针一起向后移动,当p1到达最后一个结点时,p2指针刚好指向链表的倒数第k个结点。关键算法:int getK(Node *l原创 2016-02-23 13:38:25 · 2751 阅读 · 1 评论 -
CTCI系列--1.5 字符串压缩(C语言)
题目:Implement a method to perform basic string compression using the counts of repeated characters. For example,the string aabcccccaaa would become a2b1c5a3.If the “compressed” string would not become s原创 2016-01-25 09:13:57 · 3229 阅读 · 0 评论 -
CTCI系列--1.4 字符替换(C语言)
题目:Write a method to replace all spaces in a string with ‘%20’.You may assume that the string has sufficient space at the end of the string to hold the additional characters,and that you are given the原创 2016-01-25 09:11:08 · 343 阅读 · 0 评论 -
CTCI系列--1.3 判断两个字符串是否互为变换(C语言)
题目:Given two strings,write a method to decide if one is a permutation of the other. 对于给定的两个字符串,写一个方法判断其中一个是否是另一个的变换。解题思路分析题目,要判断一个字符串是否是另一个字符串变换得来。那么首先,这两个字符串必须==长度相等==;其次两个字符串中的每个字符必须相一致。那么,我们代码转载 2016-01-22 08:50:27 · 431 阅读 · 0 评论 -
CTCI系列--1.2 字符串翻转(C语言)
题目: Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string. 用C或C++实现一个翻转C风格字符串的功能函数解答C风格字符串即为末尾以’\0’结尾的字符串,例如字符串”hello”,其末尾为’\0’,字符串长度为6解题思路:我们可以使用两个指针转载 2016-01-22 08:47:35 · 256 阅读 · 0 评论 -
CTCI系列--1.1 判断一个字符串中的字符是否唯一(C语言)
原题: Implement an algorithm to determine if a string has all unique characters.What if you cannot use additional data structures? 实现一个算法,判断一个字符串中的字符是否是唯一的。不能够使用额外的数据结构。首先得确定构成字符串的字符集有多大?是ASC转载 2016-01-21 09:15:27 · 1096 阅读 · 0 评论