leetcode:. 984 contain character string AAA and BBB (greedy)

topic:

Here Insert Picture Description

analysis:

The problem is put position . Great idea:
now put the number of small, put 2,5. . . Done and put 0,3 .. . . The rest can be put more.
More simply, directly initialized and more

Code:

char c1='b';
 char c2='a';
 int n1=B;
 int n2=A; 
 if(A>B) 
 {
  c1='a';
  c2='b';
  n1=A;
  n2=B;
 }
 string ss(A+B,c1);
 //添加n2个c2 
 int all=(ss.length()-1+1)/3;
 //0 1 2 | 3 4 5 | 6 7      5 6 7都归结为 5   x+1 /3 *3 
 for(int i=2;i<(ss.length()-1+1)/3*3;i=i+3)
 {
  ss[i]=c2;
  } 
 int c=0;
 while(1)
 {
  if(all==n2) break;
  ss[c]=c2;
  c=c+3;
  all++;
 }

Achievements: The second of the winter

Here Insert Picture Description

Published 51 original articles · won praise 20 · views 674

Guess you like

Origin blog.csdn.net/weixin_42721412/article/details/104104772
Recommended