热题100刷题记录

热题100

刷题顺序:按照难易程度排序
3/8/2021 7/100

1.两数之和

题目

image-20210308121935033

时间

3/8/2021

解题思路

c++代码

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
		vector<int> ans;
        int size=nums.size();
        for(int i=0;i<size;i++){
            int flag=0;
            for(int j=i+1;j<size;j++){
                if(nums[i]+nums[j]==target){
                    ans.push_back(i);
                    ans.push_back(j);
                    flag=1;
                    break;
                }            
            }
            if(flag==1) break;
        }
        return ans;
    }//twoSum
};//Solution

运行结果

image-20210308121919769

问题总结

对容器定义不熟悉

461.汉明距离

题目

image-20210308122534043

时间

3/8/2021 12:25-12:33

解题思路

主循环:2整除x和y的余数相比较,之后2整除x和y得到新的x和y的值

循环条件:x,y有一方不为0。

c++代码

class Solution {
public:
    int hammingDistance(int x, int y) {
        int count_bit=0;
		while(x!=0||y!=0){
            if((x%2)!=(y%2))
                count++;
            x/=2;
            y/=2;
        }
        return count;
    }
};

运行结果

image-20210308123311197

问题总结

617. 合并二叉树

题目

image-20210308123550902

时间

3/8/2021 12:36-13:16

解题思路

以tree1为主干,两棵树同步遍历。如果主树的结点为空,将tree2的结点摘下并入主干。如果tree2结点为空,返回父结点。

为了能删除结点,用后序遍历

c++代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
		if(root1==nullptr)	return root2;
        postTravel(root1,root2);
        return root1;
    }
    void postTravel(TreeNode* root1, TreeNode* root2){
        if(root2==nullptr)	return;
        if(root1->left==nullptr){
            root1->left=root2->left;
        }else{
            postTravel(root1->left,root2->left);
        }
        if(root1->right==nullptr){
            root1->right=root2->right;
        }else{
            postTravel(root1->right,root2->right);
        }
        root1->val+=root2->val;
    }
};

运行结果

image-20210308131546247

问题总结

设计多个条件判断的一定要画好分支

226. 翻转二叉树

题目

image-20210308131716517

时间

3/8/2021 13:17-13:25

解题思路

c++代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        postOrder(root);
		return root;
    }
    void postOrder(TreeNode* root){
        if(root==nullptr)
            return;
        postOrder(root->left);
        postOrder(root->right);
        TreeNode *temp=root->left;
        root->left=root->right;
        root->right=temp;
    }
};

运行结果

image-20210308132506193

问题总结

104. 二叉树的最大深度

题目

image-20210308132703255

时间

3/8/2021 13:27-13:37

解题思路

深度遍历,用一个全局变量记录最大的深度。

c++代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
		dfs(root,0);
        return max_deep;
    }
    void dfs(TreeNode* root,int deep){
        if(root==nullptr){
            if(deep>max_deep){
                max_deep=deep;
            }
            return;
        }
        dfs(root->left,deep+1);
        dfs(root->right,deep+1);
    }
private:
    int max_deep=0;
};

运行结果

image-20210308133804899

问题总结

类内变量用private修饰

206. 反转链表

题目

image-20210308134123955

时间

3/8/2021 13:41-13:56

解题思路

迭代:摘下链表,头插法

递归:尾插法

c++代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
		ListNode* head2=nullptr;
        ListNode* p=nullptr;
        while(head!=nullptr){
            p=head->next;
            head->next=head2;
            head2=head;
            head=p;
        }
        return head2;
    }
};

运行结果

image-20210308135628157

问题总结

Line 15: Char 16: runtime error: member access within misaligned address 0x000000000007 for type ‘ListNode’, which requires 8 byte alignment (ListNodeUtils.cpp)
0x000000000007: note: pointer points here

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ListNodeUtils.cpp:24:16

要给指针赋值nullptr

136. 只出现一次的数字

题目

image-20210308135837319

时间

3/8/2021 14:00-14:07

解题思路

参考题解

image-20210308140339074

c++代码

class Solution {
public:
    int singleNumber(vector<int>& nums) {
		int res=0;
        for(auto e:nums){
            res^=e;
        }
        return res;
    }
};

运行结果

问题总结

1.for(auto e:nums)

2.抑或法则

image-20210308140339074

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值