Skip to content

Commit 22598dd

Browse files
author
chenweijie
committed
替换字符串中的空格
1 parent ac3179a commit 22598dd

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

src/main/java/com/chen/algorithm/exercise/Solution.java renamed to src/main/java/com/chen/algorithm/exercise/ArraySearch.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.chen.algorithm.exercise;
22

3-
/**
4-
* @author : chen weijie
5-
* @Date: 2019-02-20 12:53 AM
6-
*/
3+
74

85

96
/**
@@ -14,7 +11,12 @@
1411
* 当要查找的数字比右上角的数字小时,左移;
1512
* 如果出了边界,则说明二维数组中不存在该整数。
1613
*/
17-
public class Solution {
14+
/**
15+
* @author : chen weijie
16+
* @Date: 2019-02-20 12:53 AM
17+
*/
18+
19+
public class ArraySearch {
1820

1921

2022
public static boolean find(int target, int[][] array) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.chen.algorithm.exercise;
2+
3+
/**
4+
* 请实现一个函数,将一个字符串中的每个空格替换成“%20”。
5+
* 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
6+
*
7+
* @author : chen weijie
8+
* @Date: 2019-02-21 12:19 AM
9+
*/
10+
public class ReplaceSpace {
11+
12+
13+
public static String solution(String str) {
14+
15+
if (str == null || "".equals(str)) {
16+
return null;
17+
}
18+
19+
StringBuilder sb = new StringBuilder();
20+
for (int i = 0; i < str.length(); i++) {
21+
22+
if (str.charAt(i) == ' ') {
23+
sb.append("%20");
24+
} else {
25+
sb.append(str.charAt(i));
26+
}
27+
}
28+
return sb.toString();
29+
}
30+
31+
32+
public static void main(String[] args) {
33+
String str = "we are very happy!";
34+
str = solution(str);
35+
System.out.println("转换后的字符串为:" + str);
36+
}
37+
38+
39+
}

0 commit comments

Comments
 (0)