全为C语言的简单题
二叉树,每个结点最多两颗子树,次序不能颠倒,有左右之分。
struct TreeNode{
int val;
struct TreeNode *left;
struct TreeNode *right;
}
路径总和(递归解法)
bool hasPathSum(struct TreeNode *root,int targetSum){
if(!root){
return false;
}
targetSum = targetSum - root->val;
if(!root->left&&!roo->right&&targetSum==0){
return true;
}
return hasPathSum(root->leth,targetSum)||hasPathSum(root->right,targetSum);
}
单向链表数据结构
struct ListNode{
int val;
struct ListNode *next;
}
反转单向链表
struct ListNode *reverseList(struct ListNode *head){
struct ListNode *pre = NULL;
struct ListNode *cur = head;
while(cur){
struct ListNode *next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
最大公约数
int gcd(int a,int b){
if(a%b==0){
return b;
}
return gcd(b,a%b);
}
用动态分配方法malloc构成数组时,要初始化,否则不全为0
缀点成线,利用叉乘的方法(规避斜率为0的情况)
bool checkStraightLine(int **coordinates,int coordinatesSize){
if(coordinatesSize<=2){
return true;
}
else{
int x = coordinates[1][0] - coordinates[0][0];
int y = coordinates[1][1] - coordinates[0][1];
for(int i=2;i<coordinatesSize;i++){
int tx = coordinates[i][0] - coordinates[0][0];
int ty = coordinates[i][1] - coordinates[0][1];
if(tx*y!=ty*x){
return false;
}
}
}
return true;
}
哈希表求解赎金信问题
bool canConstruct(char *ransomNote,char *magazine){
int count[26] = {0};
for(int i=0;i<strlen(magazine);i++){
count[magazine[i]-'a']++;
}
for(int i=0;i<strlen(ransomNote);i++){
if(count[ransomNote[i]-'a']<1){
return false;
}
count[ransomNode[i]-'a']--;
}
return true;
}
合并两个有序链表
struct ListNode * mergeTwoLists(struct ListNode *l1,struct ListNode *l2){
if(l1==NULL){
return l2;
}
if(l2==NULL){
return l1;
}
if(l1->val<=l2->val){
l1->next = mergeTwoLists(l1->next,l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1,l2->next);
return l2;
}
}
单值二叉树
bool isUnivalTree(struct TreeNode *root){
if(root==NULL){
return true;
}
else if(root->left&&root->left->val!=root->val){
return false;
}
else if(root->right&&root->right->val!=root->val){
return false;
}
return isUnivalTree(root->left)&&isUnivalTree(root->right);
}
快排
void quickSort(int *nums,int start,int end){
if(start>=end){
return;
}
int temp = nums[start];
int i = start;
int j = end;
while(i<j){
while(i<j&&nums[j]>temp){
j--;
}
nums[i] = nums[j];
while(i<j&&nums[i]<=temp){
i++;
}
nums[j] = nums[i];
}
nums[i] = temp;
quickSort(nums,start,i-1);
quickSort(nums,i+1,end);
}
三角形最大周长
int largestPerimeter(int *A,int ASize){
quickSort(A,0,ASize-1);
for(int i=ASize-3;i>=0;i--){
if(A[i]+A[i+1]<=A[i+2]||A[i]+A[i+2]<=A[i+1]||A[i+1]+A[i+2]<=A[i]){
continue;
}
return A[i]+A[i+1]+A[i+2];
}
return 0;
}
二叉搜索树,若根节点不为空,左子树所有结点的值小于根节点的值,右子树所有结点的值大于根节点的值
二叉搜索树中的搜索(递归)
struct TreeNode *searchBST(struct TreeNode *root,int val){
if(root==NULL){
return NULL;
}
else if(root->val == val){
return root;
}
else if(root->val<val){
return searchBST(root->right,val);
}
else{
return searchBST(root->left,val);
}
}
使用最小花费爬楼梯(动态规划)
int minCostClimbing(int *cost,int costSize){
int *dp = (int *)calloc(sizeof(int),costSize+1);
dp[0] = 0;
dp[1] = 0;
for(int i=2;i<=costSize;i++){
dp[i] = Min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
}
return dp[costSize];
}
二分查找
int search(int *nums,int numsSize;int target){
int low = 0;
int high = numsSize-1;
while(low<=high){
int mid = (low+high)/2;
if(nums[mid]==target){
return mid;
}
else if(nums[mid]<target){
low = mid + 1;
}
else{
high = mid - 1;
}
}
return -1;
}
旋转字符串
bool rotateString(char *A,char *B){
if(strcmp(A,B)==0){
return true;
}
for(int i=0;i<strlen(A);i++){
char a = A[0];
for(int j=1;j<strlen;j++){
A[j-1]= A[j];
}
A[strlen(A)-1] = a;
if(strcmp(A,B)==0){
return true;
}
}
return false;
}
拓普利兹矩阵(判断对角线可以直接用i+1,j+1)
bool isToeplitzMatrix(int **matrix,int matrixSize,int *matrixColSize){
int m = matrixSize;
int n = matrixColSize[0];
for(int i=0;i<m-1;i++){
for(int j=0;j<n-1;j++){
if(matrix[i][j]!=matrix[i+1][j+1]){
return false;
}
}
}
return true;
}
int a,b=0;
只有b被赋值为0,a没有被赋值
求二叉树的坡度(节点的坡度等于该节点左子树之和和右子树节点之和的差的绝对值)整个树的坡度等于所有节点坡度之和
int ABS(int x,int y){
return (x-y)>0?x-y:y-x;
}
int DFS(struct TreeNode *root,int *returnVal){
//返回的是该节点的左子树加右子树加节点的值之和
//returnVal为该节点的坡度
if(root == NULL){
return 0;
}
int left = DFS(root->left,returnVal);
int right = DFS(root->right,returnVal);
*returnVal += ABS(left,right);
return root->val+left+right;
}
int findTilt(struct TreeNode *root){
int returnVal = 0;
DFS(root,returnVal);
return returnVal;
}
N叉树的数据结构
struct Node{
int val;
int NumChildren;
struct Node **Children;
}