public class test30 { //给一个字符串str,只由'X'和'.'两种字符构成。 //'X'表示墙,不能放灯,也不需要点亮。'.'表示居民区,可以放灯,也需要点亮。 //如果灯放在i位置,可以让i-1位置i位置和i+1位置全部点亮。 //返回如果点亮str中所有需要点亮的位置,至少需要几盏灯 public static int minLight(String road){ char[] str = road.toCharArray(); int index =0; int light =0; while (index <str.length){ if (str[index] == 'X') { index++; }else{ light++; if(index+1 == str.length){ break; }else{ if (str[index + 1] == 'X') { index = index+2; }else{ index = index+3; } } } } return light; } }
08-14
163
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-04
248
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)