1,字符串反轉(oc)node
@interface CharReverse : NSObject
void char_reverse(char * cha);
@end
複製代碼
@implementation CharReverse
void char_reverse(char * cha){
char * begin = cha;
char * end = cha + strlen(cha) - 1;
while (begin < end) {
char temp = *begin;
*(begin++) = *end;
*(end--) = temp;
}
}
@end
複製代碼
//調用
char ch[] = "hellow,world";
char_reverse(ch);
printf("%s\n",ch);
複製代碼
字符串反轉(swift)算法
func reverseString(_ s: inout [Character]) {
guard !s.isEmpty else {
return
}
var j = s.count-1;
var i = 0;
var temp: Character;
while(i < j){ //相似冒泡
temp = s[i];
s[i] = s[j];
s[j] = temp;
i += 1;
j -= 1;
}
}
var cha:[Character] = ["C", "a", "t", "!", "🐱"]
複製代碼
//調用
reverseString(&cha)
複製代碼
2,單鏈表反轉(oc)swift
struct Node{
int data;
struct Node *next;
};
@interface LinkListReverse : NSObject
struct Node *reserveList(struct Node *head);//鏈表反轉
struct Node *constructList(void);//初始化
void printList(struct Node *head);//打印
@end
複製代碼
@implementation LinkListReverse
struct Node *reserveList(struct Node *head){
struct Node *p = head;
struct Node *newH = NULL;
while (p != NULL) {
struct Node *temp = p->next;//獲取下個指針避免丟失
p->next = newH;//接到新的指針頭
newH = p;//反轉
p = temp;//指針後移
}
return newH;
}
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;
if (head == NULL) {
head = node;
}else{
cur->next = node;
}
cur = node;
}
return head;
}
void printList(struct Node *head){
struct Node * temp = head;
while (temp!=NULL) {
printf("node is %d \n",temp->data);
temp = temp->next;
}
}
@end
複製代碼
//調用
struct Node *list = constructList();
printList(list);
printf("-----\n");
struct Node *newList = reserveList(list);
printList(newList);
複製代碼
3,給定一個整數數組 nums 和一個目標值 target,請你在該數組中找出和爲目標值的那 兩個 整數,並返回他們的數組下標。數組
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var dic = [Int:Int]()
for(index,num)in nums.enumerated(){
print(index,num)
if let oldIndex = dic[target-num] {
return [oldIndex,index]
}else{
dic[num] = index
}
}
return[-1,-1]
}
複製代碼
4,合併2個有序數組bash
@interface MergeSortedList : NSObject
void mergeSortedList(int a[],int aLen,int b[],int bLen,int result[]);
@end
複製代碼
@implementation MergeSortedList
void mergeSortedList(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) {
if (a[p] <= b[q]) {
result[i] = a[p];
p++;
}else {
result[i] = b[q];
q++;
}
i++;
}
while (p<aLen) {//a數組有剩餘,繼續遍歷a數組並賦值
result[i] = a[p++];
i++;
}
while (q<bLen) {//b數組有剩餘,繼續遍歷b數組並賦值
result[i] = b[q++];
i++;
}
}
@end
複製代碼
5,在字符串中找出第一個出現一次的字母 主要考察Hash算法 eg:字母a對應的ASCII值是97,能夠記錄在數組下標索引爲97的位置函數
char -> index //經過f(key) = key hash函數關聯
存儲和查找都經過該函數,有效提升查找效率
複製代碼
@interface HashFind : NSObject
char findFirstChar(char * cha);
@end
複製代碼
@implementation HashFind
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;
}
@end
複製代碼