Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
bb08e6c7
C
Cs Summary Reflection
项目概览
梦境迷离
/
Cs Summary Reflection
10 个月 前同步成功
通知
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 搜索 >>
未验证
提交
bb08e6c7
编写于
7月 31, 2020
作者:
梦境迷离
提交者:
GitHub
7月 31, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #38 from sweeneycai/sweeneycai
Sweeneycai
上级
9cfea70e
91052926
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
87 addition
and
0 deletion
+87
-0
scala-leetcode/README.md
scala-leetcode/README.md
+2
-0
scala-leetcode/src/main/scala/io/github/sweeneycai/Leetcode_365.scala
...de/src/main/scala/io/github/sweeneycai/Leetcode_365.scala
+82
-0
scala-leetcode/src/main/scala/io/github/sweeneycai/sweeneycai.md
...eetcode/src/main/scala/io/github/sweeneycai/sweeneycai.md
+3
-0
未找到文件。
scala-leetcode/README.md
浏览文件 @
bb08e6c7
...
...
@@ -9,3 +9,5 @@ Leetcode Scala 实现
## author
[
dreamylost
](
./src/main/scala/io/github/dreamylost/dreamylost.md
)
[
sweeneycai
](
./src/main/scala/io/github/sweeneycai/sweeneycai.md
)
\ No newline at end of file
scala-leetcode/src/main/scala/io/github/sweeneycai/Leetcode_365.scala
0 → 100644
浏览文件 @
bb08e6c7
package
io.github.sweeneycai
/**
* 365. 水壶问题
*/
object
WaterPouringProblem
extends
App
{
def
canMeasureWater
(
x
:
Int
,
y
:
Int
,
z
:
Int
)
:
Boolean
=
{
val
game
=
new
Pouring
(
Vector
(
x
,
y
))
game
.
solutions
(
z
).
take
(
1
).
nonEmpty
}
class
Pouring
(
capacity
:
Vector
[
Int
])
{
// States
type
State
=
Vector
[
Int
]
val
initialState
:
Vector
[
Int
]
=
capacity
map
(
x
=>
0
)
// Moves
trait
Move
{
def
change
(
state
:
State
)
:
State
}
case
class
Empty
(
glass
:
Int
)
extends
Move
{
def
change
(
state
:
State
)
:
State
=
state
updated
(
glass
,
0
)
}
case
class
Fill
(
glass
:
Int
)
extends
Move
{
def
change
(
state
:
State
)
:
State
=
state
updated
(
glass
,
capacity
(
glass
))
}
case
class
Pour
(
from
:
Int
,
to
:
Int
)
extends
Move
{
def
change
(
state
:
State
)
:
State
=
{
val
amount
=
state
(
from
)
min
(
capacity
(
to
)
-
state
(
to
))
state
updated
(
from
,
state
(
from
)
-
amount
)
updated
(
to
,
state
(
to
)
+
amount
)
}
}
val
glasses
:
Range
=
capacity
.
indices
val
moves
:
Seq
[
Move
]
=
(
for
(
g
<-
glasses
)
yield
Empty
(
g
))
++
(
for
(
g
<-
glasses
)
yield
Fill
(
g
))
++
(
for
(
from
<-
glasses
;
to
<-
glasses
if
from
!=
to
)
yield
Pour
(
from
,
to
))
class
Path
(
history
:
List
[
Move
],
val
endState
:
State
)
{
// def endState: State = (history foldRight initialState) (_ change _)
// def endState: State = (history foldRight initialState)((move, state) => move.change(state))
// private def trackState(xs: List[Move]): State = xs match {
// case Nil => initialState
// case move :: xs1 => move change trackState(xs1)
// }
def
extend
(
move
:
Move
)
:
Path
=
new
Path
(
move
::
history
,
move
change
endState
)
override
def
toString
:
String
=
(
history
.
reverse
mkString
" "
)
+
"-->"
+
endState
}
val
initialPath
=
new
Path
(
Nil
,
initialState
)
def
from
(
paths
:
Set
[
Path
],
explored
:
Set
[
State
])
:
Stream
[
Set
[
Path
]]
=
if
(
paths
.
isEmpty
)
Stream
.
empty
else
{
val
more
=
for
{
path
<-
paths
next
<-
moves
map
path
.
extend
if
!(
explored
contains
next
.
endState
)
}
yield
next
paths
#::
from
(
more
,
explored
++
(
more
map
(
_
.
endState
)))
}
val
pathSets
=
from
(
Set
(
initialPath
),
Set
(
initialState
))
def
solutions
(
target
:
Int
)
:
Stream
[
Path
]
=
for
{
pathSet
<-
pathSets
path
<-
pathSet
if
(
path
.
endState
contains
target
)
||
path
.
endState
.
sum
==
target
}
yield
path
}
val
pouring
=
new
Pouring
(
Vector
(
3
,
5
))
println
(
pouring
.
solutions
(
4
).
take
(
1
))
}
scala-leetcode/src/main/scala/io/github/sweeneycai/sweeneycai.md
0 → 100644
浏览文件 @
bb08e6c7
### 其他
-
[
水壶问题
](
https://leetcode-cn.com/problems/water-and-jug-problem/
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录