
平时大家在工作当中用到的算法不算多,不过面试的话一线互联网公司喜欢问一些算法面试题。为了提高个人面试通过率可以学习了解一些基础的算法还是非常有必要的。接下来会分享一些面试当中或者面试官喜欢问的一些算法问题?来一起学习吧!
分为以下几个小章节
- 字符串反转算法
- 单链表反转算法
- 有序数组合并
- Hash算法之查找字符串中第一次出现的字符
- 两个视图的公共父视图(iOS)
以下是具体实现代码,可以参考下代码实现思路。好记性不如烂笔头大家可以敲敲代码。方便大家理解。。。。
- 请手写字符串反转算法实现
void revertString(char *cha){
char *start = cha;
char *end = cha +strlen(cha) - 1;
while(start < end){
//第一种通过指针交换头尾
char temp = *start;
*(start++) = *end;
*(end--) = temp;
//第二种通过^反转字符串
*end ^= *start;
*start ^= *end;
*end ^= *start;
start ++;
end --;
}
}
//调用
- (void)viewDidLoad {
[super viewDidLoad];
char cha[] = "hello,world";
revertString(cha);
NSLog(@"cha:%s",cha);
}
2.单链表反转如何实现?
// ReverseList.h
// 算法题专栏
////Created by Jason on 17/7/18.
// Copyright © 2018年 ET. All rights reserved.
#import <Foundation/Foundation.h>
/*** 链表反转*/
struct Node {
int data;
struct Node *next;
};
@interface ReverseList : NSObject
//链表初始化
struct Node *constructList(void);
//链表反转
struct Node *reverseList(struct Node *head);
//打印链表
void printList(struct Node *note);
@end
//链表初始化struct Node *constructList(void){
//定义链表头部
struct Node *head = NULL;
//当前链表结点
struct Node *cur = NULL;
for(int i = 0;i< 5;i++){
//开辟链表空间
struct Node *note = malloc(sizeof(struct Node));
note->data = i;
if(head == NULL){
head = note;
} else {
cur->next = note;
}
cur = note;
}
return head;
}
//链表反转
struct Node *reverseList(struct Node *head){
struct Node *p = head;
struct Node *newHead = NULL;
while(p != NULL){
struct Node *temp = p->next;
p->next = newHead;
newHead = p;
p = temp;
}
return newHead;
}
//打印函数
void printList(struct Node *note){
struct Node *temp = note;
while(temp != NULL){
print("reverseList:%d",temp->data);
temp = temp->next;
}
}有序数组合并
/** 合并两个有序数组
* a = [1 3 4 6]
* b = [2 5 7 8 9]
* result = [1 2 3 4 5 6 7 8 9]
*/
//定义函数
void mergeSortList(int a[],int aLen,int b[],int bLen,int result[]);
.m 实现void mergeSortList(int a[],int aLen,int b[],int bLen,int result[]) {
int p = 0;//指向a数组下标
int q = 0;//指向b数组下标
int i = 0;//指向result下标
while (p < aLen && q < bLen) {
if (a[p] <= b[q]) {
result[i] = a[p];//最小值放到result数组里
p++;//移动p指针
} else {
result[i] = b[q];
q++;//移动q指针
}
i++;
}
//数组剩余的全部追加到result
while (p < aLen) {
result[i] = a[p++];
i++;
}
while (q < bLen) {
result[i] = b[q++];
i++;
}
}
调用
int a[4] = {1,3,5,7};
int b[6] = {2,4,5,8,9,10};
int result[10];
mergeSortList(a, 4, b, 6, result);
printf("合并前");
for (int i = 0; i<10; i++) {
printf(" %d ",result[i]);
}
Hash算法之查找第一次不重复的字符
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') {
if (array[*p] == 1) {
result = *p;
break;
}
p++;
}
return result;
}查找两个视图的公共父视图
//查找两个视图的公共父视图
- (NSArray<UIView *>*)findCommonSuperView:(UIView *)view other:(UIView *)viewOther
{ NSMutableArray *result = [NSMutableArray array]; NSArray *arrayOne = [self findSuperViews:view]; NSArray *arrayOther = [self findSuperViews:viewOther]; int i = 0; while (i < MIN(arrayOne.count, 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]; temp = temp.superview; } return result;}