28. 实现 strStr()
暴力解法O(m*n)
class Solution {
public:
int strStr(string haystack, string needle) {
int n=haystack.length();
int m=needle.length();
if(n==0&&m!=0)return -1;
if(m==0)return 0;
int i=0,j=0;
while(i<n&&j<m)
{
if(haystack[i]==needle[j]){
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
}
if(j==m)return i-m;
return -1;
}
};
point:对于搜索不到时双指针的变换i=i-j+1; j=0;
库函数解法
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.empty())return 0;
int pos=haystack.find(needle);
return pos;
}
};
344. 反转字符串
class Solution {
public:
void reverseString(vector<char>& s) {
int i=0,j=s.size()-1;
while(i<j)
{
char tmp=s[i];
s[i]=s[j];
s[j]=tmp;
i++;
j--;
}
}
};
561. 数组拆分 I
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
sort(nums.begin(),nums.end());
int sum=0;
for(int i=0;i<nums.size()-1;i=i+2)
{
sum+=nums[i];
}
return sum;
}
};
1672. 最富有客户的资产总量
class Solution {
public:
int maximumWealth(vector<vector<int>>& accounts) {
int n=accounts.size();
int m=accounts[0].size();
int ans=0;
for(int i=0;i<n;i++)
{
int sum=0;
for(int j=0;j<m;j++)
{
sum+=accounts[i][j];
}
ans=ans>sum?ans:sum;
}
return ans;
}
};
27. 移除元素
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n=nums.size();
int j=0;
for(int i=0;i<n;i++)
{
if(nums[i]!=val)
{
nums[j]=nums[i];
j++;
}
}
return j;
}
};
485. 最大连续1的个数
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int n=nums.size();
int l=0,ans=0;
for(int r=0;r<n;r++)
{
if(nums[r]==1)l=r;
while(l<n&&nums[l]==1)
{
l++;
}
ans=(l-r)>ans?(l-r):ans;
}
return ans;
}
};
209. 长度最小的子数组
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int n=nums.size();
int len=n;
for(int i=0;i<n;i++)
{
int j=i;
int sum=0;
while(sum<s&&j<n){
sum+=nums[j];
j++;
}
if(sum>=s)len=len<(j-i)?len:(j-i);
if(sum<s&&j-i>=n) return 0;
}
return len;
}
};
// if(sum<s&&j-i>=n) return 0;
return len==n+1?0:len;
1512. 好数对的数目
class Solution {
public:
int numIdenticalPairs(vector<int>& nums) {
int n=nums.size();
int count=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(nums[i]==nums[j])count++;
}
}
return count;
}
};
118. 杨辉三角
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans(numRows);
for(int i=0;i<numRows;i++)
{
ans[i].resize(i + 1);
for(int j=0;j<=i;j++)
{
if(i==j||j==0)ans[i][j]=1;
else
{
ans[i][j]=ans[i-1][j-1]+ans[i-1][j];
}
}
}
return ans;
}
};
point: resize
119. 杨辉三角 II
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<vector<int>> tri(rowIndex+1);
// cout<<tri[0].size()<<endl;
for(int i=0;i<=rowIndex;i++)
{
tri[i].resize(i+1);
// cout<<tri[i].size()<<endl;
for(int j=0;j<=i;j++)
{
if(j==i||j==0) {
tri[i][j]=1;
}
else {
tri[i][j]=tri[i-1][j-1]+tri[i-1][j];
}
}
}
return tri[rowIndex];
}
};
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+1);
for(int i=0;i<=rowIndex;i++)
{
ans[i]=1;
for(int j=i-1;j>0;j--)
{
ans[j]=ans[j]+ans[j-1];
}
}
return ans;
}
};
557. 反转字符串中的单词 III
class Solution {
public:
string reverseWords(string s) {
int n=s.length();
int i=0,j=0;
while(i<n&&j<n)
{
int j=i;
while(s[i]!=' '&&i<n)i++;
myreverse(s,j,i);
i++;
}
return s;
}
void myreverse(string &s,int left,int right)
{
int l=left,r=right;
while(l<r)
{
swap(s[l],s[r-1]);
l++;
r--;
}
}
};
class Solution {
public:
string reverseWords(string s) {
istringstream ss(s);
string ans,str;
while(ss>>str)
{
reverse(str.begin(),str.end());
ans+=str+' ';
}
return ans.substr(0,ans.length()-1);
}
};
144. 二叉树的前序遍历
/**
* 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:
void traversal(TreeNode* cur, vector<int>& vec)
{
if(cur==nullptr)return;
vec.push_back(cur->val);//middle
traversal(cur->left,vec);//left
traversal(cur->right,vec);//right
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
traversal(root,ans);
return ans;
}
};
94. 二叉树的中序遍历
void traversal(TreeNode* cur, vector<int>& vec)
{
if(cur==nullptr)return;
traversal(cur->left,vec);//left
vec.push_back(cur->val);//middle
traversal(cur->right,vec);//right
}
145. 二叉树的后序遍历
void traversal(TreeNode* cur, vector<int>& vec)
{
if(cur==nullptr)return;
traversal(cur->left,vec);//left
traversal(cur->right,vec);//right
vec.push_back(cur->val);//middle
}
102. 二叉树的层序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
queue<TreeNode*> que;
if(root!=NULL) que.push(root);
while(!que.empty())
{
vector<int> vec;
int n=que.size();
for(int i=0;i<n;i++)
{
TreeNode* node=que.front();
que.pop();
vec.push_back(node->val);
if(node->left)que.push(node->left);
if(node->right)que.push(node->right);
}
ans.push_back(vec);
}
return ans;
}
};
107. 二叉树的层次遍历 II
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> ans;
queue<TreeNode*> que;
if(root!=NULL)que.push(root);
while(!que.empty())
{
vector<int> level;
int n=que.size();
for(int i=0;i<n;i++)
{
TreeNode* node=que.front();
que.pop();
level.push_back(node->val);
if(node->left)que.push(node->left);
if(node->right)que.push(node->right);
}
ans.push_back(level);
}
reverse(ans.begin(),ans.end());
return ans;
}
};
199. 二叉树的右视图
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> que;
vector<int> ans;
if(root!=NULL)que.push(root);
while(!que.empty())
{
int n=que.size();
for(int i=0;i<n;i++)
{
TreeNode*node=que.front();
que.pop();
if(i==n-1)ans.push_back(node->val);
if(node->left)que.push(node->left);
if(node->right)que.push(node->right);
}
}
return ans;
}
};
637. 二叉树的层平均值
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> ans;
queue<TreeNode*> que;
if(root!=NULL)que.push(root);
while(!que.empty())
{
int n=que.size();
double sum=0;
for(int i=0;i<n;i++)
{
TreeNode* node=que.front();
que.pop();
sum+=node->val;
if(node->left)que.push(node->left);
if(node->right)que.push(node->right);
}
ans.push_back(sum/n);
}
return ans;
}
};
429. N 叉树的层序遍历
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> ans;
queue<Node*> que;
if(root!=NULL)que.push(root);
while(!que.empty())
{
vector<int> level;
int n=que.size();
for(int i=0;i<n;i++)
{
Node* first=que.front();
que.pop();
level.push_back(first->val);
for(Node* child : first->children)
{
if(child)que.push(child);
}
}
ans.push_back(level);
}
return ans;
}
};
515. 在每个树行中找最大值
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> ans;
queue<TreeNode*> que;
if(root!=NULL) que.push(root);
while(!que.empty())
{
int max=INT_MIN;
int n=que.size();
for(int i=0;i<n;i++)
{
TreeNode* node=que.front();
que.pop();
max=max>node->val?max:node->val;
if(node->left)que.push(node->left);
if(node->right)que.push(node->right);
}
ans.push_back(max);
}
return ans;
}
};
116、117. 填充每个节点的下一个右侧节点指针 II
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if(root!=NULL)que.push(root);
while(!que.empty())
{
int n=que.size();
for(int i=0;i<n;i++)
{
Node* first=que.front();
que.pop();
Node* second=que.front();
if(i==n-1)first->next=NULL;
else first->next=second;
if(first->left)que.push(first->left);
if(first->right)que.push(first->right);
}
}
return root;
}
};
1470. 重新排列数组
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
auto ans=nums;
int i=0,j=n;
for(int k=0;k<2*n;k+=2)
{
ans[k]=nums[i];
ans[k+1]=nums[j];
i++;
j++;
}
return ans;
}
};