Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
3ef20064
C
Cs Summary Reflection
项目概览
梦境迷离
/
Cs Summary Reflection
8 个月 前同步成功
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Cs Summary Reflection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
3ef20064
编写于
3月 13, 2021
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of github.com:jxnu-liguobin/cs-summary-reflection
上级
ff4951e5
a15b1f61
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
471 addition
and
0 deletion
+471
-0
java-leetcode/src/main/java/io/github/poorguy/explore/learn/dynamicprogramming/LongestSubstringWithoutRepeatingCharacters.java
...ogramming/LongestSubstringWithoutRepeatingCharacters.java
+28
-0
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/AddAndSearchWordDataStructureDesign.java
...plore/learn/trie/AddAndSearchWordDataStructureDesign.java
+81
-0
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/ImplementTriePrefixTree.java
...b/poorguy/explore/learn/trie/ImplementTriePrefixTree.java
+88
-0
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/MapSumPairs.java
...ava/io/github/poorguy/explore/learn/trie/MapSumPairs.java
+73
-0
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/MaximumXorOfTwoNumbersInAnArray.java
...y/explore/learn/trie/MaximumXorOfTwoNumbersInAnArray.java
+20
-0
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/ReplaceWords.java
...va/io/github/poorguy/explore/learn/trie/ReplaceWords.java
+70
-0
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/WordSearch2.java
...ava/io/github/poorguy/explore/learn/trie/WordSearch2.java
+111
-0
未找到文件。
java-leetcode/src/main/java/io/github/poorguy/explore/learn/dynamicprogramming/LongestSubstringWithoutRepeatingCharacters.java
0 → 100644
浏览文件 @
3ef20064
/* All Contributors (C) 2021 */
package
io.github.poorguy.explore.learn.dynamicprogramming
;
import
java.util.HashMap
;
import
java.util.Map
;
class
LongestSubstringWithoutRepeatingCharacters
{
public
int
lengthOfLongestSubstring
(
String
s
)
{
if
(
s
.
length
()
==
0
)
{
return
0
;
}
// Character as key and index as value
Map
<
Character
,
Integer
>
map
=
new
HashMap
<>();
// two pointers
int
l
=
0
;
int
max
=
1
;
for
(
int
r
=
0
;
r
<
s
.
length
();
r
++)
{
if
(
map
.
containsKey
(
s
.
charAt
(
r
)))
{
// Math.max and +1 is important
l
=
Math
.
max
(
l
,
map
.
get
(
s
.
charAt
(
r
))
+
1
);
}
map
.
put
(
s
.
charAt
(
r
),
r
);
max
=
Math
.
max
(
max
,
r
-
l
+
1
);
}
return
max
;
}
}
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/AddAndSearchWordDataStructureDesign.java
0 → 100644
浏览文件 @
3ef20064
/* All Contributors (C) 2021 */
package
io.github.poorguy.explore.learn.trie
;
import
java.util.ArrayList
;
import
java.util.List
;
class
AddAndSearchWordDataStructureDesign
{
private
Character
ch
=
null
;
private
AddAndSearchWordDataStructureDesign
[]
children
=
new
AddAndSearchWordDataStructureDesign
[
26
];
private
boolean
isEnd
=
false
;
/** Initialize your data structure here. */
public
AddAndSearchWordDataStructureDesign
()
{}
public
void
addWord
(
String
word
)
{
AddAndSearchWordDataStructureDesign
pointer
=
this
;
char
[]
chars
=
word
.
toCharArray
();
for
(
char
c
:
chars
)
{
if
(
pointer
.
children
[
c
-
'a'
]
==
null
)
{
AddAndSearchWordDataStructureDesign
child
=
new
AddAndSearchWordDataStructureDesign
();
child
.
ch
=
c
;
pointer
.
children
[
c
-
'a'
]
=
child
;
}
pointer
=
pointer
.
children
[
c
-
'a'
];
}
pointer
.
isEnd
=
true
;
}
public
boolean
search
(
String
word
)
{
AddAndSearchWordDataStructureDesign
pointer
=
this
;
char
[]
chars
=
word
.
toCharArray
();
List
<
Character
>
charList
=
new
ArrayList
<>(
chars
.
length
);
for
(
char
c
:
chars
)
{
charList
.
add
(
c
);
}
return
search
(
charList
,
pointer
);
}
private
boolean
search
(
List
<
Character
>
charList
,
AddAndSearchWordDataStructureDesign
pointer
)
{
if
(
charList
.
size
()
==
0
)
{
return
false
;
}
if
(
charList
.
get
(
0
)
==
'.'
)
{
for
(
int
i
=
0
;
i
<
26
;
i
++)
{
if
(
pointer
.
children
[
i
]
==
null
)
{
continue
;
}
if
(
pointer
.
children
[
i
].
isEnd
&&
charList
.
size
()
==
1
)
{
return
true
;
}
boolean
result
=
search
(
charList
,
pointer
.
children
[
i
]);
if
(
result
)
{
return
true
;
}
charList
.
add
(
0
,
'.'
);
}
return
false
;
}
else
{
if
(
pointer
.
children
[
charList
.
get
(
0
)
-
'a'
]
==
null
)
{
return
false
;
}
if
(
pointer
.
children
[
charList
.
get
(
0
)
-
'a'
].
isEnd
&&
charList
.
size
()
==
1
)
{
return
true
;
}
Character
removed
=
charList
.
remove
(
0
);
boolean
result
=
search
(
charList
,
pointer
.
children
[
removed
-
'a'
]);
if
(
result
)
{
return
true
;
}
charList
.
add
(
0
,
removed
);
}
return
false
;
}
}
/**
* Your WordDictionary object will be instantiated and called as such: WordDictionary obj = new
* WordDictionary(); obj.addWord(word); boolean param_2 = obj.search(word);
*/
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/ImplementTriePrefixTree.java
0 → 100644
浏览文件 @
3ef20064
/* All Contributors (C) 2021 */
package
io.github.poorguy.explore.learn.trie
;
import
java.util.HashMap
;
import
java.util.Map
;
class
ImplementTriePrefixTree
{
private
Character
val
;
private
boolean
isEnd
;
private
Map
<
Character
,
ImplementTriePrefixTree
>
children
;
/** Initialize your data structure here. */
public
ImplementTriePrefixTree
()
{
val
=
null
;
isEnd
=
false
;
children
=
null
;
}
/** Inserts a word into the trie. */
public
void
insert
(
String
word
)
{
if
(
word
==
null
||
""
.
equals
(
word
))
{
return
;
}
char
[]
chars
=
word
.
toCharArray
();
ImplementTriePrefixTree
pointer
=
this
;
for
(
char
c
:
chars
)
{
if
(
pointer
.
children
==
null
)
{
pointer
.
children
=
new
HashMap
<>();
}
if
(
pointer
.
children
.
containsKey
(
c
))
{
pointer
=
pointer
.
children
.
get
(
c
);
}
else
{
ImplementTriePrefixTree
trie
=
new
ImplementTriePrefixTree
();
trie
.
val
=
c
;
pointer
.
children
.
put
(
c
,
trie
);
pointer
=
trie
;
}
}
pointer
.
isEnd
=
true
;
}
/** Returns if the word is in the trie. */
public
boolean
search
(
String
word
)
{
if
(
word
==
null
||
""
.
equals
(
word
))
{
return
false
;
}
char
[]
chars
=
word
.
toCharArray
();
ImplementTriePrefixTree
pointer
=
this
;
for
(
char
c
:
chars
)
{
if
(
pointer
.
children
==
null
||
pointer
.
children
.
isEmpty
())
{
return
false
;
}
ImplementTriePrefixTree
child
=
pointer
.
children
.
get
(
c
);
if
(
child
==
null
)
{
return
false
;
}
else
{
pointer
=
child
;
}
}
return
pointer
.
isEnd
;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public
boolean
startsWith
(
String
prefix
)
{
if
(
prefix
==
null
||
""
.
equals
(
prefix
))
{
return
false
;
}
char
[]
chars
=
prefix
.
toCharArray
();
ImplementTriePrefixTree
pointer
=
this
;
for
(
char
c
:
chars
)
{
if
(
pointer
.
children
==
null
||
pointer
.
children
.
isEmpty
())
{
return
false
;
}
ImplementTriePrefixTree
child
=
pointer
.
children
.
get
(
c
);
if
(
child
==
null
)
{
return
false
;
}
else
{
pointer
=
child
;
}
}
return
true
;
}
}
/**
* Your Trie object will be instantiated and called as such: Trie obj = new Trie();
* obj.insert(word); boolean param_2 = obj.search(word); boolean param_3 = obj.startsWith(prefix);
*/
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/MapSumPairs.java
0 → 100644
浏览文件 @
3ef20064
/* All Contributors (C) 2021 */
package
io.github.poorguy.explore.learn.trie
;
import
java.util.HashMap
;
import
java.util.Map
;
class
MapSumPairs
{
private
Character
ch
;
private
Map
<
Character
,
MapSumPairs
>
children
;
private
int
val
;
/** Initialize your data structure here. */
public
MapSumPairs
()
{
this
.
ch
=
null
;
this
.
children
=
null
;
this
.
val
=
0
;
}
public
void
insert
(
String
key
,
int
val
)
{
char
[]
chars
=
key
.
toCharArray
();
MapSumPairs
pointer
=
this
;
for
(
char
c
:
chars
)
{
if
(
pointer
.
children
==
null
)
{
pointer
.
children
=
new
HashMap
<>();
}
if
(
pointer
.
children
.
containsKey
(
c
))
{
pointer
=
pointer
.
children
.
get
(
c
);
}
else
{
MapSumPairs
mapSum
=
new
MapSumPairs
();
mapSum
.
ch
=
c
;
pointer
.
children
.
put
(
c
,
mapSum
);
pointer
=
mapSum
;
}
}
pointer
.
val
=
val
;
}
public
int
sum
(
String
prefix
)
{
MapSumPairs
pointer
=
this
;
char
[]
chars
=
prefix
.
toCharArray
();
for
(
char
c
:
chars
)
{
if
(
pointer
.
children
==
null
)
{
return
0
;
}
MapSumPairs
child
=
pointer
.
children
.
get
(
c
);
if
(
child
==
null
)
{
return
0
;
}
pointer
=
child
;
}
return
sum
(
pointer
);
}
private
int
sum
(
MapSumPairs
mapSum
)
{
if
(
mapSum
==
null
)
{
return
0
;
}
if
(
mapSum
.
children
==
null
||
mapSum
.
children
.
isEmpty
())
{
return
mapSum
.
val
;
}
int
sum
=
0
;
for
(
MapSumPairs
child
:
mapSum
.
children
.
values
())
{
sum
+=
sum
(
child
);
}
return
mapSum
.
val
+
sum
;
}
}
/**
* Your MapSum object will be instantiated and called as such: MapSum obj = new MapSum();
* obj.insert(key,val); int param_2 = obj.sum(prefix);
*/
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/MaximumXorOfTwoNumbersInAnArray.java
0 → 100644
浏览文件 @
3ef20064
/* All Contributors (C) 2021 */
package
io.github.poorguy.explore.learn.trie
;
class
MaximumXorOfTwoNumbersInAnArray
{
public
int
findMaximumXOR
(
int
[]
nums
)
{
if
(
nums
.
length
==
1
)
{
return
0
;
}
int
max
=
0
;
for
(
int
i
=
0
;
i
<
nums
.
length
-
1
;
i
++)
{
for
(
int
j
=
i
+
1
;
j
<
nums
.
length
;
j
++)
{
int
xor
=
nums
[
i
]
^
nums
[
j
];
if
(
xor
>
max
)
{
max
=
xor
;
}
}
}
return
max
;
}
}
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/ReplaceWords.java
0 → 100644
浏览文件 @
3ef20064
/* All Contributors (C) 2021 */
package
io.github.poorguy.explore.learn.trie
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
class
ReplaceWords
{
private
class
Trie
{
private
Character
ch
=
null
;
private
Map
<
Character
,
Trie
>
children
=
new
HashMap
<>();
private
boolean
isEnd
=
false
;
private
void
insert
(
String
str
)
{
Trie
pointer
=
this
;
char
[]
chars
=
str
.
toCharArray
();
for
(
char
c
:
chars
)
{
if
(
pointer
.
children
.
containsKey
(
c
))
{
pointer
=
pointer
.
children
.
get
(
c
);
}
else
{
Trie
child
=
new
Trie
();
child
.
ch
=
c
;
pointer
.
children
.
put
(
c
,
child
);
pointer
=
child
;
}
}
pointer
.
isEnd
=
true
;
}
// if no prefix, return str. else return shortest prefix
private
String
prefix
(
String
str
)
{
Trie
pointer
=
this
;
char
[]
chars
=
str
.
toCharArray
();
List
<
Character
>
result
=
new
ArrayList
<>();
boolean
end
=
false
;
for
(
char
c
:
chars
)
{
if
(!
pointer
.
children
.
containsKey
(
c
))
{
break
;
}
else
{
result
.
add
(
c
);
Trie
child
=
pointer
.
children
.
get
(
c
);
if
(
child
.
isEnd
)
{
end
=
true
;
break
;
}
pointer
=
child
;
}
}
if
(!
end
)
{
return
str
;
}
return
result
.
stream
().
map
(
Object:
:
toString
).
collect
(
Collectors
.
joining
(
""
));
}
}
public
String
replaceWords
(
List
<
String
>
dictionary
,
String
sentence
)
{
Trie
trie
=
new
Trie
();
for
(
String
str
:
dictionary
)
{
trie
.
insert
(
str
);
}
String
[]
words
=
sentence
.
split
(
" "
);
List
<
String
>
wordList
=
new
ArrayList
<>(
words
.
length
);
for
(
String
word
:
words
)
{
wordList
.
add
(
trie
.
prefix
(
word
));
}
return
String
.
join
(
" "
,
wordList
);
}
}
java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/WordSearch2.java
0 → 100644
浏览文件 @
3ef20064
/* All Contributors (C) 2021 */
package
io.github.poorguy.explore.learn.trie
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
/** todo a little slow */
class
WordSearch2
{
private
class
Node
{
Character
ch
=
null
;
Node
[]
children
=
new
Node
[
26
];
boolean
isEnd
=
false
;
}
private
Set
<
String
>
result
=
new
HashSet
<>();
public
List
<
String
>
findWords
(
char
[][]
board
,
String
[]
words
)
{
Node
head
=
new
Node
();
Node
pointer
=
head
;
for
(
String
word
:
words
)
{
for
(
Character
ch
:
word
.
toCharArray
())
{
if
(
pointer
.
children
[
ch
-
'a'
]
==
null
)
{
Node
child
=
new
Node
();
child
.
ch
=
ch
;
pointer
.
children
[
ch
-
'a'
]
=
child
;
}
pointer
=
pointer
.
children
[
ch
-
'a'
];
}
pointer
.
isEnd
=
true
;
pointer
=
head
;
}
for
(
int
i
=
0
;
i
<
board
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
board
[
0
].
length
;
j
++)
{
pointer
=
head
;
search
(
pointer
,
i
,
j
,
board
,
new
boolean
[
board
.
length
][
board
[
0
].
length
],
new
ArrayList
<>());
}
}
return
new
ArrayList
<>(
result
);
}
private
boolean
search
(
Node
pointer
,
int
row
,
int
col
,
char
[][]
board
,
boolean
[][]
flag
,
List
<
Character
>
chars
)
{
if
(
row
<
0
||
row
>=
board
.
length
||
col
<
0
||
col
>=
board
[
0
].
length
||
flag
[
row
][
col
])
{
return
false
;
}
char
ch
=
board
[
row
][
col
];
Node
child
=
pointer
.
children
[
ch
-
'a'
];
if
(
child
==
null
)
{
return
false
;
}
flag
[
row
][
col
]
=
true
;
chars
.
add
(
child
.
ch
);
if
(
child
.
isEnd
)
{
result
.
add
(
listToString
(
chars
));
}
boolean
hasNext
=
false
;
if
(
search
(
child
,
row
-
1
,
col
,
board
,
flag
,
chars
))
{
hasNext
=
true
;
}
if
(
search
(
child
,
row
+
1
,
col
,
board
,
flag
,
chars
))
{
hasNext
=
true
;
}
if
(
search
(
child
,
row
,
col
-
1
,
board
,
flag
,
chars
))
{
hasNext
=
true
;
}
if
(
search
(
child
,
row
,
col
+
1
,
board
,
flag
,
chars
))
{
hasNext
=
true
;
}
chars
.
remove
(
chars
.
size
()
-
1
);
flag
[
row
][
col
]
=
false
;
return
child
.
isEnd
||
hasNext
;
}
private
String
listToString
(
List
<
Character
>
list
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
Character
ch
:
list
)
{
sb
.
append
(
sb
);
}
return
sb
.
toString
();
}
// [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]]
// ["oath","pea","eat","rain"]
public
static
void
main
(
String
[]
args
)
{
WordSearch2
solution
=
new
WordSearch2
();
char
[][]
board
=
{
{
'o'
,
'a'
,
'a'
,
'n'
},
{
'e'
,
't'
,
'a'
,
'e'
},
{
'i'
,
'h'
,
'k'
,
'r'
},
{
'i'
,
'f'
,
'l'
,
'v'
}
};
String
[]
words
=
{
"oath"
,
"pea"
,
"eat"
,
"rain"
};
List
<
String
>
wordList
=
solution
.
findWords
(
board
,
words
);
wordList
.
forEach
(
System
.
out
::
println
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录