热题100
刷题顺序:按照难易程度排序
3/8/2021 7/100
1.两数之和
题目
时间
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
运行结果
问题总结
对容器定义不熟悉
461.汉明距离
题目
时间
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;
}
};
运行结果
问题总结
无
617. 合并二叉树
题目
时间
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;
}
};
运行结果
问题总结
设计多个条件判断的一定要画好分支
226. 翻转二叉树
题目
时间
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;
}
};
运行结果
问题总结
无
104. 二叉树的最大深度
题目
时间
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;
};
运行结果
问题总结
类内变量用private修饰
206. 反转链表
题目
时间
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;
}
};
运行结果
问题总结
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. 只出现一次的数字
题目
时间
3/8/2021 14:00-14:07
解题思路
参考题解
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.抑或法则