算法实例
##字符串反转
void char_reverse (char *cha)
{
// 指向第一个字符
char* begin = cha;
// 指向最后一个字符
char* end = cha + strlen(cha) - 1;
while (begin < end) {
// 交换前后两个字符,同时移动指针
char temp = *begin;
*(begin++) = *end;
*(end--) = temp;
}
}
- (NSString *)reverseString:(NSString *)originString{
NSString *resultStr = @"";
NSInteger strlength = (NSInteger)originString.length-1;
for (NSInteger i = strlength; i >= 0; i--) {
NSString *indexStr = [originString substringWithRange:NSMakeRange(i, 1)];
resultStr = [resultStr stringByAppendingString:indexStr];
}
return resultStr;
}
/*******递归的方式写字符串长度函数*******/
int my_strlen(char *str)
{
if(*str == '\0') //当传入的字符串中没有字符
return 0; //字符串长度为0
else
return 1+ my_strlen(str+1); /*运用递归,每递归一次长度加1,直到遍历到的'\0'时结束递归*/
}
/*******递归方式写字符串反转*******/
void reverse_string(char *string)
{
//调用上面的字符串长度函数;
int len = my_strlen(string);
//当字符串长度小于等于1时,不执行;
if(len <= 1)
return ;
else
{
char temp = string[0]; //将第一个字符的值保存在temp中;
string[0] = string[len-1];//将最后一个字符赋给第一个字符;
string[len-1] = '\0'; //将最后一个字符的内容赋为'\0';
reverse_string(string+1);//递归调用下一次反转;
string[len-1] = temp; //将temp赋给当前的最后一个字符;
}
}
调用
-(void)charreverse
{
//C语言
char ch[] = "hello,world";
char_reverse(ch);
printf("ch = %s\n",ch);
//OC语言
NSString * chinese = @"你好啊forin";
CharReverse * cr = [CharReverse new];
NSString * reverse = [cr reverseString:chinese];
NSLog(@"reversechinese=%@",reverse);
//递归
char ch1[] = "abcdefghijklmno";
reverse_string(ch1);
printf("ch1=%s ",ch1);
}
##链表反转
- 构建链表
struct Node* constructList(void)
{
// 头结点定义
struct Node *head = NULL;
// 记录当前尾结点
struct Node *cur = NULL;
for (int i = 1; i < 5; i++) {
struct Node *node = malloc(sizeof(struct Node));
node->data = i;
// printf("data=%d",node->data);
// 头结点为空,新结点即为头结点
if (head == NULL) {
head = node;
}else{ 当前结点的next为新结点
cur->next = node;
}
// 设置当前结点为新结点
cur = node;
}
return head;
}
- 反转链表
struct Node* constructList(void)
{
// 头结点定义
struct Node *head = NULL;
// 记录当前尾结点
struct Node *cur = NULL;
for (int i = 1; i < 5; i++) {
struct Node *node = malloc(sizeof(struct Node));
node->data = i;
// printf("data=%d",node->data);
// 头结点为空,新结点即为头结点
if (head == NULL) {
head = node;
}else{ 当前结点的next为新结点
cur->next = node;
}
// 设置当前结点为新结点
cur = node;
}
return head;
}
- 打印链表
void printList(struct Node *head)
{
for (int i = 1; i < 5; i++) {
printf("节点是: %d \n", head->data);
head = head->next;
}
}
调用
-(void)singleList
{
struct Node *head = constructList();
printList(head);
printf("*************ReverseList********\n");
struct Node *newhead = reverseList(head);
printList(newhead);
}
##合并有序数组
void mergeList(int a[], int aLen, int b[], int bLen, int result[])
{
int p = 0; // 遍历数组a的指针
int q = 0; // 遍历数组b的指针
int i = 0; // 记录当前存储位置
// 任一数组没有到达边界则进行遍历
while (p < aLen && q < bLen) {
// 如果a数组对应位置的值小于b数组对应位置的值
if (a[p] <= b[q]) {
// 存储a数组的值
result[i] = a[p];
// 移动a数组的遍历指针
p++;
}
else{
// 存储b数组的值
result[i] = b[q];
// 移动b数组的遍历指针
q++;
}
// 指向合并结果的下一个存储位置
i++;
}
// 如果a数组有剩余
while (p < aLen) {
// 将a数组剩余部分拼接到合并结果的后面
result[i] = a[p++];
i++;
}
// 如果b数组有剩余
while (q < bLen) {
// 将b数组剩余部分拼接到合并结果的后面
result[i] = b[q++];
i++;
}
}
调用
-(void)mergesortList
{
int a[5] = {1,4,6,7,9};
int b[8] = {2,3,5,6,8,10,11,12};
int result[13];
//归并操作
mergeList(a, 5, b, 8, result);
printf("合并结果: ");
for (int i=0; i< 13; i++) {
printf("%d, ",result[i]);
}
}
##哈希算法
char findFirstChar(char* cha)
{
char result = '\0';//空字符
// 定义一个数组 用来存储各个字母出现次数
int array[256];
// 对数组进行初始化操作
for (int i=0; i<256; i++) {
array[i] =0;
}
// 定义一个指针 指向当前字符串头部
char* p = cha;
// 遍历每个字符
while (*p != '\0') {
// 在字母对应存储位置 进行出现次数+1操作
array[*(p++)]++;
}
// 将P指针重新指向字符串头部
p = cha;
// 遍历每个字母的出现次数
while (*p != '\0') {
// 遇到第一个出现次数为1的字符,打印结果
if (array[*p] == 1)
{
result = *p;
break;
}
// 反之继续向后遍历
p++;
}
return result;
}
调用
-(void)hashresult
{
char ch[] = "abaccdeff";
char fc = findFirstChar(ch);
printf("first char = %c \n",fc);
}
##查找共同父视图
#import <UIKit/UIKit.h>
- (NSArray <UIView *> *)findCommonSuperView:(UIView *)viewOne other:(UIView *)viewOther
{
NSMutableArray *result = [NSMutableArray array];
// 查找第一个视图的所有父视图
NSArray *arrayOne = [self findSuperViews:viewOne];
// 查找第二个视图的所有父视图
NSArray *arrayOther = [self findSuperViews:viewOther];
int i = 0;
// 越界限制条件
while (i < MIN((int)arrayOne.count, (int)arrayOther.count)) {
// 倒序方式获取各个视图的父视图
UIView *superOne = [arrayOne objectAtIndex:arrayOne.count - i - 1];
UIView *superOther = [arrayOther objectAtIndex:arrayOther.count - i - 1];
// 比较如果相等 则为共同父视图
if (superOne == superOther) {
[result addObject:superOne];
i++;
}
// 如果不相等,则结束遍历
else{
break;
}
}
return result;
}
- (NSArray <UIView *> *)findSuperViews:(UIView *)view
{
// 初始化为第一父视图
UIView *temp = view.superview;
// 保存结果的数组
NSMutableArray *result = [NSMutableArray array];
while (temp) {
[result addObject:temp];
// 顺着superview指针一直向上查找
temp = temp.superview;
}
return result;
}
##查找中位数
//求一个无序数组的中位数
int findMedian(int a[], int aLen)
{
int low = 0;
int high = aLen - 1;
int mid = (aLen - 1) / 2;
int div = PartSort(a, low, high);
while (div != mid)
{
if (mid < div)
{
//左半区间找
div = PartSort(a, low, div - 1);
}
else
{
//右半区间找
div = PartSort(a, div + 1, high);
}
}
//找到了
return a[mid];
}
int PartSort(int a[], int start, int end)
{
int low = start;
int high = end;
//选取关键字
int key = a[end];
while (low < high)
{
//左边找比key大的值
while (low < high && a[low] <= key)
{
++low;
}
//右边找比key小的值
while (low < high && a[high] >= key)
{
--high;
}
if (low < high)
{
//找到之后交换左右的值
int temp = a[low];
a[low] = a[high];
a[high] = temp;
}
}
int temp = a[high];
a[high] = a[end];
a[end] = temp;
return low;
}
调用
-(void)mediannum
{
int list[9] = {12,3,10,8,6,7,11,13,9};
int median = findMedian(list, 9);
printf("median is = %d \n",median);
}