QAQ
最近有点意识不清醒,过的很迷……
希望担心的事最终会有一个好结果
两道简单题,读懂了题目很容易做。
记起了StringBuffer。
leetcode868
public class Solution868 {
public int binaryGap(int n) {
int dis = 0;
int mark=1;
while(n!=0 && (n&1)!=1){
n=n>>1;
}
while(n!=0){
n = n>>1;
if((n&1)!=1){
mark++;
}else{
dis = Math.max(dis,mark);
mark = 1;
}
}
return dis;
}
}
leetcode1370
class Solution1370 {
public String sortString(String s) {
StringBuffer newS = new StringBuffer();
int[] count = new int[26];
Arrays.fill(count,0);
for(char ch : s.toCharArray()){
count[ch-97]++;
}
while(s.length()>newS.length()){
for(int i = 0;i < 26;i++){
if(count[i]!=0){
newS = newS.append((char)(97+i));
count[i]--;
}
}
if(s.length() == newS.length()){
return newS.toString();
}
for(int i = 25;i >= 0;i--){
if(count[i]!=0){
newS = newS.append((char)(97+i));
count[i]--;
}
}
}
return newS.toString();
}
}