文章目录
一、链表
二、BFS和DFS
leetcode130 被围绕的区域
DFS递归版本
class Solution {
public:
void dfs(vector<vector<char>>&board,int i,int j){
if(i<0 || j<0 || i>=board.size() || j>=board[0].size() || board[i][j]=='X' || board[i][j]=='#'){
return;
}
board[i][j]='#';
dfs(board,i-1,j);
dfs(board,i+1,j);
dfs(board,i,j-1);
dfs(board,i,j+1);
}
void solve(vector<vector<char>>& board) {
if(board.empty()||board[0].empty()){
return;
}
int row=board.size();
int col=board[0].size();
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
bool edge=(i==0||j==0||i==row-1||j==col-1);
if(edge&&board[i][j]=='O'){
dfs(board,i,j);
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(board[i][j]=='O'){
board[i][j]='X';
}
if(board[i][j]=='#'){
board[i][j]='O';
}
}
}
}
};
DFS非递归版本
class Solution {
private:
class pos{
public:
int i,j;
pos(int x,int y):i(x),j(y){}
};
void dfs(vector<vector<char>>& board,int i,int j){
stack<pos> st;
st.emplace(i,j);
board[i][j]='#';
int row=board.size();
int col=board[0].size();
while(!st.empty()){
pos cur=st.top();
if(cur.i-1>=0&&board[cur.i-1][cur.j]=='O'){
st.emplace(cur.i-1,cur.j);
board[cur.i-1][cur.j]='#';
continue;
}
if(cur.i+1<=row-1&&board[cur.i+1][cur.j]=='O'){
st.emplace(cur.i+1,cur.j);
board[cur.i+1][cur.j]='#';
continue;
}
if(cur.j-1>=0&&board[cur.i][cur.j-1]=='O'){
st.emplace(cur.i,cur.j-1);
board[cur.i][cur.j-1]='#';
continue;
}
if(cur.j+1<=col-1&&board[cur.i][cur.j+1]=='O'){
st.emplace(cur.i,cur.j+1);
board[cur.i][cur.j+1]='#';
continue;
}
st.pop();
}
}
public:
void solve(vector<vector<char>>& board) {
if(board.empty()||board[0].empty()){
return;
}
int row=board.size();
int col=board[0].size();
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
bool edge=(i==0)||(j==0)||(i==row-1)||(j==col-1);
if(edge&&board[i][j]=='O'){
dfs(board,i,j);
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(board[i][j]=='O'){
board[i][j]='X';
}
if(board[i][j]=='#'){
board[i][j]='O';
}
}
}
}
};
leetcode 257 二叉树的所有路径
/**
* 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<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(root==NULL){
return res;
}
string temp;
temp+=to_string(root->val);
dfs(res,root,temp);
return res;
}
void dfs(vector<string>& res,TreeNode* root,string temp){
if(root->left==NULL&&root->right==NULL){
res.push_back(temp);
}
if(root->left){
string lefttemp(temp);
lefttemp+="->";
lefttemp+=to_string(root->left->val);
dfs(res,root->left,lefttemp);
}
if(root->right){
string righttemp(temp);
righttemp+="->";
righttemp+=to_string(root->right->val);
dfs(res,root->right,righttemp);
}
}
};
leetcode 329矩阵中的最长递增路径
解法一
class Solution {
public:
int result=0;
void dfs(vector<vector<int>>& matrix,int i,int j,int step){
if(i-1>=0&&matrix[i-1][j]>matrix[i][j]){
dfs(matrix,i-1,j,step+1);
}
if(i+1<matrix.size()&&matrix[i+1][j]>matrix[i][j]){
dfs(matrix,i+1,j,step+1);
}
if(j-1>=0&&matrix[i][j-1]>matrix[i][j]){
dfs(matrix,i,j-1,step+1);
}
if(j+1<matrix[0].size()&&matrix[i][j+1]>matrix[i][j]){
dfs(matrix,i,j+1,step+1);
}
result=max(result,step);
}
int longestIncreasingPath(vector<vector<int>>& matrix) {
if(matrix.empty()){
return 0;
}
int row=matrix.size();
if(row==0){
return 0;
}
int col=matrix[0].size();
if(col==0){
return 0;
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
dfs(matrix,i,j,1);
}
}
return result;
}
};
解法二
//maxLen[i][j]表示的是以matrix[i][j]为起始点的最长递增路径
class Solution {
public:
int dfs(vector<vector<int>>& matrix,vector<vector<int>>& maxLen,int i,int j){
if(maxLen[i][j]){
return maxLen[i][j];
}
int curLen=1;
if(i-1>=0&&matrix[i-1][j]>matrix[i][j]){
maxLen[i-1][j]=dfs(matrix,maxLen,i-1,j);
curLen=max(curLen,1+maxLen[i-1][j]);
}
if(i+1<matrix.size()&&matrix[i+1][j]>matrix[i][j]){
maxLen[i+1][j]=dfs(matrix,maxLen,i+1,j);
curLen=max(curLen,1+maxLen[i+1][j]);
}
if(j-1>=0&&matrix[i][j-1]>matrix[i][j]){
maxLen[i][j-1]=dfs(matrix,maxLen,i,j-1);
curLen=max(curLen,1+maxLen[i][j-1]);
}
if(j+1<matrix[0].size()&&matrix[i][j+1]>matrix[i][j]){
maxLen[i][j+1]=dfs(matrix,maxLen,i,j+1);
curLen=max(curLen,1+maxLen[i][j+1]);
}
return curLen;
}
int longestIncreasingPath(vector<vector<int>>& matrix) {
if(matrix.empty()){
return 0;
}
int result=0;
int row=matrix.size();
int col=matrix[0].size();
vector<vector<int>> maxLen(row,vector<int>(col,0));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
result=max(result,dfs(matrix,maxLen,i,j));
}
}
return result;
}
};
解法三
class Solution {
public:
int mov[4][4]={{-1,0},{1,0},{0,-1},{0,1}};
int dfs(vector<vector<int>>& matrix,vector<vector<int>>& maxLen,int i,int j){
if(maxLen[i][j]){
return maxLen[i][j];
}
maxLen[i][j]++;
for(int k=0;k<4;k++){
int newrow=i+mov[k][0];
int newcol=j+mov[k][1];
if(newrow>=0&&matrix.size()>newrow&&newcol>=0&&newcol<matrix[0].size()&&matrix[i][j]<matrix[newrow][newcol]){
maxLen[i][j]=max(maxLen[i][j],dfs(matrix,maxLen,newrow,newcol)+1);
}
}
return maxLen[i][j];
}
int longestIncreasingPath(vector<vector<int>>& matrix) {
if(matrix.empty()){
return 0;
}
int row=matrix.size();
int col=matrix[0].size();
int result=0;
vector<vector<int>>maxLen(row,vector<int>(col,0));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
result=max(result,dfs(matrix,maxLen,i,j));
}
}
return result;
}
};
解法三 动态规划 完整版
1.将所有的坐标按照对应位置的值升序排序
2.依次处理上一步中得到的坐标列表,从附近比当前点矮的点DP值取最大加1得到当前点的DP值
时间复杂度分析:
第一步的时间复杂度是 O(log(mn))
第二步的时间复杂度是 O(mn)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#include<vector>
class Point {
public:
int x, y, v;
Point(int ix, int iy, int iv) {
x = ix;
y = iy;
v = iv;
}
};
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
if (matrix.size() == 0)
return 0;
const int rows = matrix.size();
const int cols = matrix[0].size();
vector<vector<int>> dp(rows, vector<int>(cols, 1));
vector<Point> vp;
vp.reserve(rows * cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
vp.push_back(Point(i, j, matrix[i][j]));
}
}
// 采用lambda表达式,根据读入数据依次创建坐标点,并根据升序排序
sort(vp.begin(), vp.end(), [](const Point &lhs, const Point &rhs){
return lhs.v < rhs.v;
});
for(int i=0;i<vp.size();i++){
cout<<vp[i].x<<" "<<vp[i].y<<" "<<vp[i].v<<" "<<endl;
}
int ret = 1;
for (const auto &p : vp) {
auto x = p.x;
auto y = p.y;
auto v = p.v;
// 寻找附近比当前矮的点的最高DP值
if (x > 0 && matrix[x - 1][y] < v && dp[x - 1][y] + 1 > dp[x][y])
dp[x][y] = dp[x - 1][y] + 1;
if (x < rows - 1 && matrix[x + 1][y] < v && dp[x + 1][y] + 1 > dp[x][y])
dp[x][y] = dp[x + 1][y] + 1;
if (y > 0 && matrix[x][y - 1] < v && dp[x][y - 1] + 1 > dp[x][y])
dp[x][y] = dp[x][y - 1] + 1;
if (y < cols - 1 && matrix[x][y + 1] < v && dp[x][y + 1] + 1 > dp[x][y])
dp[x][y] = dp[x][y + 1] + 1;
if (dp[x][y] > ret)
ret = dp[x][y];
}
return ret;
}
};
int main(){
int a[3][3]={{9,9,4},{6,6,8},{2,1,1}};
vector<vector<int>> brr(3,vector<int>(3,0));
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
brr[i][j]=a[i][j];
}
}
Solution s;
int ret=s.longestIncreasingPath(brr);
cout<<ret;
}
三、字符串
leetcode 415 字符串相加
class Solution {
public:
string addStrings(string num1, string num2) {
string ans="";
int fir_len=num1.size()-1,sec_len=num2.size()-1;
int carry=0;
while(fir_len>=0||sec_len>=0){
int fir=fir_len>=0?num1[fir_len]-'0':0;
int sec=sec_len>=0?num2[sec_len]-'0':0;
int tmp=fir+sec+carry;
carry=tmp/10;
int res=tmp%10;
fir_len--;
sec_len--;
ans+=to_string(res);
}
if(carry==1){
ans+="1";
}
reverse(ans.begin(),ans.end());
return ans;
}
};
leetcode 43 字符串相乘
//普通竖式
class Solution {
public:
string multiply(string num1, string num2) {
if(num1=="0"||num2=="0"){
return "0";
}
int fir_len=num1.size()-1;
int sec_len=num2.size()-1;
int carry=0;
string ans="0";
for(int i=sec_len;i>=0;i--){
string str;
int val2=num2.at(i)-'0';
for(int j=sec_len;j>i;j--){
str.push_back(0);
}
for(int j=fir_len;j>=0;j--){
int val1=num1.at(j)-'0';
int product=val1*val2+carry;
carry=product/10;
str.push_back(product%10);
}
while(carry!=0){
str.push_back(carry);
carry=carry/10;
}
reverse(str.begin(),str.end());
//为何需要以下步骤:
//因为在push_back(8)的时候,会将8视为ASCII值。所以需要依次加字符0;
for(auto &c:str){
c+='0';
}
ans=addstring(ans,str);
}
return ans;
}
string addstring(string &num1,string& num2){
int fir_len=num1.size()-1;
int sec_len=num2.size()-1;
int carry=0;
string str;
while(fir_len>=0||sec_len>=0){
int fir=fir_len>=0?num1[fir_len]-'0':0;
int sec=sec_len>=0?num2[sec_len]-'0':0;
int tmp=fir+sec+carry;
int res=tmp%10;
carry=tmp/10;
str+=to_string(res);
fir_len--;
sec_len--;
}
if(carry==1){
str+="1";
}
reverse(str.begin(),str.end());
return str;
}
};
//优化竖式
class Solution {
public:
string multiply(string num1, string num2) {
if(num1=="0" || num2=="0") return "0";
int fir_len=num1.size()-1;
int sec_len=num2.size()-1;
vector<int> res(fir_len+sec_len+2,0);
//res[i+j]存放进位的数据,res[i+j+1]存放对10取余之后的数据。
for(int i=sec_len;i>=0;i--){
int val2=num2.at(i)-'0';
for(int j=fir_len;j>=0;j--){
int val1=num1.at(j)-'0';
res[i+j+1]+=val1*val2;
res[i+j]+=res[i+j+1]/10;
res[i+j+1]=res[i+j+1]%10;
}
}
string str;
for(int i=0;i<res.size();i++){
if(i==0&&res[i]==0){
continue;
}
str.push_back(res[i]+'0');
}
return str;
}
};
leetcode 20 有效的括号
//仅用栈
class Solution {
public:
bool isValid(string s) {
int len=s.length();
if(len==0){
return true;
}
stack<char> st;
for(int i=0;i<len;i++){
if(s[i]=='('||s[i]=='{'||s[i]=='['){
st.push(s[i]);
}
if(s[i]==')'){
if(!st.empty()&&st.top()=='('){
st.pop();
}
else{
return false;
}
}
if(s[i]==']'){
if(!st.empty()&&st.top()=='['){
st.pop();
}
else{
return false;
}
}
if(s[i]=='}'){
if(!st.empty()&&st.top()=='{'){
st.pop();
}
else{
return false;
}
}
}
if(st.empty()){
return true;
}
else{
return false;
}
}
};
//栈和map
class Solution {
public:
bool isValid(string s) {
int len=s.length();
stack<char> st;
unordered_map<char,char> tmp{{')','('},{']','['},{'}','{'}};
if(len==0){
return true;
}
for(int i=0;i<len;i++){
if(s[i]=='('||s[i]=='['||s[i]=='{'){
st.push(s[i]);
continue;
}
if(!st.empty()&&tmp[s[i]]==st.top()){
st.pop();
}
else
{
return false;
}
}
if(st.empty()){
return true;
}
else{
return false;
}
}
};
四、单调栈
leetcode 84 柱状图中最大的矩形
//未加哨兵
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int len=heights.size();
if(len==0){
return 0;
}
int res=0;
stack<int> st;
for(int i=0;i<len;i++){
while(!st.empty()&&heights[st.top()]>heights[i]){
int height=heights[st.top()];
st.pop();
int width;
width=i;
if(!st.empty()){
width=i-st.top()-1;
}
res=max(res,width*height);
}
st.push(i);
}
while(!st.empty()){
int height=heights[st.top()];
st.pop();
int width;
width=len;
if(!st.empty()){
width=len-st.top()-1;
}
res=max(res,width*height);
}
return res;
}
};
//加了哨兵
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int len=heights.size();
if(len==0) return 0;
if(len==1) return heights[0];
heights.insert(heights.begin(),0);
heights.push_back(0);
stack<int> st;
st.push(0);
int res=0;
for(int i=1;i<heights.size();i++){
while(heights[st.top()]>heights[i]){
int height=heights[st.top()];
st.pop();
res=max(res,height*(i-st.top()-1));
}
st.push(i);
}
return res;
}
};
leetcode 85 最大矩形
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
int maximalRectangle(vector<vector<char>>& matrix) {
int row=matrix.size();
if(row==0){
return 0;
}
int col=matrix[0].size();
int res=0;
stack<int> st;
vector<int> vec(col+2,0);
vec[0]=-1;
vec[col+1]=-1;
for(int i=0;i<row;i++){
st.push(0);
for(int j=0;j<col;j++){
if(matrix[i][j]=='0'){
vec[j+1]=0;
}
else{
vec[j+1]++;
}
}
for(int j=1;j<col+2;j++){
while(vec[st.top()]>vec[j]){
int height=vec[st.top()];
cout<<"j的值:"<<j<<"--"<<height<<endl;
st.pop();
res=max(res,height*(j-st.top()-1));
cout<<"st.top()"<<st.top()<<endl;
cout<<"height*(j-st.top()-1:"<<height*(j-st.top()-1)<<endl;
cout<<"res:"<<res<<endl;
}
st.push(j);
}
while(!st.empty()){
st.pop();
}
}
return res;
}
int main()
{
vector<vector<char>> matrix; //这里也可以直接定义向量的尺寸
//初始化
int row;
int column;
cout << "请输入数组的行数和列数:";
cin >> row >> column;
//下面是给向量分配存储空间
matrix.resize(row);
for (int i = 0; i < row; i++)
{
matrix[i].resize(column);
}
//存入元素
cout << "请输入数组元素:" << endl;
for (int m = 0; m < row; m++)
{
for (int n = 0; n < column; n++)
{
cin >> matrix[m][n]; //利用cin在命令框中一个个输入
//也可以是下面的方式直接输入
//arr[m][n] = m+n //可以等于m+n,也可以等于其他数字
}
}
int res=maximalRectangle(matrix);
cout<<res;
return 0;
}
五、动态规划
leetcode 322 零钱兑换
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount+1,amount+1);
dp[0]=0;
for(int i=0;i<dp.size();i++){
for(int coin:coins){
if(i-coin<0){
continue;
}
dp[i]=min(dp[i],dp[i-coin]+1);
}
}
return dp[amount]==amount+1?-1:dp[amount];
}
};
leetcode 64 最小路径和
华为
#include<iostream>
using namespace std;
int main(){
int a;
while(cin>>hex>>a){
cout<<a<<endl;
}
}
#include<iostream>
using namespace std;
int main(){
int n,N;
while(cin>>N){
int arr[1001]={0};
while(N--){
cin>>n;
arr[n]=1;
}
for(int i=0;i<1001;i++){
if(arr[i]){
cout<<i<<endl;
}
}
}
}
//set容器
#include<iostream>
#include<set>
using namespace std;
int main(){
int N;
int arr[1001];
while(cin>>N){
for(int i=0;i<N;i++){
cin>>arr[i];
}
set<int> num(arr,arr+N);
for(set<int>:: iterator it=num.begin();it!=num.end();it++){
cout<<*it<<endl;
}
}
}
#include<iostream>
#include<queue>
using namespace std;
int main(){
int n;
while(cin>>n){
queue<int> q;
int count=0;
for(int i=0;i<n;i++){
q.push(i);
}
while(q.size()!=1){
if(count!=2){
int val=q.front();
q.pop();
count++;
q.push(val);
}
else{
q.pop();
count=0;
}
}
int val=q.front();
cout<<val<<endl;
}
}
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char str[101];
while(cin>>str){//或者gets(str)
int num[256]={0};
for(int i=0;str[i]!='\0';i++){
num[str[i]]++;
if(num[str[i]]==1){
cout<<str[i];
}
}
cout<<endl;
}
}
//哈希表
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main(){
string s;
while(cin>>s){
string res;
map<char,int> cnt;
for(int i=0;i<s.size();i++){
if(cnt[s[i]]==0){
cnt[s[i]]++;
res+=s[i];
}
}
cout<<res<<endl;
}
}
#include<iostream>
using namespace std;
int getMax(int* pScore,int low,int high){
if(low>high){
int temp=high;
high=low;
low=temp;
}
int maxValue=pScore[low];
for(int i=low+1;i<=high;i++){
if(pScore[i]>maxValue){
maxValue=pScore[i];
}
}
return maxValue;
}
int main(){
int N,M;
while(cin>>N>>M){
int* pScore=new int[N+1];
for(int i=1;i<=N;i++){
cin>>pScore[i];
}
for(int i=0;i<M;i++){
char ch;
int low;
int high;
cin>>ch>>low>>high;
if(ch=='Q'){
int val=getMax(pScore,low,high);
cout<<val<<endl;
}
if(ch=='U'){
pScore[low]=high;
}
}
delete [] pScore;
}
}
//vector版本
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
int N,M;
while(cin>>N>>M){
vector<int> stu(N);
char ch;
int low,high;
for(int i=0;i<N;i++){
cin>>stu[i];
}
for(int i=0;i<M;i++){
cin>>ch>>low>>high;
if(ch=='U'){
stu[low-1]=high;
}
if(ch=='Q'){
if(low>high){
swap(low,high);//若没有这句,会出现数组越界的错误。
}
cout<<*max_element(stu.begin()+low-1,stu.begin()+high)<<endl;//要出现数组越界等错误,注意看看是否首地址大于尾地址。
}
}
}
return 0;
}
erase函数的用法
stable_sort()与sort()的用法区别
C++中substr函数的用法
C++ 中max()与max_element()的区别
C++】统计string里面出现的字符的个数(使用count函数)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool compare(pair<string,int> a,pair<string,int> b){
return a.second>b.second;
}
int main(){
string input,file;
vector<pair<string,int>> errors;
while(getline(cin,input)){
int pos=input.rfind('\\');
file=input.substr(pos+1);
errors.push_back(make_pair(file,1));
for(int i=0;i<errors.size()-1;i++){
if(errors[i].first==file){
errors[i].second++;
errors.pop_back();
break;
}
}
}
stable_sort(errors.begin(),errors.end(),compare);
int idx=0;
while(idx<8&&idx<errors.size()){
string check=errors[idx].first;
int t=check.find(' ');
if(t>16){
errors[idx].first.erase(0,t-16);
}
cout<<errors[idx].first<<" "<<errors[idx].second<<endl;
idx++;
}
}
//先将所有的字符串存入哈希表,key为字符串,value为<出现顺序,出现次数>,顺序取相同的字符串的最小值,次数一直累加
//排序的话,利用set重写比较器,按次数降序,次数相同则按出现顺序排列
//插入过程利用hash时间复杂度可以认为是O(n)
//排序过程set的是红黑树,可以认为是O(nlgn) ,总的复杂度就是这个了
#include<iostream>
#include<unordered_map>
#include<set>
#include<string.h>
using namespace std;
struct info{//记录出现的顺序,和次数
int rank;
int count;
info(int rank,int count){
this->rank=rank;
this->count=count;
}
};
struct fullinfo{//一条完整的结果,字符串和次数
string file;
int rank;
int count;
fullinfo(string file,int rank,int count){
this->file=file;
this->rank=rank;
this->count=count;
}
};
struct classcomp {//set的比较器
bool operator()(const struct fullinfo& f1,const struct fullinfo& f2){
if(f1.count==f2.count)
return f1.rank<f2.rank;
return f1.count>f2.count;
}
};
typedef struct info INFO;
typedef struct fullinfo FULLINFO;
int main(){
unordered_map<string,INFO> record;
unordered_map<string,INFO>::iterator it;
unordered_map<string,INFO>::const_iterator itfind;
set<FULLINFO,classcomp> ret;
set<FULLINFO,classcomp>::iterator sit;
string linestr;//一行输入
string file;//文件名+行号
int pos;//空格的位置
int i=1;
while(getline(cin,linestr)){
if(linestr.length()==0)
break;
pos=linestr.rfind("\\");
file=linestr.substr(pos+1);//拆分得到最后的filename和count
itfind=record.find(file);//在map中查看是否已经有了该字符串,没有则插入,有则次数加1
if(itfind==record.end()){
INFO tmpi(i,1);
record.insert(pair<string,INFO>(file,tmpi));
}
else{
INFO tmpi(itfind->second.rank,itfind->second.count+1);
record.erase(file);
record.insert(pair<string,INFO>(file,tmpi));
}
i++;
}
for(it=record.begin();it!=record.end();it++){
FULLINFO tmpfull(it->first,it->second.rank,it->second.count);//构建排序的set集合
ret.insert(tmpfull);
}
for(i=0,sit=ret.begin();sit!=ret.end()&&i<8;++sit,++i){//最多输出8条记录,file少于16位
if(file.find(" ")<=16){
cout<<(*sit).file<<" "<<(*sit).count<<endl;
}
else{
cout<<(*sit).file.substr(file.find(" ")-16)<<" "<<(*sit).count<<endl;
}
}
return 0;
}
//方法一
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
string line;
while(getline(cin,line)){
if(line.find("joker JOKER")!=-1){
cout<<"joker JOKER"<<endl;
}
else{
int dash=line.find('-');
string car1=line.substr(0,dash);
string car2=line.substr(dash+1);
int c1=count(car1.begin(),car1.end(),' ');
int c2=count(car2.begin(),car2.end(),' ');
string first1=car1.substr(0,car1.find(' '));
string first2=car2.substr(0,car2.find(' '));
string str="345678910JQKA2jokerJOKER";
if(c1==c2){
if(str.find(first1)>str.find(first2)){
cout<<car1<<endl;
}
else{
cout<<car2<<endl;
}
}
else{
if(c1==3){
cout<<car1<<endl;
}
else if(c2==3){
cout<<car2<<endl;
}
else{
cout<<"ERROR"<<endl;
}
}
}
}
}
//方法二
#include <string>
#include <vector>
#include <iostream>
using namespace std;
#define PrintS1 {cout << s1 << endl;return 0;}
#define PrintS2 {cout << s2 << endl;return 0;}
int count(string & str){
int n = 0;
string::size_type i = 0 , k=0;
while ((k=str.find(' ',i)) != string::npos){
n++;i = k + 1;
}
return n;
}
int main(){
string input;
getline(cin, input);
string s1, s2;
int t = input.find('-');
s1 = input.substr(0, t);
s2 = input.substr(t + 1);
int n1 = count(s1);
int n2 = count(s2);
string POKER = "345678910JQKA2jokerJOKER";
vector<string> MAX = { "joker JOKER", "JOKER joker" };
if (s1 == MAX[0] || s1 == MAX[1]) PrintS1
if (s2 == MAX[0] || s2 == MAX[1]) PrintS2
if (n1==n2){
string f1, f2;
string::size_type k = s1.find(' ');
f1 = s1.substr(0, k);
k = s2.find(' ');
f2 = s2.substr(0, k);
if (POKER.find(f1) > POKER.find(f2))PrintS1
PrintS2
}
if (n1 == 3 && n2 != 3) PrintS1
if (n2 == 3 && n1 != 3) PrintS2
cout << "ERROR" << endl;
}
美团
class Solution {
public:
/**
*
* @param str1 string字符串
* @param str2 string字符串
* @return string字符串
*/
string getMinString(string str1, string str2) {
// write code here
if(str2.size()==0){
return str1;
}
int num1[256]={0};
int num2[256]={0};
for(int i=0;i<str1.size();i++){
num1[str1[i]]++;
}
for(int i=0;i<str2.size();i++){
num2[str2[i]]++;
}
for(int i=0;i<str2.size();i++){
if(num1[str2[i]]<num2[str2[i]]){
return "";
}
}
int front,back;
for(int i=0;i<str1.size();i++){
if(--num1[str1[i]]<num2[str1[i]]){
front=i;
break;
}
}
for(int i=str1.size()-1;i>=0;i--){
if(--num1[str1[i]]<num2[str1[i]]){
back=i;
break;
}
}
string res;
for(int i=front;i<=back;i++){
res+=str1[i];
}
return res;
}
};
//单调栈加哨兵
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
int maximalRectangle(int matrix[][20],int row,int col) {
if(row==0){
return 0;
}
int res=0;
stack<int> st;
vector<int> vec(col+2,0);
vec[0]=-1;
vec[col+1]=-1;
for(int i=0;i<row;i++){
st.push(0);
for(int j=0;j<col;j++){
if(matrix[i][j]==0){
vec[j+1]=0;
}
else{
vec[j+1]++;
}
}
for(int j=1;j<col+2;j++){
while(vec[st.top()]>vec[j]){
int height=vec[st.top()];
st.pop();
res=max(res,height*(j-st.top()-1));
}
st.push(j);
}
while(!st.empty()){
st.pop();
}
}
return res;
}
int main()
{
int matrix[20][20];
int i=-2,j=0,row=-1,col;
char ch;
while(cin.get(ch)){
if(ch=='{'){
i++;
j=0;
row++;
}
if(ch=='}'){
col=j;
}
if(ch=='1'){
matrix[i][j]=1;
j++;
}
if(ch=='0'){
matrix[i][j]=0;
j++;
}
}
int res=maximalRectangle(matrix,row,col);
cout<<res;
return 0;
}
其他
#include<typeinfo>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
double i;
int j;
cout << typeid(i).name()<<endl;
cout << typeid(j).name()<<endl;
//输出结果为i(表示int),即类型的名称首字母
cout << typeid("8"-"0").name()<<endl;
cout<<("8"-"0")<<endl;
cout<<("8"-"0")*2<<endl;
cout<<("0"-"8")<<endl;
cout<<('0'-'8')<<endl;
string str;
str.push_back(0);
cout << typeid(str[0]).name()<<endl;
cout << typeid(to_string(j)).name()<<endl;
string curr;
curr.push_back(65);
curr.push_back(3);
curr.push_back(6);
curr.push_back(8);
for(int i=0;i<curr.size();i++){
cout<<curr[i]<<" ";
}
cout<<typeid(curr[0]).name()<<endl;
for (auto &c : curr) {
c += '0';
cout<<c<<endl;
}
for(int i=0;i<curr.size();i++){
cout<<curr[i]<<" ";
}
cout<<endl;
cout<<typeid(curr[0]).name()<<endl;
string str1("some something");
return 0;
}