55. 跳跃游戏
class Solution {
public:
bool canJump(vector<int>& nums) {
int k=0;//0能到达
int n=nums.size();
for(int i=0; i<n; ++i){
if(i>k) return false;
k=max(k, nums[i]+i);
}
return true;
}
};
45. 跳跃游戏II
class Solution {
public:
int jump(vector<int>& nums) {
int end=0;//当前这组最右边位置
int maxPos=0;//当前可达最远位置
int n=nums.size();
int k=0;//跳跃次数
for(int i=0; i<n-1; ++i){//n-1是因为最后位置前面的一组已经计算过走一次要的步数了
maxPos=max(maxPos, i+nums[i]);
if(i==end){
k++;
end=maxPos;
}
}
return k;
}
};
53. 最大子数组和
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n=nums.size();
int ans=INT_MIN;
vector<int> dp(n+1,0);
for(int i=1; i<=n; ++i){
dp[i]=max(dp[i-1]+nums[i-1], nums[i-1]);
ans=max(ans, dp[i]);
}
return ans;
}
};
152. 乘积最大子数组
class Solution {
public:
int maxProduct(vector<int>& nums) {
int n=nums.size();
int maxnum=nums[0], minnum=nums[0], ans=nums[0];
for(int i=1; i<n; ++i){
int mx=maxnum, mn=minnum;//如果直接用maxnum,minnum,下面算minnum的时候代的是更新后的maxnum
maxnum=max(max(mx*nums[i], mn*nums[i]),nums[i]);
minnum=min(min(mx*nums[i], mn*nums[i]),nums[i]);
ans=max(maxnum, ans);
}
return ans;
//return ans;
}
};
198. 打家劫舍
dp1[i]是当前i要偷得到的最大金额
dp2[i]是当前i不偷得到的最大金额
class Solution {
public:
int rob(vector<int>& nums) {
int n=nums.size();
if(n==1) return nums[0];
vector<int> dp1(n,0);
vector<int> dp2(n,0);
int ans=0;
dp1[0]=nums[0];
for(int i=1; i<n; ++i){
dp1[i]=dp2[i-1]+nums[i];
dp2[i]=max(dp1[i-1], dp2[i-1]);
ans=max(max(dp1[i], dp2[i]), ans);
}
return ans;
}
};
剑指 Offer 31. 栈的压入、弹出序列
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> st;
int n=pushed.size();
int pu=0, po=0;
while(pu<n){
st.push(pushed[pu]);
while(!st.empty() && st.top()==popped[po]){
st.pop();
po++;
}
pu++;
}
if(st.empty()) return true;
else return false;
}
};
2. 两数相加
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int ans=0;
ListNode* dummyhead=new ListNode(0, nullptr);
ListNode* head=dummyhead;
ListNode* cur1=l1, *cur2=l2;
int flag=0;
while(cur1 && cur2){
int he=cur1->val+cur2->val;
he+=flag;
if(he<10){
head->next=new ListNode(he, nullptr);
head=head->next;
flag=0;
}
else{
head->next=new ListNode(he-10, nullptr);
head=head->next;
flag=1;
}
cur1=cur1->next;
cur2=cur2->next;
}
while(cur1){
int he=cur1->val;
he+=flag;
if(he<10){
head->next=new ListNode(he, nullptr);
head=head->next;
flag=0;
}
else{
head->next=new ListNode(he-10, nullptr);
head=head->next;
flag=1;
}
cur1=cur1->next;
}
while(cur2){
int he=cur2->val;
he+=flag;
if(he<10){
head->next=new ListNode(he, nullptr);
head=head->next;
flag=0;
}
else{
head->next=new ListNode(he-10, nullptr);
head=head->next;
flag=1;
}
cur2=cur2->next;
}
if(flag==1){
head->next=new ListNode(1, nullptr);
}
return dummyhead->next;
}
};
减少冗余
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummyhead=new ListNode(0, nullptr);
auto head=dummyhead;
int flag=0;
while(l1 || l2){
int n1=l1?l1->val:0;
int n2=l2?l2->val:0;
int he=n1+n2+flag;
if(he>=10) flag=1;
else flag=0;
he=he%10;
head->next=new ListNode(he, nullptr);
head=head->next;
if(l1) l1=l1->next;
if(l2) l2=l2->next;
}
if(flag==1) head->next=new ListNode(1, nullptr);
return dummyhead->next;
}
};
6. Z 字形变换
class Solution {
public:
string convert(string s, int numRows) {
if(numRows<2) return s;
int flag=-1;
vector<string> v(numRows);
int i=0;
for(auto ch:s){
if(i==0 || i==numRows-1){
flag=-flag;
}
v[i]+=ch;
i+=flag;
}
string ans;
for(auto str:v){
ans+=str;
}
return ans;
}
};
宏定义面试题
unordered_map<char, string> mp = {
{'A', "Bcd"},{'B', "bc"},{'D',"efg"}
};
int main() {
string s = "ABD";
string s1=s;
int n = s.size();
int k = 0;
while (k < n) {
if (s1[k] >= 'A' && s1[k] <= 'Z') {
s1 = s1.substr(0, k) + mp[s1[k]] + s1.substr(k + 1);
//cout << s1 << endl;
n = s1.size();
}
else k++;
}
cout << s1;
return 0;
}