从右往左遍历乘数,将乘数的每一位与被乘数相乘得到对应的结果,再将每次得到的结果累加。这道题中,被乘数是num1,乘数是num2。
需要注意的是,num2 除了最低位以外,其余的每一位的运算结果都需要补 0。
char* multiply(char* num1, char* num2)
{
int len1 = strlen(num1);
int len2 = strlen(num2);
int len = len1 + len2;//两数相乘,得到的乘积的位数 <= strlen(num1) + strlen(num2)
int i = 0, j = 0;
int tmp = 0;
char* ret = NULL;
int buffer[220] = { 0 };
ret = (char*)malloc(220);
memset(ret, 0, 220);
if ((len1 == 1 && num1[0] == '0') || (len2 == 1 && num2[0] == '0'))
{
sprintf(ret + strlen(ret), "%c", '0');
return ret;
}
for (i = len1 - 1;i >= 0;i--)//一开始我是使用for(i = 0;i < len1 ;i++),得到错误的结果
{
for (j = len2 - 1;j >= 0;j--)//...
{
tmp = (buffer[i + j + 1] + ((num1[i] - 48) * (num2[j] - 48)));
buffer[i + j + 1] = tmp % 10;
buffer[i + j] += tmp / 10;
}
}
for (i = 0;i < len;i++)
{
printf("%d\r\n", buffer[i]);
if (i == 0 && buffer[i] == 0)
{
continue;
}
sprintf(ret + strlen(ret), "%c", buffer[i] + 48);
}
return ret;
}
int cmp_int(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
int threeSumClosest(int* nums, int numsSize, int target) {
qsort(nums, numsSize, sizeof(int), cmp_int);//使用快排进行排序
int best = nums[0] + nums[1] + nums[2];
for (int i = 0; i < numsSize; i++)
{
int left = i + 1;
int right = numsSize - 1;
while (left < right)
{
int min = nums[i] + nums[left] + nums[left + 1];
//先来算出最小值,如果target比最小值还小,后面代码就不用进行了,直接break
if (target < min)
{
if (abs(min - target) < abs(best - target))
{
best = min;
}
break;
}
int max = nums[i] + nums[right] + nums[right - 1];
//如果target比最大值还大,直接break
if (target > max)
{
if (abs(max - target) < abs(best - target))
{
best = max;
}
break;
}
int sum = nums[i] + nums[left] + nums[right];
if (sum == target)//有可能会出现相等的情况,相等了就直接返回
{
return target;
}
if (abs(sum - target) < abs(best - target))
{
best = sum;
}
if (sum > target)
{
right--;
}
else
{
left++;
}
}
}
return best;
}
void backTarck(int n, int* returnSize, char** returnStr, int leftNum, int rightNum, char* stack, int top)
{
if ((rightNum + leftNum) >= 2 * n)
{
// 当前长度已达2n
stack[top] = '\0';
if (isValid(stack))
{
returnStr[*returnSize] = (char*)malloc(sizeof(char) * (top + 1));
strcpy(returnStr[*returnSize], stack);
*returnSize = *returnSize + 1;
}
return;
}
if (leftNum < n)
{
stack[top] = '(';
backTarck(n, returnSize, returnStr, leftNum + 1, rightNum, stack, top + 1);
}
if (rightNum < n)
{
stack[top] = ')';
backTarck(n, returnSize, returnStr, leftNum, rightNum + 1, stack, top + 1);
}
}
char** generateParenthesis(int n, int* returnSize)
{
char** returnStr = (char**)malloc(sizeof(char*) * 2000);
char* stack = (char*)malloc(sizeof(char) * (n * 2 + 1));
*returnSize = 0;
backTarck(n, returnSize, returnStr, 0, 0, stack, 0);
return returnStr;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int TreeSize(struct TreeNode* root)
{
return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
}
void _postorder(struct TreeNode* root, int* a,int* pi)
{
if(root==NULL)
{
return;
}
_postorder(root->left,a,pi);
_postorder(root->right,a,pi);
a[(*pi)++]=root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
int size=TreeSize(root);
int* a=(int*)malloc(size*sizeof(int));
int i=0;
_postorder( root,a,&i);
*returnSize=size;
return a;
}
bool isValid(char * s)
{
//l存储字符串长度
int l=strlen(s);
//创建一个动态数组存储括号
char *q=(char*)malloc(l*sizeof(char));
//字符串和数组的下标
int i=0,j=0;
for(;i<l;i++)
{
//如果此时字符串为右括号,但是数组里面没有括号
//说明没有对应的左括号,匹配失败
if((s[i]==')'||s[i]==']'||s[i]=='}')&&j==0)
return 0;
//如果此时字符串为左括号,将该符号存储值数组中
if(s[i]=='{'||s[i]=='('||s[i]=='[')
q[j++]=s[i];
//如果此时字符串为右括号,与在数组的最后一位比较
//如果匹配成功,数组长度减一
//下一次循环的左括号直接覆盖原来的符号
else if(s[i]=='}'&&q[j-1]=='{')
j--;
else if(s[i]==')'&&q[j-1]=='(')
j--;
else if(s[i]==']'&&q[j-1]=='[')
j--;
//匹配失败
else
return 0;
}
//程序执行到此时,说明右括号全部匹配到对应的左括号
//只需判断此时是否还有剩余的左括号
//即数组是否为空判断字符串是否匹配成功
if(j==0)
return 1;
else
return 0;
}
这次题目有难度,要加油!!!