1.使用StringBuffer替代String挨个字符进行操作 StringBuffer sb=new StringBuffer(str);
2.sb.charAt(i)进行字符串循环
3.sb.append(char)进行字符数组的组成
4.sb.deleteAt(i)进行指定位置字符的删除
5.若比较StringBuffer字符是否相等需要将其转换成String 使用toString()方法
1 class Solution { 2 public boolean backspaceCompare(String s, String t) { 3 StringBuffer sb=new StringBuffer(s); 4 StringBuffer sb1=new StringBuffer(t); 5 StringBuffer sb2=new StringBuffer(); 6 StringBuffer sb3=new StringBuffer(); 7 int count=0; 8 int count1=0; 9 int len=s.length(); 10 int len1=t.length(); 11 for(int i=0;i<len;i++){ 12 if(sb.charAt(i)!='#'){ 13 sb2.append(sb.charAt(i)); 14 count++; 15 }else{ 16 sb2.deleteCharAt(count-1); 17 count--; 18 } 19 } 20 for(int i=0;i<len1;i++){ 21 if(sb1.charAt(i)!='#'){ 22 sb3.append(sb1.charAt(i)); 23 count1++; 24 }else{ 25 sb3.deleteCharAt(count1-1); 26 count1--; 27 } 28 } 29 return sb2.toString().equals(sb3.toString()); 30 31 } 32 }
标签:字符,String,Q9,StringBuffer,int,sb,new,LeetCode844,退格 From: https://www.cnblogs.com/cff1/p/18234140