一 字符串反轉(zhuǎn)
字符串反轉(zhuǎn)指示圖.png
核心代碼:
void char_reverse(char *cha) {
char *begin = cha;//指向第一個(gè)字符
char *end = cha + strlen(cha) - 1;//指向最后一個(gè)字符
while (begin < end) {//交換字符,同時(shí)移動(dòng)指針
char temp = *begin;
*begin = *end;
*end = temp;
begin++;
end--;
}
}
二 鏈表反轉(zhuǎn)
核心代碼 [頭插法核心思想]
struct Node* reverseList(struct Node *head) {
struct Node *p = head;//遍歷指針,初始值為列表的頭結(jié)點(diǎn)
struct Node *newH = NULL;//反轉(zhuǎn)后的列表頭部
while (p != NULL) {
struct Node *temp = p->next;//暫存下一個(gè)節(jié)點(diǎn)
p->next = newH;//指向新列表的頭結(jié)點(diǎn)
newH = p;//更改新列表的頭部為當(dāng)前節(jié)點(diǎn)
p = temp;//移動(dòng)p指針
}
return newH;
}
三 有序數(shù)組合并
有序數(shù)組合并指示圖.png
核心代碼:
void mergeList(int a[], int aLen, int b[], int bLen, int result[]) {
int p = 0;
int q = 0;
int i = 0;//記錄當(dāng)前的存儲(chǔ)位置
while (p < aLen && q < bLen) {
if (a[p] < b[q]) {
result[i] = a[p];
p++;
}else {
result[i] = b[q];
q++;
}
i++;
}
while (p < aLen) {
result[i] = a[p];
p++;
i++;
}
while (q < bLen) {
result[i] = b[q];
q++;
i++;
}
}
四 Hash算法
例:在一個(gè)字符串中找到第一個(gè)只出現(xiàn)一次的字符[核心思想:生成一個(gè)數(shù)組,數(shù)組中存儲(chǔ)每個(gè)字符出現(xiàn)的次數(shù)]
char findFirstChar(char *cha) {
char result = '\0';
int array[128];//定義數(shù)組,用來記錄各個(gè)字母出現(xiàn)的次數(shù),初始值均為0
for (int i = 0; i < 128; i++) {
array[i] = 0;
}
char *p = cha;//指針p指向字符串的頭部遍歷字符串
while (*p != '\0') {
array[*p]++;
p++;
}
p = cha;//再次指向頭部遍歷字符串,找到次數(shù)為1的字符
while (*p != '\0') {
if (array[*p] == 1) {
result = *p;
break;
}
p++;
}
return result;
}
五 查找兩個(gè)子視圖的共同父視圖
倒敘比較不一樣的父試圖
- (NSArray <UIView *> *)findCommonSuperView:(UIView *)viewOne other:(UIView *)viewOther
{
NSMutableArray *result = [NSMutableArray array];
// 查找第一個(gè)視圖的所有父視圖
NSArray *arrayOne = [self findSuperViews:viewOne];
// 查找第二個(gè)視圖的所有父視圖
NSArray *arrayOther = [self findSuperViews:viewOther];
int i = 0;
// 越界限制條件
while (i < MIN((int)arrayOne.count, (int)arrayOther.count)) {
// 倒序方式獲取各個(gè)視圖的父視圖
UIView *superOne = [arrayOne objectAtIndex:arrayOne.count - i - 1];
UIView *superOther = [arrayOther objectAtIndex:arrayOther.count - i - 1];
// 比較如果相等 則為共同父視圖
if (superOne == superOther) {
[result addObject:superOne];
i++;
}
// 如果不相等,則結(jié)束遍歷
else{
break;
}
}
return result;
}
- (NSArray <UIView *> *)findSuperViews:(UIView *)view
{
// 初始化為第一父視圖
UIView *temp = view.superview;
// 保存結(jié)果的數(shù)組
NSMutableArray *result = [NSMutableArray array];
while (temp) {
[result addObject:temp];
// 順著superview指針一直向上查找
temp = temp.superview;
}
return result;
}
六 求無序數(shù)組當(dāng)中的中位數(shù)
可以有兩種解法:(1. 排序算法+中位數(shù))(2.利用快排思想)
核心相關(guān)代碼:
int findMedia(int a[],int aLen) {
int low = 0;
int high = aLen - 1;
int mid = (aLen - 1)/2;//中位點(diǎn)
int tempMedia = 0;
while (tempMedia != mid) {
tempMedia = partSort(a, low, high);
if (tempMedia > mid) {//快排的思想確定當(dāng)前基準(zhǔn)數(shù)
high = tempMedia - 1;
}else if (tempMedia < mid) {
low = tempMedia + 1;
}else {
break;
}
}
return a[tempMedia];
}
int partSort(int a[],int start, int end) {
int low = start;
int high = end;
int key = a[end];
while (low < high) {
while (low < high) {//左邊找比key大的值
if (a[low] <= key) {
low++;
}else {
int temp = a[low];
a[high] = temp;
break;
}
}
while (low < high) {//右邊找比key小的值
if (a[high] >= key) {
high--;
}else {
int temp = a[high];
a[low] = temp;
break;
}
}
}
a[low] = key;
return low;
}
七 兩個(gè)數(shù)n、m 如果是n= 2 m=5催首,用遞歸實(shí)現(xiàn)2 3 4 5相加等于14
int sum(int n, int m) {
if (m == n) {
return n;
}else {
return m + sum(n, m-1);
}
}