Skip to content

Commit c3bf9fb

Browse files
committed
数据结构算法
1 parent 3f7893a commit c3bf9fb

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.dunwu.ds.str;
2+
3+
// 【二进制求和】
4+
//
5+
// 给定两个二进制字符串,返回他们的和(用二进制表示)。
6+
//
7+
// 输入为非空字符串且只包含数字 1 和 0。
8+
//
9+
// 示例 1:
10+
//
11+
// 输入: a = "11", b = "1"
12+
// 输出: "100"
13+
// 示例 2:
14+
//
15+
// 输入: a = "1010", b = "1011"
16+
// 输出: "10101"
17+
18+
19+
/**
20+
* @author Zhang Peng
21+
* @date 2018-11-04
22+
*/
23+
public class AddBinary {
24+
public static String addBinary(String a, String b) {
25+
StringBuilder sb = new StringBuilder();
26+
int i = a.length() - 1, j = b.length() - 1, carry = 0;
27+
while (i >= 0 || j >= 0) {
28+
int sum = carry;
29+
if (j >= 0) {
30+
sum += b.charAt(j--) - '0';
31+
}
32+
if (i >= 0) {
33+
sum += a.charAt(i--) - '0';
34+
}
35+
sb.append(sum % 2);
36+
carry = sum / 2;
37+
}
38+
if (carry != 0) {
39+
sb.append(carry);
40+
}
41+
return sb.reverse().toString();
42+
}
43+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.github.dunwu.ds.str;
2+
3+
4+
// 【实现 strStr() 函数】
5+
//
6+
// 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
7+
//
8+
// 示例 1:
9+
//
10+
// 输入: haystack = "hello", needle = "ll"
11+
// 输出: 2
12+
// 示例 2:
13+
//
14+
// 输入: haystack = "aaaaa", needle = "bba"
15+
// 输出: -1
16+
// 说明:
17+
//
18+
// 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
19+
//
20+
// 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
21+
22+
23+
/**
24+
* @author Zhang Peng
25+
* @date 2018-11-05
26+
*/
27+
public class ImplementStrstr {
28+
public static int strStr(String haystack, String needle) {
29+
if (haystack.equals(needle)) {
30+
return 0;
31+
}
32+
33+
if (haystack == null || haystack.length() == 0) {
34+
return -1;
35+
}
36+
37+
if (needle == null || needle.length() == 0) {
38+
return 0;
39+
}
40+
41+
if (haystack.length() < needle.length()) {
42+
return -1;
43+
}
44+
45+
int begin = 0;
46+
int i = 0;
47+
int j = 0;
48+
while (i < haystack.length() && begin < haystack.length()) {
49+
if (j == needle.length()) {
50+
return begin;
51+
} else if (haystack.charAt(i) == needle.charAt(j)) {
52+
i++;
53+
j++;
54+
} else {
55+
j = 0;
56+
begin++;
57+
i = begin;
58+
}
59+
}
60+
61+
if (i == haystack.length() && j == needle.length()) {
62+
return begin;
63+
}
64+
return -1;
65+
}
66+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.github.dunwu.ds.str;
2+
3+
// 【最长公共前缀】
4+
//
5+
// 编写一个函数来查找字符串数组中的最长公共前缀。
6+
//
7+
// 如果不存在公共前缀,返回空字符串 ""。
8+
//
9+
// 示例 1:
10+
//
11+
// 输入: ["flower","flow","flight"]
12+
// 输出: "fl"
13+
// 示例 2:
14+
//
15+
// 输入: ["dog","racecar","car"]
16+
// 输出: ""
17+
// 解释: 输入不存在公共前缀。
18+
// 说明:
19+
//
20+
// 所有输入只包含小写字母 a-z 。
21+
22+
23+
/**
24+
* @author Zhang Peng
25+
* @date 2018-11-05
26+
*/
27+
public class LongestCommonPrefix {
28+
public static String longestCommonPrefix(String[] strs) {
29+
if (strs == null || strs.length == 0) {
30+
return "";
31+
}
32+
33+
int index = 0;
34+
StringBuilder sb = new StringBuilder();
35+
36+
while (index < strs[0].length()) {
37+
char c = strs[0].charAt(index);
38+
boolean flag = true;
39+
for (String str : strs) {
40+
if (index >= str.length()) {
41+
flag = false;
42+
break;
43+
}
44+
if (str.charAt(index) != c) {
45+
flag = false;
46+
break;
47+
}
48+
}
49+
50+
if (flag) {
51+
sb.append(c);
52+
index++;
53+
} else {
54+
break;
55+
}
56+
}
57+
return sb.toString();
58+
}
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.dunwu.ds.str;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* @author Zhang Peng
8+
* @date 2018-11-05
9+
*/
10+
public class AddBinaryTest {
11+
@Test
12+
public void test() {
13+
Assert.assertEquals("100", AddBinary.addBinary("11", "1"));
14+
Assert.assertEquals("10101", AddBinary.addBinary("1010", "1011"));
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.dunwu.ds.str;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* @author Zhang Peng
8+
* @date 2018-11-05
9+
*/
10+
public class ImplementStrstrTest {
11+
@Test
12+
public void test() {
13+
Assert.assertEquals(0, ImplementStrstr.strStr("", ""));
14+
Assert.assertEquals(-1, ImplementStrstr.strStr("aaa", "aaaa"));
15+
Assert.assertEquals(0, ImplementStrstr.strStr("aaa", ""));
16+
Assert.assertEquals(2, ImplementStrstr.strStr("hello", "ll"));
17+
Assert.assertEquals(-1, ImplementStrstr.strStr("aaaaa", "bba"));
18+
Assert.assertEquals(1, ImplementStrstr.strStr("mississippi", "issi"));
19+
Assert.assertEquals(9, ImplementStrstr.strStr("mississippi", "pi"));
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.dunwu.ds.str;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* @author Zhang Peng
8+
* @date 2018-11-05
9+
*/
10+
public class LongestCommonPrefixTest {
11+
@Test
12+
public void test() {
13+
String[] strs1 = {"flower", "flow", "flight"};
14+
String[] strs2 = {"dog", "racecar", "car"};
15+
16+
Assert.assertEquals("fl", LongestCommonPrefix.longestCommonPrefix(strs1));
17+
Assert.assertEquals("", LongestCommonPrefix.longestCommonPrefix(strs2));
18+
}
19+
}

0 commit comments

Comments
 (0)