Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions C++/1143.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int longestCommonSubsequence(string t, string s) {
int tn=t.size(),sn=s.size();
if(tn>sn){
swap(tn,sn);
swap(t,s);
}
vector<int> v(tn+1);
auto x=v;
int ans=0;
for(int i=0 ; i<sn ; i++){
for(int j=0 ; j<tn ; j++){
if(s[i]==t[j]){
v[j+1]=x[j]+1;
}
else{
v[j+1]=max(v[j],x[j+1]);
}
}
swap(x,v);
v.clear();
v.resize(tn+1);
}
return x[tn];
}
};
33 changes: 33 additions & 0 deletions C++/nextpermutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
void nextPermutation(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int sz = num.size();
int k=-1;
int l;
//step1
for (int i=0;i<sz-1;i++){
if (num[i]<num[i+1]){
k=i;
}
}
if (k==-1){
sort(num.begin(),num.end());
return;
}

//step2
for (int i=k+1;i<sz;i++){
if (num[i]>num[k]){
l=i;
}
}
//step3
int tmp = num[l];
num[l]=num[k];
num[k]=tmp;
//step4
reverse(num.begin()+k+1,num.end());
}
};
34 changes: 34 additions & 0 deletions C++/stringstream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str)
{
// Complete this function
stringstream ss(str);
vector<int> result;
char ch;
int tmp;

while (ss >> tmp)
{
result.push_back(tmp);
ss >> ch;
}

return result;
}

int main()
{
string str;
cin >> str;
vector<int> integers = parseInts(str);
for(int i = 0; i < integers.size(); i++)
{
cout << integers[i] << "\n";
}

return 0;
}