【例题1】2325. 解密消息 - 力扣(LeetCode)
int isTrue(char c,char chs[26],int pos){
int i;
for(i=0;i<pos;i++){
if(c == chs[i]){
return 0;
}
}
return 1;
}
char lookForChar(char c, char chs[26]){
for(int i = 0;i<26;i++){
if(c == chs[i]){
return (char)('a'+i);
}
}
return ' ';
}
char * decodeMessage(char * key, char * message){
char* chs = (char*)malloc(sizeof(char)*27);
int cur = 0;int pos = 0;
while(*(key+cur) != '\0' ){
char ch = *(key+cur);
if( ch<='z' && ch >= 'a' && isTrue(ch,chs,pos)){
*(chs+pos) = ch;
pos++;
}
cur++;
}
char* newStr = (char*)malloc(sizeof(char)*2001);
cur = 0;
while(*(message+cur) != '\0'){
if (*(message+cur) != ' ') {
*(newStr+cur) = lookForChar(*(message+cur),chs);
}else{
*(newStr+cur) = ' ';
}
cur++;
}
*(newStr+cur) = '\0';
return newStr;
}
【例题2】1379. 找出克隆二叉树中的相同节点 - 力扣(LeetCode)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
if(target->val == cloned->val){
return cloned;
}
if(cloned->left != NULL){
original = getTargetCopy(original,cloned->left,target);
}
if(cloned->right != NULL){
original = getTargetCopy(original,cloned->right,target);
}
return original;
}
};
【例题3】2469. 温度转换 - 力扣(LeetCode)
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
double* convertTemperature(double celsius, int* returnSize){
double* ans = (double*)malloc(sizeof(double)*2);
*(ans+0) = celsius + 273.15;
*(ans+1) = celsius*1.80 + 32.00;
*returnSize = 2;
return ans;
}
【例题4】LCR 189. 设计机械累加器 - 力扣(LeetCode)
int mechanicalAccumulator(int target) {
return target == 1 ? 1 : mechanicalAccumulator(target-1)+target;
}
【例题5】371. 两整数之和 - 力扣(LeetCode)
int getSum(int a, int b){
while(b!=0){
unsigned int temp = (unsigned int) (a&b)<<1; // 得到进位
a = a^b; // 获得无进位加法结果
b = temp;
}
return a;
}
标签:TreeNode,cur,int,C语言,第四天,cloned,target,original,入门
From: https://blog.51cto.com/u_16188762/7704751