当前位置:网站首页>Sword finger offer 05 Replace spaces

Sword finger offer 05 Replace spaces

2022-07-05 05:29:00 ThE wAlkIng D

Title Description

 Insert picture description here

Problem analysis

1. Use arrays to do , First, create an array , Calculate the length of the original string as the length of the array
2. Traversal array . Space string found , Replace it with “%20”;
3. The new array builds three times the length of the original array , Used to store new strings
4. String array to string
5. Investigate .charAt Usage of , And the use of converting character arrays into strings (String a =new String())

Code instance

class Solution {
    
    public String replaceSpace(String s) {
    
        int len = s.length();
        int[] s1 = new int[3*n];
        int j = 0;
        for(int i = 0;i < n;i++){
    
        	if(s.charAt(i) == ' '){
    
				s1[j++] = "%";
				s1[j++] = "2";
				s1[j++] = "0";
			}
			else{
    
				s1[j++] = s.chaAt(i);
			}
        }
        String newStr = new String(s1,0,j);
        return newStr;
        
    }
}
原网站

版权声明
本文为[ThE wAlkIng D]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050525449215.html