- 字符串反轉(zhuǎn)
- 鏈表反轉(zhuǎn)
- 有序數(shù)組合并
- Hash算法
- 查找兩個(gè)子視圖的共同父視圖
- 求無序數(shù)組當(dāng)中的中位數(shù)
一趴樱、字符串反轉(zhuǎn)
給定字符串 "hello,world",實(shí)現(xiàn)將其反轉(zhuǎn)。輸出結(jié)果:dlrow,olleh
- (void)charReverse
{
NSString * string = @"hello,world";
NSLog(@"%@",string);
NSMutableString * reverString = [NSMutableString stringWithString:string];
for (NSInteger i = 0; i < (string.length + 1)/2; i++) {
[reverString replaceCharactersInRange:NSMakeRange(i, 1) withString:[string substringWithRange:NSMakeRange(string.length - i - 1, 1)]];
[reverString replaceCharactersInRange:NSMakeRange(string.length - i - 1, 1) withString:[string substringWithRange:NSMakeRange(i, 1)]];
}
NSLog(@"reverString:%@",reverString);
//C
char ch[100];
memcpy(ch, [string cStringUsingEncoding:NSUTF8StringEncoding], [string length]);
//設(shè)置兩個(gè)指針脖苏,一個(gè)指向字符串開頭,一個(gè)指向字符串末尾
char * begin = ch;
char * end = ch + strlen(ch) - 1;
//遍歷字符數(shù)組,逐步交換兩個(gè)指針?biāo)赶虻膬?nèi)容甘凭,同時(shí)移動(dòng)指針到對應(yīng)的下個(gè)位置捶枢,直至begin>=end
while (begin < end) {
char temp = *begin;
*(begin++) = *end;
*(end--) = temp;
}
NSLog(@"reverseChar[]:%s",ch);
}
二握截、鏈表反轉(zhuǎn)
反轉(zhuǎn)前:1->2->3->4->NULL
反轉(zhuǎn)后:4->3->2->1->NULL
/** 定義一個(gè)鏈表 */
struct Node {
NSInteger data;
struct Node * next;
};
- (void)listReverse
{
struct Node * p = [self constructList];
[self printList:p];
//反轉(zhuǎn)后的鏈表頭部
struct Node * newH = NULL;
//頭插法
while (p != NULL) {
//記錄下一個(gè)結(jié)點(diǎn)
struct Node * temp = p->next;
//當(dāng)前結(jié)點(diǎn)的next指向新鏈表的頭部
p->next = newH;
//更改新鏈表頭部為當(dāng)前結(jié)點(diǎn)
newH = p;
//移動(dòng)p到下一個(gè)結(jié)點(diǎn)
p = temp;
}
[self printList:newH];
}
/**
打印鏈表
@param head 給定鏈表
*/
- (void)printList:(struct Node *)head
{
struct Node * temp = head;
printf("list is : ");
while (temp != NULL) {
printf("%zd ",temp->data);
temp = temp->next;
}
printf("\n");
}
/** 構(gòu)造鏈表 */
- (struct Node *)constructList
{
//頭結(jié)點(diǎn)
struct Node *head = NULL;
//尾結(jié)點(diǎn)
struct Node *cur = NULL;
for (NSInteger i = 0; i < 10; i++) {
struct Node *node = malloc(sizeof(struct Node));
node->data = i;
//頭結(jié)點(diǎn)為空,新結(jié)點(diǎn)即為頭結(jié)點(diǎn)
if (head == NULL) {
head = node;
}else{
//當(dāng)前結(jié)點(diǎn)的next為尾結(jié)點(diǎn)
cur->next = node;
}
//設(shè)置當(dāng)前結(jié)點(diǎn)為新結(jié)點(diǎn)
cur = node;
}
return head;
}
三烂叔、有序數(shù)組合并
將有序數(shù)組 {1,4,6,7,9} 和 {2,3,5,6,8,9,10,11,12} 合并為
{1,2,3,4,5,6,6,7,8,9,9,10,11,12}
- (void)orderListMerge
{
int aLen = 5,bLen = 9;
int a[] = {1,4,6,7,9};
int b[] = {2,3,5,6,8,9,10,11,12};
[self printList:a length:aLen];
[self printList:b length:bLen];
int result[14];
int p = 0,q = 0,i = 0;//p和q分別為a和b的下標(biāo)谨胞,i為合并結(jié)果數(shù)組的下標(biāo)
//任一數(shù)組沒有達(dá)到s邊界則進(jìn)行遍歷
while (p < aLen && q < bLen) {
//如果a數(shù)組對應(yīng)位置的值小于b數(shù)組對應(yīng)位置的值,則存儲(chǔ)a數(shù)組的值,并移動(dòng)a數(shù)組的下標(biāo)與合并結(jié)果數(shù)組的下標(biāo)
if (a[p] < b[q]) result[i++] = a[p++];
//否則存儲(chǔ)b數(shù)組的值蒜鸡,并移動(dòng)b數(shù)組的下標(biāo)與合并結(jié)果數(shù)組的下標(biāo)
else result[i++] = b[q++];
}
//如果a數(shù)組有剩余胯努,將a數(shù)組剩余部分拼接到合并結(jié)果數(shù)組的后面
while (++p < aLen) {
result[i++] = a[p];
}
//如果b數(shù)組有剩余,將b數(shù)組剩余部分拼接到合并結(jié)果數(shù)組的后面
while (q < bLen) {
result[i++] = b[q++];
}
[self printList:result length:aLen + bLen];
}
- (void)printList:(int [])list length:(int)length
{
for (int i = 0; i < length; i++) {
printf("%d ",list[i]);
}
printf("\n");
}
四逢防、HASH算法
- 哈希表
例:給定值是字母a叶沛,對應(yīng)ASCII碼值是97,數(shù)組索引下標(biāo)為97忘朝。
這里的ASCII碼灰署,就算是一種哈希函數(shù),存儲(chǔ)和查找都通過該函數(shù),有效地提高查找效率氓侧。 -
在一個(gè)字符串中找到第一個(gè)只出現(xiàn)一次的字符脊另。如輸入"abaccdeff",輸出'b'
字符(char)是一個(gè)長度為8的數(shù)據(jù)類型约巷,因此總共有256種可能偎痛。每個(gè)字母根據(jù)其ASCII碼值作為數(shù)組下標(biāo)對應(yīng)數(shù)組種的一個(gè)數(shù)字。數(shù)組中存儲(chǔ)的是每個(gè)字符出現(xiàn)的次數(shù)独郎。
- (void)hashTest
{
NSString * testString = @"hhaabccdeef";
char testCh[100];
memcpy(testCh, [testString cStringUsingEncoding:NSUTF8StringEncoding], [testString length]);
int list[256];
for (int i = 0; i < 256; i++) {
list[i] = 0;
}
char *p = testCh;
char result = '\0';
while (*p != result) {
list[*(p++)]++;
}
p = testCh;
while (*p != result) {
if (list[*p] == 1) {
result = *p;
break;
}
p++;
}
printf("result:%c",result);
}
五踩麦、查找兩個(gè)子視圖的共同父視圖
思路:分別記錄兩個(gè)子視圖的所有父視圖并保存到數(shù)組中,然后倒序?qū)ふ?直至找到第一個(gè)不一樣的父視圖氓癌。
- (void)findCommonSuperViews:(UIView *)view1 view2:(UIView *)view2
{
NSArray * superViews1 = [self findSuperViews:view1];
NSArray * superViews2 = [self findSuperViews:view2];
NSMutableArray * resultArray = [NSMutableArray array];
int i = 0;
while (i < MIN(superViews1.count, superViews2.count)) {
UIView *super1 = superViews1[superViews1.count - i - 1];
UIView *super2 = superViews2[superViews2.count - i - 1];
if (super1 == super2) {
[resultArray addObject:super1];
i++;
}else{
break;
}
}
NSLog(@"resultArray:%@",resultArray);
}
- (NSArray <UIView *>*)findSuperViews:(UIView *)view
{
UIView * temp = view.superview;
NSMutableArray * result = [NSMutableArray array];
while (temp) {
[result addObject:temp];
temp = temp.superview;
}
return result;
}
六谓谦、求無序數(shù)組中的中位數(shù)
中位數(shù):當(dāng)數(shù)組個(gè)數(shù)n為奇數(shù)時(shí),為(n + 1)/2贪婉,即是最中間那個(gè)數(shù)字反粥;當(dāng)n為偶數(shù)時(shí),為(n/2 + (n/2 + 1))/2,即是中間兩個(gè)數(shù)字的平均數(shù)疲迂。
首先要先去了解一些幾種排序算法:iOS排序算法
思路:
- 1.排序算法+中位數(shù)
首先用冒泡排序才顿、快速排序、堆排序尤蒿、希爾排序等排序算法將所給數(shù)組排序郑气,然后取出其中位數(shù)即可。 - 2.利用快排思想