update i9w
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
# LFU
|
||||
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
# LRU
|
||||
+2
-2
@@ -9,8 +9,8 @@
|
||||
* 插入排序
|
||||
* 选择排序
|
||||
* 希尔排序
|
||||
* 快排
|
||||
* 归并排序
|
||||
* 快排 : 选最后一个作为pivot(基数), 将数据分为 左边小于 pivot, 右边大于 pivot
|
||||
* 归并排序 :
|
||||
* 堆排序
|
||||
|
||||
**线性排序算法**
|
||||
|
||||
@@ -13,7 +13,9 @@ char char_first_appear_once(const char *source)
|
||||
```
|
||||
|
||||
思路一: 蛮力统计, O(n^2)复杂度
|
||||
思路二: 使用hash表,2次扫描,第一次建立hash表 key为字符,value为出现次数;第二次扫描找到第一个value为1的key,时间复杂度O(n)
|
||||
思路二: 使用hash表,2次扫描
|
||||
* 第一次建立hash表 key为字符,value为出现次数;
|
||||
* 第二次扫描找到第一个value为1的key,时间复杂度O(n)
|
||||
|
||||
hash表长度 256,字符直接作为key值。需要注意的是 char 的范围是 -128~127,unsigned char 才是0~255
|
||||
|
||||
|
||||
@@ -139,55 +139,6 @@ char *replace_blank(char *source){
|
||||
|
||||
|
||||
|
||||
|
||||
## 小写字母排在大写字母的前面
|
||||
|
||||
有一个由大小写组成的字符串,现在需要对他进行修改,将其中的所有小写字母排在大写字母的前面(大写或小写字母之间不要求保持原来次序),如有可能尽量选择时间和空间效率高的算法 c语言函数原型:
|
||||
|
||||
```
|
||||
void proc(char *str)
|
||||
```
|
||||
分析:
|
||||
|
||||
比如:HaJKPnobAACPc,要小写字母前面且不要求保存顺序,可以是:anobcHJKPAACP
|
||||
|
||||
1. 小写字母 a~z 的 ASCII码值是 97~122,A~Z的 ASCII码值是 65~90;0~9 的ASCII码是 48~57
|
||||
2. 两边向中间扫描,左边大写右边小写就交换;如果都小写,头指针向前知道找到大写;如果都是大写,尾指针向后找小写;
|
||||
|
||||
|
||||
示例代码
|
||||
|
||||
```
|
||||
char *proc(char *str){
|
||||
char *start = str;
|
||||
char *end = str;
|
||||
if (str == NULL) return NULL;
|
||||
while(*end != '\0') end++;
|
||||
end--;
|
||||
|
||||
while(start < end){
|
||||
if (*start >= 'A' && *start <= 'Z'){//大写
|
||||
if (*end >= 'a' && *end <= 'z'){
|
||||
char tmp = *start;
|
||||
*start = *end;
|
||||
*end = tmp;
|
||||
|
||||
start++;
|
||||
}
|
||||
end--;
|
||||
}else{//小写
|
||||
if (*end >= 'A' && *end <= 'Z'){
|
||||
end--;
|
||||
}
|
||||
start++;
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 字符串原地压缩
|
||||
|
||||
题目描述:“abeeeeegaaaffa" 压缩为 "abe5ag3f2a",请编程实现。
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
# 字符串-排序
|
||||
|
||||
|
||||
|
||||
|
||||
## 小写字母排在大写字母的前面
|
||||
|
||||
有一个由大小写组成的字符串,现在需要对他进行修改,将其中的所有小写字母排在大写字母的前面(大写或小写字母之间不要求保持原来次序),如有可能尽量选择时间和空间效率高的算法 c语言函数原型:
|
||||
|
||||
```
|
||||
void proc(char *str)
|
||||
```
|
||||
分析:
|
||||
|
||||
比如:HaJKPnobAACPc,要小写字母前面且不要求保存顺序,可以是:anobcHJKPAACP
|
||||
|
||||
1. 小写字母 a~z 的 ASCII码值是 97~122,A~Z的 ASCII码值是 65~90;0~9 的ASCII码是 48~57
|
||||
2. 两边向中间扫描,左边大写右边小写就交换;如果都小写,头指针向前知道找到大写;如果都是大写,尾指针向后找小写;
|
||||
|
||||
|
||||
示例代码
|
||||
|
||||
```
|
||||
char *proc(char *str){
|
||||
char *start = str;
|
||||
char *end = str;
|
||||
if (str == NULL) return NULL;
|
||||
while(*end != '\0') end++;
|
||||
end--;
|
||||
|
||||
while(start < end){
|
||||
if (*start >= 'A' && *start <= 'Z'){//大写
|
||||
if (*end >= 'a' && *end <= 'z'){
|
||||
char tmp = *start;
|
||||
*start = *end;
|
||||
*end = tmp;
|
||||
|
||||
start++;
|
||||
}
|
||||
end--;
|
||||
}else{//小写
|
||||
if (*end >= 'A' && *end <= 'Z'){
|
||||
end--;
|
||||
}
|
||||
start++;
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -59,8 +59,27 @@ int isLinkCicle(Link *head);
|
||||
|
||||
## 链表反转
|
||||
|
||||
三个指针,遍历一遍(0(n)复杂度
|
||||
|
||||
思路一:迭代,三个指针,遍历一遍(0(n)复杂度
|
||||
思路一:递归实现,较难理解
|
||||
|
||||
迭代实现
|
||||
|
||||
```
|
||||
```
|
||||
|
||||
|
||||
递归实现
|
||||
|
||||
```
|
||||
ListNode reverse(ListNode head) {
|
||||
if (head.next == null) return head;
|
||||
ListNode last = reverse(head.next);
|
||||
head.next.next = head;
|
||||
head.next = null;
|
||||
return last;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## 从尾到头输出链表
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
* 大数问题
|
||||
* 公倍数
|
||||
* 素数
|
||||
* 丑数
|
||||
|
||||
|
||||
## ipv4 转 int
|
||||
@@ -119,11 +120,11 @@ tmp[i] = tmp[i]>=0 ? tmp[i] : tmp[i]+16;
|
||||
|
||||
## 寻找丑数
|
||||
|
||||
题目:我们把只包含因子2、3和5的数称作丑数(Ugly Number)。
|
||||
例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。
|
||||
> 我们把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。
|
||||
> 分析:这是一道在网络上广为流传的面试题,据说google曾经采用过这道题。
|
||||
|
||||
求按从小到大的顺序的第1500个丑数。
|
||||
|
||||
分析:这是一道在网络上广为流传的面试题,据说google曾经采用过这道题。
|
||||
|
||||
这里的因子应该不包含本身,因此这个序列应该是这样:
|
||||
1,2,3,4,5,6,8,9,10,12,15,16,18,20,28....
|
||||
@@ -146,3 +147,4 @@ tmp[i] = tmp[i]>=0 ? tmp[i] : tmp[i]+16;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -14,3 +14,5 @@ int countPrimes(int n)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## Fibonacci数列
|
||||
|
||||
题目:定义Fibonacci数列如下:
|
||||
|
||||
@@ -24,10 +24,46 @@ B = [1,2,7]
|
||||
A = [1,2,4,5,6,7]
|
||||
```
|
||||
|
||||
思路1:合并2个数列,变成 `[4,5,6,1,2,7]` , 归因排序即可
|
||||
思路2:
|
||||
思路1:合并2个数列,变成 `[4,5,6,1,2,7]` , 归因排序即可, 递归思路
|
||||
思路2:尾部向头部扫描,将大的值放在尾部
|
||||
|
||||
|
||||
```
|
||||
//尾部向头部扫描,将大的值放在尾部
|
||||
static void mergeSequenceList(int[] a, int aSize , int[] b){
|
||||
|
||||
int len_a = a.length -1;
|
||||
|
||||
int index_a = aSize -1;
|
||||
int index_b = b.length -1;
|
||||
|
||||
while (index_a >= 0 && index_b >= 0) {
|
||||
|
||||
//2个指针都有值时
|
||||
if (a[index_a] >= 0 && b[index_b] >= 0) {
|
||||
if (a[index_a] > b[index_b]) {
|
||||
a[len_a--] = a[index_a--];
|
||||
}else{
|
||||
a[len_a--] = b[index_b--];
|
||||
}
|
||||
}
|
||||
|
||||
//a 无值,b有值 ; 把剩下b放好
|
||||
if (index_a < 0 && index_b >= 0) {
|
||||
while (index_b >= 0) {
|
||||
a[len_a--] = b[index_b--];
|
||||
}
|
||||
}
|
||||
|
||||
//a 有值,b无值; 把剩下a放好
|
||||
if (index_a >=0 && index_b < 0) {
|
||||
while (index_b >= 0) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## 两个序列和之差最小
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
|
||||
|
||||
|
||||
## 把一个有序整数数组放到二叉树中?
|
||||
|
||||
分析:本题考察二叉搜索树的建树方法,简单的递归结构。
|
||||
|
||||
@@ -10,4 +10,5 @@
|
||||
|
||||
|
||||
|
||||
|
||||
## 多叉树的层序遍历
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
|
||||
|
||||
## [链表](2%20链表)
|
||||
## [链表](2%20链表.md)
|
||||
|
||||
链表处理的常见问题包括:
|
||||
|
||||
|
||||
@@ -1,40 +1,4 @@
|
||||
# YYCache
|
||||
|
||||
## YYCache
|
||||
|
||||
[YYCache](https://github.com/ibireme/YYCache.git) 是 iOS 系统上一套线程安全的 `Key-Value` 缓存实现,使用 `Objective-C` 语言实现。`YYCache` 使用 `双向链表队列+hash表结构` 实现。
|
||||
|
||||
### 用到的算法介绍
|
||||
|
||||
先来看一下它的数据结构:
|
||||
|
||||
```
|
||||
// 这是一个节点的结构
|
||||
@interface _YYLinkedMapNode : NSObject {
|
||||
@package
|
||||
__unsafe_unretained _YYLinkedMapNode *_prev; // retained by dic
|
||||
__unsafe_unretained _YYLinkedMapNode *_next; // retained by dic
|
||||
id _key;
|
||||
id _value;
|
||||
NSUInteger _cost;
|
||||
NSTimeInterval _time;
|
||||
}
|
||||
```
|
||||
|
||||
这里定义了一个双向链表结构,`_prev`,`_next`分别指向前缀节点和后缀节点。
|
||||
|
||||
```
|
||||
@interface _YYLinkedMap : NSObject {
|
||||
@package
|
||||
CFMutableDictionaryRef _dic; // do not set object directly
|
||||
NSUInteger _totalCost;
|
||||
NSUInteger _totalCount;
|
||||
_YYLinkedMapNode *_head; // MRU, do not change it directly
|
||||
_YYLinkedMapNode *_tail; // LRU, do not change it directly
|
||||
BOOL _releaseOnMainThread;
|
||||
BOOL _releaseAsynchronously;
|
||||
}
|
||||
```
|
||||
_dic 就是存储缓存节点的hash结构
|
||||
_head 指向链表的头部,_tail指向链表的尾部,组成一个队列结构
|
||||
|
||||
[YYCache](https://github.com/ibireme/YYCache.git) 是 iOS 系统上一套线程安全的 `Key-Value` 缓存实现,使用 `Objective-C` 语言实现。`YYCache` 缓存使用 `双向链表队列+hash表结构` 实现。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user