力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解决方案,只求互相提升。
第1题:解码异或后的排列试题要求如下:
回答(C语言):
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* decode(int* encoded, int encodedSize, int* returnSize) {
int n = encodedSize + 1;
int total = 0;
for (int i = 1; i > k);
struct HashTable* tmp;
HASH_FIND_INT(hashTable, &x, tmp);
if (tmp != NULL) {
found = true;
break;
}
}
if (found) {
x = x_next;
} else {
// 如果没有找到满足等式的 a_i 和 a_j,那么 x 的第 k 个二进制位只能为 0
// 即为 x = x*2
x = x_next - 1;
}
}
return x;
}
运行效率如下所示:
试题要求如下:
解题思路:
二叉树的中序遍历:按照访问左子树——根节点——右子树的方式遍历这棵树,而在访问左子树或者右子树的时候我们按照同样的方式遍历,直到遍历完整棵树。因此整个遍历过程天然具有递归的性质,我们可以直接用递归函数来模拟这一过程。
回答(C语言):
/**
* 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().
*/
void inorder(struct TreeNode* root, int* res, int* resSize) {
if (!root) {
return;
}
inorder(root->left, res, resSize);
res[(*resSize)++] = root->val;
inorder(root->right, res, resSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 501);
*returnSize = 0;
inorder(root, res, returnSize);
return res;
}
运行效率如下所示:
试题要求如下:
解题思路:
此题使用二分查找,将mid输入guess函数,根据返回值调整查找边界,我这里用的是【left,mid】和【mid+1,right】,命中时将mid返回即可。
回答(C语言):
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is lower than the guess number
* 1 if num is higher than the guess number
* otherwise return 0
* int guess(int num);
*/
int guessNumber(int n){
int left=1;
int right=n;
while(left first时:third变为second、second变为first、first变为当前数;
* first > 当前数 > second时:third变为second、second变为当前数;
* second > 当前数 > third时:third变为当前数;
* 其中当前数 == first、second、third时不需要更新三者的值.
*/
if(nums[i] > first) {
third = second;
second = first;
first = nums[i];
} else if(nums[i] < first && nums[i] > second) {
third = second;
second = nums[i];
} else if(nums[i] < second && nums[i] > third) {
third = nums[i];
}
}
/* tmp1和tmp2分别与tmp3的值比较是否相等
* 若有相等的tmp值,表明数组中只有两种新的数,返回first
* 否则返回third
* 这里必须将tmp3与其他两个相比较,因为tmp3是最后改变的,
* 若tmp3也改变了,就说明有三个不同的值。
*/
if(tmp1 == tmp3 || tmp2 == tmp3) {
return first;
} else {
return third;
}
}
运行效率如下所示:
试题要求如下:
回答(C语言):
/**
* 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().
*/
void postorder(struct TreeNode *root, int *res, int *resSize) {
if (root == NULL) {
return;
}
postorder(root->left, res, resSize);
postorder(root->right, res, resSize);
res[(*resSize)++] = root->val;
}
int *postorderTraversal(struct TreeNode *root, int *returnSize) {
int *res = malloc(sizeof(int) * 2001);
*returnSize = 0;
postorder(root, res, returnSize);
return res;
}
运行效率如下所示:
试题要求如下:
回答(C语言):
/**
* Definition for a Node.
* struct Node {
* int val;
* int numChildren;
* struct Node** children;
* };
*/
int maxDepth(struct Node* root)
{
if (root == NULL)
{
return 0;
}
else {
int d = 1, m = 0;
for (int i = 0; i < root->numChildren; i++)
{
int c = maxDepth(root->children[i]);
if (m < c) { // 找子树的最大深度
m = c;
}
}
return d + m;
}
}
运行效率如下所示:
试题要求如下:
解题思路:
判定一个单词结束:当前字符非空格且下一字符为空格或结束符。
回答(C语言):
int countSegments(char * s){
int cnt = 0;
while (*s) {
if (*s != ' ' && (*(s + 1) == ' ' || *(s + 1) == '\0'))
cnt++;
s++;
}
return cnt;
}
运行效率如下所示: