Skip to content

Commit 13e6e84

Browse files
committed
fd
1 parent 7fbdd2e commit 13e6e84

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.inuker.solution;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by liwentian on 2017/12/10.
7+
*/
8+
9+
public class LongestWordInDictionaryThroughDeleting {
10+
11+
public String findLongestWord(String s, List<String> d) {
12+
String longest = "";
13+
for (String dictWord : d) {
14+
int i = 0;
15+
16+
/**
17+
* 首先要走一遍s的所有字符,如果对上了word则i++
18+
* 看能不能将word从头对到尾
19+
*/
20+
for (char c : s.toCharArray()) {
21+
if (i < dictWord.length() && c == dictWord.charAt(i)) i++;
22+
}
23+
24+
if (i == dictWord.length() && dictWord.length() >= longest.length()) {
25+
/**
26+
* 相同长度时按字符排序
27+
*/
28+
if (dictWord.length() > longest.length() || dictWord.compareTo(longest) < 0) {
29+
longest = dictWord;
30+
}
31+
}
32+
}
33+
return longest;
34+
}
35+
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,26 @@
3131
public class main {
3232

3333
public static void main(String[] args) {
34-
List<Integer> list = new test().countSmaller(new int[] {
35-
5, 2, 6, 1
36-
});
37-
for (Integer n : list) {
38-
System.out.print(n + " ");
39-
}
34+
String s = findLongestWord("abpcplea", Arrays.asList("ale","apple","monkey","plea"));
35+
System.out.println(s);
4036
}
4137

4238

43-
static class test {
44-
class Node {
45-
Node left, right;
46-
int val;
47-
48-
/**
49-
* 左子树的节点个数
50-
*/
51-
int sum;
52-
53-
/**
54-
* 节点重复数
55-
*/
56-
int dup = 1;
57-
58-
public Node(int v, int s) {
59-
val = v;
60-
sum = s;
39+
public static String findLongestWord(String s, List<String> d) {
40+
String longest = "";
41+
for (String dictWord : d) {
42+
int i = 0;
43+
for (char c : s.toCharArray()) {
44+
if (i < dictWord.length() && c == dictWord.charAt(i)) i++;
6145
}
62-
}
63-
64-
public List<Integer> countSmaller(int[] nums) {
65-
Node root = null;
66-
Integer[] res = new Integer[nums.length];
67-
for (int i = nums.length - 1; i >= 0; i--) {
68-
root = insert(root, nums[i], 0, res, i);
69-
}
70-
return Arrays.asList(res);
71-
}
7246

73-
private Node insert(Node root, int n, int pre, Integer[] res, int i) {
74-
if (root == null) {
75-
root = new Node(n, 0);
76-
res[i] = pre;
77-
} else if (root.val == n) {
78-
root.dup++;
79-
res[i] = pre + root.sum;
80-
} else if (n < root.val) {
81-
root.sum++;
82-
root.left = insert(root.left, n, pre, res, i);
83-
} else {
84-
root.right = insert(root.right, n, pre + root.sum + root.dup, res, i);
47+
if (i == dictWord.length() && dictWord.length() >= longest.length()) {
48+
if (dictWord.length() > longest.length() || dictWord.compareTo(longest) < 0) {
49+
longest = dictWord;
50+
}
8551
}
86-
return root;
8752
}
53+
return longest;
8854
}
8955

9056

0 commit comments

Comments
 (0)