【例题1】2744. 最大字符串配对数目 - 力扣(LeetCode)
int maximumNumberOfStringPairs(char ** words, int wordsSize){
int ans = 0;
for(int i=0;i<wordsSize-1;i++){
for(int j=i+1;j<wordsSize;j++){
// 字符串长度不相等
int len1 = strlen(words[i]);
int len2 = strlen(words[j]);
if(len1 != len2) continue;
// 长度相等,字符逆序不符合,跳出
int index = 0;
while(index < len1){
if(words[i][index] != words[j][len1-1-index]){
index = -1;
break;
}
index++;
}
if(index != -1) {
ans++;
break;
}
}
}
return ans;
}
【例题2】537. 复数乘法 - 力扣(LeetCode)
// 得到四个参数
void getNumber(int* arr, char* num1){
int flag1=0;
int i=0;
if(num1[i] == '-'){
flag1 = 1;i++;
}
int a = num1[i]-'0';
i++;
while(num1[i]!='+'){
a=a*10;
a+=(num1[i])-'0';
i++;
}
//跳过+号
i++;
int flag2 = 0; // 需不需要-号
if(num1[i] == '-') {
flag2=1;i++; //跳过负号
}
int b = num1[i]-'0';
i++;
while(num1[i]!='i'){
b=b*10;
b+=(num1[i])-'0';
i++;
}
arr[0] = flag1==1?-a:a;
arr[1] = flag2==1?-b:b;
}
//得到结果字符串
void getStr(char* str,int L,int R){
int start = 10000;
int i = 0;
if(L<0){
str[i++] = '-';
L*=-1;
}
while(L / start == 0){
start /= 10;
if(start == 0){
str[i++] = '0';break;
}
}
while(start != 0){
str[i++] = L/start+'0';
L%=start;
start/=10;
}
str[i++] = '+';
start = 10000;
if(R<0){
str[i++] = '-';
R*=-1;
}
while(R / start == 0){
start /= 10;
if(start == 0){
str[i++] = '0';break;
}
}
while(start != 0){
str[i++] = R/start+'0';
R%=start;
start/=10;
}
str[i++] = 'i';
str[i] = '\0';
}
//主程序
char * complexNumberMultiply(char * num1, char * num2){
// a+bi * c+di
// ac - bd + (ad+bc)i
int* arr1 = (int*)malloc(sizeof(int)*2);
int* arr2 = (int*)malloc(sizeof(int)*2);
getNumber(arr1,num1);
getNumber(arr2,num2);
//arr1[0] = a
//arr1[1] = b
//arr2[0] = c
//arr2[1] = d
char* str = (char*)malloc(sizeof(char)*50);
int i=0;
int L = arr1[0] * arr2[0] - arr1[1] * arr2[1]; // ac-bd
int R = arr1[0] * arr2[1] + arr1[1] * arr2[0]; // ad+bc
getStr(str,L,R);
return str;
}
【例题3】2181. 合并零之间的节点 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeNodes(struct ListNode* head){
struct ListNode* O = head;
struct ListNode* L = head;
struct ListNode* R = head->next;
while(R!=NULL){
while(R->next->val!=0){
// 下一个位置 不为0 将值累加
R->next->val += R->val;
R=R->next;
}
// 找到0位置,将结果保存在0位置上
R->next->val = R->val;
R=R->next;
L->next = R;
L = R;// 调整指针
R = L->next;
}
return O->next;
}
【例题4】1823. 找出游戏的获胜者 - 力扣(LeetCode)
struct Node{
int val; // 序号 1 - n
struct Node* next;
};
//类约瑟夫问题
int findTheWinner(int n, int k){
if(n == 1) return 1;
if(n == 2) return k%2==0?1:2;
if(k == 1) return n;
//循环链表
struct Node * head = (struct Node*)malloc(sizeof(struct Node));
struct Node * R = head;
R->val = 1;
for(int i=2;i<=n;i++){
struct Node * node = (struct Node*)malloc(sizeof(struct Node));
node->val = i; // 序号值
R->next = node; // R的下一个结点
R = node; // R进入下一个结点
node->next = head; // 循环链表
}
int i=0;
//以R结点为起始点,判断下一个点是否为需要删去的点
//直到剩下最后一个点
while(1){
i++;
if(i%k==0){
R->next = R->next->next;
continue;
}
if(R->next == R){
return R->val;
}
R = R->next;
}
return -1;
}
【例题5】2120. 执行所有后缀指令 - 力扣(LeetCode)
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* executeInstructions(int n, int* startPos, int startPosSize, char * s, int* returnSize){
int len = strlen(s);
int* ans = (int*)malloc(sizeof(int)*len);
*returnSize = len;
for(int i=0;i<len;i++){
int x = startPos[1]; // 列
int y = startPos[0]; // 行
int count = 0;
for(int j=i;j<len;j++){
switch(s[j]){
case 'L': x--;break;
case 'R': x++;break;
case 'U': y--;break;
case 'D': y++;break;
}
if(x<0 || x>=n || y<0 || y>=n) break;
count++;
}
ans[i] = count;
}
return ans;
}
标签:第二十八,struct,val,int,Coding,next,++,C语言,num1
From: https://blog.51cto.com/u_16188762/8070144