Skip to content

Commit 1372185

Browse files
committed
update
1 parent 3412bfa commit 1372185

File tree

9 files changed

+99
-13
lines changed

9 files changed

+99
-13
lines changed

algs4-scala/src/main/scala/org/gs/digraph/AcyclicSP.scala

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class AcyclicSP(g: EdgeWeightedDigraph, s: Int) {
1919
private val edgeTo = Array.fill[Option[DirectedEdge]](g.numV)(None)
2020
private val topological = new Topological(g)
2121

22-
topological.order match {
22+
topological.order() match {
2323
case None => throw new IllegalArgumentException(s"EdgeWeightedDigraph:$g is not acyclic")
2424
case Some(x) =>
2525
for {
@@ -29,8 +29,8 @@ class AcyclicSP(g: EdgeWeightedDigraph, s: Int) {
2929
}
3030

3131
private def relax(e: DirectedEdge): Unit = {
32-
val v = e.from
33-
val w = e.to
32+
val v = e.from()
33+
val w = e.to()
3434
if (_distTo(w) > _distTo(v) + e.weight) {
3535
_distTo(w) = _distTo(v) + e.weight
3636
edgeTo(w) = Some(e)
@@ -49,10 +49,9 @@ class AcyclicSP(g: EdgeWeightedDigraph, s: Int) {
4949
else {
5050
val path = new ListBuffer[DirectedEdge]()
5151

52-
@tailrec
53-
def loop(e: DirectedEdge) {
52+
@tailrec def loop(e: DirectedEdge) {
5453
e +=: path
55-
edgeTo(e.from) match {
54+
edgeTo(e.from()) match {
5655
case None =>
5756
case Some(x) => loop(x)
5857
}

algs4-scala/src/main/scala/org/gs/digraph/EdgeWeightedDepthFirstOrder.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.gs.digraph
22

3-
import scala.collection.mutable.Queue
4-
53
/** Depth first order for an EdgeWeightedDigraph
64
*
75
* @constructor creates a new EdgeWeightedDepthFirstOrder with an EdgeWeightedDigraph, vertex count
@@ -15,7 +13,7 @@ class EdgeWeightedDepthFirstOrder(g: EdgeWeightedDigraph) extends BaseDepthFirst
1513
preCounter += 1
1614
_pre(v) = preCounter
1715
preOrder.enqueue(v)
18-
g.adj(v) foreach (e => if (!marked(e.to)) dfs(e.to))
16+
g.adj(v) foreach (e => if (!marked(e.to())) dfs(e.to()))
1917
postOrder.enqueue(v)
2018
postCounter += 1
2119
_post(v) = postCounter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.gs.digraph
2+
3+
import scala.annotation.tailrec
4+
5+
class IndexedSeq[+A] extends scala.collection.IndexedSeq[A] {
6+
override def apply(i: Int): A = ???
7+
8+
override def length: Int = ???
9+
10+
override def foldLeft[B](z: B)(op: (B, A) => B): B = {
11+
@inline @tailrec def inner(acc: B, n: Int): B =
12+
if (n == length) acc
13+
else inner(op(acc, apply(n)), n + 1)
14+
15+
inner(z, 0)
16+
}
17+
18+
override def foldRight[B](z: B)(op: (A, B) => B): B = {
19+
@inline @tailrec def inner(acc: B, n: Int): B =
20+
if (n == 0) acc
21+
else inner(op(apply(n), acc), n - 1)
22+
23+
inner(z, length)
24+
}
25+
}

algs4-scala/src/main/scala/org/gs/digraph/Topological.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class Topological[A <: DigraphMarker](g: A) {
2222
case d: Digraph => new DepthFirstOrder(d)
2323
case e: EdgeWeightedDigraph => new EdgeWeightedDepthFirstOrder(e)
2424
}
25-
Some(dfs.reversePost)
25+
Some(dfs.reversePost())
2626
} else None
2727

2828
private lazy val _order = createOrder(!finder.hasCycle)
2929

3030
/** returns if it has a topological order */
31-
def hasOrder(): Boolean = _order != None
31+
def hasOrder(): Boolean = _order.isDefined
3232

3333
/** returns list of vertex numbers in topological order */
3434
def order(): Option[List[Int]] = _order

build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Commons._
22

33
ThisBuild / version := "1.0.0"
44

5-
ThisBuild / scalaVersion := "2.13.1"
5+
ThisBuild / scalaVersion := "2.13.2"
66

77
ThisBuild / shellPrompt := (s => Project.extract(s).currentProject.id + " > ")
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode.p100
2+
3+
object P006ZigZagConversion {
4+
def convert(s: String, numRows: Int): String = {
5+
if (numRows < 2) return s
6+
7+
val bf = new StringBuilder()
8+
val N = s.length
9+
var i = 0
10+
val cycleLen = 2 * numRows - 2
11+
while (i < numRows) {
12+
var j = 0
13+
while (j + i < N) {
14+
bf.addOne(s.charAt(j + i))
15+
val IDX = j + cycleLen - i
16+
if (i != 0 && i != numRows - 1 && IDX < N) {
17+
bf.addOne(s.charAt(IDX))
18+
}
19+
20+
j += cycleLen
21+
}
22+
i += 1
23+
}
24+
bf.toString()
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode.p100
2+
3+
import org.scalatest.matchers.should.Matchers
4+
import org.scalatest.wordspec.AnyWordSpec
5+
6+
class P006ZigZagConversionTest extends AnyWordSpec with Matchers {
7+
import P006ZigZagConversion._
8+
val S = "PAYPALISHIRING"
9+
"P006ZigZagConversionTest" should {
10+
"convert1" in { convert(S, 3) shouldBe "PAHNAPLSIIGYIR" }
11+
"convert2" in { convert(S, 4) shouldBe "PINALSIGYAHRPI" }
12+
}
13+
}

project/build.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.3.8
1+
sbt.version=1.3.10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package study
2+
3+
sealed trait Animal
4+
5+
class Cat extends Animal {
6+
def meow(): Unit = println("meow")
7+
}
8+
9+
class Dog extends Animal {
10+
def woof(): Unit = println("woof")
11+
}
12+
13+
object Animal {
14+
def perform(animal: Animal): Unit = {
15+
val func = animal match {
16+
case cat: Cat => cat.meow _
17+
case dog: Dog => dog.woof _
18+
}
19+
func.apply()
20+
}
21+
22+
def main(args: Array[String]): Unit = {
23+
perform(new Cat())
24+
}
25+
}

0 commit comments

Comments
 (0)