Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7afa074

Browse files
author
tumanob
committed
Add TwoSum Solution (JS,Scala)
Add first solution TwoSum and make template for future tests and submissions
1 parent 71f390a commit 7afa074

File tree

10 files changed

+134
-2
lines changed

10 files changed

+134
-2
lines changed

‎.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/ScalaSolutions/.idea/
2+
/ScalaSolutions/project/
3+
/ScalaSolutions/target/
4+
ScalaSolutions/default.properties

‎JsSolutions/.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"request": "launch",
7+
"name": "Launch Current Opened File",
8+
"program": "${file}"
9+
}
10+
]
11+
}

‎JsSolutions/TwoSum_p1.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* https://leetcode.com/problems/two-sum/
3+
* Easy
4+
* Given an array of integers nums and an integer target,
5+
* return indices of the two numbers such that they add up to target.
6+
* You may assume that each input would have exactly one solution,
7+
* and you may not use the same element twice.
8+
* You can return the answer in any order.
9+
*
10+
* @param {number[]} nums
11+
* @param {number} target
12+
* @return {number[]}
13+
*/
14+
var twoSum = function(nums, target) {
15+
var arrayMap = new Map();
16+
17+
for (i = 0; i < nums.length; i++) {
18+
if (arrayMap.has(target-nums[i])) {
19+
return [i, arrayMap.get(target-nums[i])];
20+
}
21+
arrayMap.set(nums[i], i);
22+
}
23+
};
24+
25+
// Tests
26+
console.log(twoSum([2,7,11,15], 9)); // [0,1]
27+
console.log(twoSum([3,2,4], 6)); // [1,2]
28+
console.log(twoSum([3,3], 6)); // [0,1]

‎JsSolutions/workspace.code-workspace

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
]
7+
}

‎README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
# leetcode
2-
My solution for leetcode puzzles
1+
# Solution for leetcode problems
2+
This project is a set of solutions from leetcode.
3+
4+
Problems are split into a folder by language used:
5+
- JavaScript - VSCode is used to build and debug. All files are located in the same folder named by the task name and problem number.
6+
- Scala - IntelliJ IDEA is my tool of choice. Problems split into packages with similar name_problem number pattern. Each solution is in its own package to make it easier to run and debug
7+
in IntelliJ IDEA that I use as my main IDE.
8+
9+

‎ScalaSolutions/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Scala Leetcode Solutions
2+
Each solution is in its own package to make it easier to run and debug
3+
in IntelliJ IDEA that I use as my main IDE.
4+
This project is a set of solutions from leetcode.
5+
6+
### sbt project compiled with Dotty
7+
8+
#### Usage
9+
10+
This is a normal sbt project, you can compile code with `sbt compile` and run it
11+
with `sbt run`, `sbt console` will start a Dotty REPL.
12+
13+
For more information on the sbt-dotty plugin, see the
14+
[dotty-example-project](https://github.com/lampepfl/dotty-example-project/blob/master/README.md).

‎ScalaSolutions/build.sbt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
val dottyVersion = "0.26.0-RC1"
2+
3+
lazy val root = project
4+
.in(file("."))
5+
.settings(
6+
name := "dotty-simple",
7+
version := "0.1.0",
8+
9+
scalaVersion := dottyVersion,
10+
11+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
12+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.io.File
2+
3+
object Main {
4+
5+
/**
6+
* Prints the list of solutions
7+
* @param args
8+
*/
9+
def main(args: Array[String]): Unit = {
10+
println("Scala Leetcode Solutions List:")
11+
getListOfSubDirectories(System.getProperty("user.dir") + "/src/main/scala").foreach(println)
12+
}
13+
14+
def getListOfSubDirectories(directoryName: String): Array[String] = {
15+
(new File(directoryName))
16+
.listFiles
17+
.filter(_.isDirectory)
18+
.map(_.getName)
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package TwoSum_p1
2+
3+
object Solution {
4+
5+
def main(args: Array[String]): Unit = {
6+
println(twoSum(Array(2,7,11,15), 18).mkString("[", ",", "]")) // [1,2]
7+
println(twoSum(Array(2,7,11,15), 9).mkString("[", ",", "]")) // [0,1]
8+
println(twoSum(Array(2,7,11,15), 26).mkString("[", ",", "]")) // [2,3]
9+
}
10+
11+
//leetcode submit region begin(Prohibit modification and deletion)
12+
def twoSum(nums: Array[Int], target: Int): Array[Int] = {
13+
def twoSum(index: Int, previous: Map[Int, Int]): Array[Int] = {
14+
previous.get(target - nums(index)) match {
15+
case Some(previousIndex) => Array(previousIndex, index)
16+
case None => twoSum(index + 1, previous ++ Map(nums(index) -> index))
17+
}
18+
}
19+
twoSum(0, Map.empty)
20+
}
21+
//leetcode submit region end(Prohibit modification and deletion)
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* For tests and checks
3+
*/
4+
5+
object Solution {
6+
7+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /