-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/string/add binary #1
base: master
Are you sure you want to change the base?
Changes from all commits
91def6d
5c4a6f5
56de1b0
44b16da
73afb39
ee480ad
c55d9a3
af315d0
5f166a9
b7595ab
4e27564
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,91 @@ | ||
package algorithms.array | ||
|
||
import scala.collection.mutable | ||
|
||
/** | ||
* There is a parking lot with only one empty spot. Given the initial state | ||
* of the parking lot and the final state. Each step we are only allowed to | ||
* move a car | ||
* out of its place and move it into the empty spot. | ||
* The goal is to find out the least movement needed to rearrange | ||
* the parking lot from the initial state to the final state. | ||
* | ||
* Say the initial state is an array: | ||
* | ||
* [1,2,3,0,4], | ||
* where 1,2,3,4 are different cars, and 0 is the empty spot. | ||
* | ||
* And the final state is | ||
* | ||
* [0,3,2,1,4]. | ||
* We can swap 1 with 0 in the initial array to get [0,2,3,1,4] and so on. | ||
* Each step swap with 0 only. | ||
* # There is a parking lot with only one empty spot. Given the initial state | ||
*# of the parking lot and the final state. Each step we are only allowed to | ||
*# move a car | ||
*# out of its place and move it into the empty spot. | ||
*# The goal is to find out the least movement needed to rearrange | ||
*# the parking lot from the initial state to the final state. | ||
** | ||
# Say the initial state is an array: | ||
** | ||
# [1,2,3,0,4], | ||
*# where 1,2,3,4 are different cars, and 0 is the empty spot. | ||
** | ||
# And the final state is | ||
** | ||
# [0,3,2,1,4]. | ||
*# We can swap 1 with 0 in the initial array to get [0,2,3,1,4] and so on. | ||
*# Each step swap with 0 only. | ||
** | ||
Example | ||
*input : 1, 2, 3, 0, 4 | ||
*final : 0, 3, 2, 1, 4 | ||
** | ||
answer : 4 steps | ||
** | ||
step 1 : 0, 2, 3, 1, 4 | ||
*step 2 : 2, 0, 3, 1, 4 // 없으면 0 제외한 가장 앞에 것과 swap | ||
*step 3 : 2, 3, 0, 1, 3 | ||
*step 4 : 0, 3, 2, 1, 4 | ||
*site : https://discuss.leetcode.com/topic/67928/rearrange-parking-lot | ||
*site : https://www.careercup.com/question?id=5687257423937536 | ||
* | ||
* User: before30 | ||
* Date: 2017. 5. 9. | ||
* Time: PM 8:49 | ||
*/ | ||
object Garage { | ||
|
||
def apply(init: List[Int], fin: List[Int]): Int = { | ||
if (init.size != fin.size) throw new IllegalArgumentException("Size of initial and final state is not equal") | ||
|
||
var i = 0 | ||
var move = 0 | ||
var list = init.drop(0) | ||
|
||
while (list != fin) { | ||
val v = list.apply(i) | ||
val f = fin.apply(i) | ||
def swap(current: List[Int], emptyIdx: Int, targetIdx: Int): List[Int] = { | ||
current.updated(emptyIdx, current(targetIdx)).updated(targetIdx, current(emptyIdx)) | ||
} | ||
|
||
if (v != 0 && v != f) { | ||
list = list.updated(list.indexOf(0), v).updated(i, 0) | ||
move += 1 | ||
def isFinished(currentGarage: List[Int], finalGarage: List[Int]): Boolean = { | ||
currentGarage == finalGarage | ||
} | ||
|
||
if (list.indexOf(v) != fin.indexOf(v)) { | ||
list = list.updated(list.indexOf(v), list.apply(fin.indexOf(v))).updated(fin.indexOf(v), v) | ||
move += 1 | ||
def calc(initGarage: scala.collection.mutable.MutableList[Int], desigredGarage: List[Int]): Int = { | ||
var step = 0 | ||
for ( i <- 0 until initGarage.length ) { | ||
if (initGarage(i) != desigredGarage(i) | ||
&& desigredGarage(i) != 0) { | ||
val indexOfZero = initGarage.indexOf(0) | ||
if (i != indexOfZero) { | ||
val temp = initGarage(i) | ||
initGarage(i) = 0 | ||
initGarage(indexOfZero) = temp | ||
step = step + 1 | ||
} | ||
val indexOfTarget = initGarage.indexOf(desigredGarage(i)) | ||
initGarage(i) = initGarage(indexOfTarget) | ||
initGarage(indexOfTarget) = 0 | ||
step = step + 1 | ||
} | ||
|
||
i = (i + 1) % init.size | ||
} | ||
|
||
move | ||
step | ||
} | ||
|
||
def getFirstDeprecntExceptZero(currentGarage: List[Int], finalGarage: List[Int]): Either[String, Int] = { | ||
|
||
Left("nothing") | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
// println(swap(List(1,2,3,4,0), 4, 0)) | ||
// println(isFinished(List(1, 2, 3, 4, 5), List(1, 2, 3, 4, 5))) | ||
// println(isFinished(List(1,2,3,4,5), List(2,1,3,4,5))) | ||
|
||
// println(calc(List(1,2,3,0), List(1,2,3,0))) | ||
/* | ||
input : 1, 2, 3, 0, 4 | ||
final : 0, 3, 2, 1, 4 | ||
|
||
*/ | ||
println(calc(mutable.MutableList(1, 2, 3, 0, 4), List(0, 3, 2, 1, 4))) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기존의 구현이 잘못된 것이 아니라면 굳이 빼지 않고 새로운 구현을 추가해나가는 형태로 하는것이 어떨까 합니다.
또 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package algorithms.array | ||
|
||
import scala.collection.immutable.Stream.Empty | ||
|
||
/** | ||
* User: before30 | ||
* Date: 2017. 5. 8. | ||
* Time: AM 8:59 | ||
* | ||
* Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. | ||
* You may assume the integer do not contain any leading zero, except the number 0 itself. | ||
* The digits are stored such that the most significant digit is at the head of the list. | ||
* | ||
* Example | ||
* f([1, 2, 3] : 123) = [1, 2, 4]: 124 | ||
* f([9, 9, 9] : 999) = [1, 0, 0, 0]: 1000 | ||
*/ | ||
object PlusOne { | ||
|
||
def apply(digits: List[Int], digit: Int): List[Int] = { | ||
plusDigit(digits, digit) | ||
} | ||
|
||
def apply(digits: List[Int]): List[Int] = { | ||
plusOne(digits) | ||
} | ||
|
||
def plusOne(digits: List[Int]): List[Int] = { | ||
plusDigit(digits, 1) | ||
} | ||
|
||
def plusDigit(digits: List[Int], digit: Int): List[Int] = { | ||
val test = calc(digits.reverse, digit, Nil) | ||
println(test) | ||
test | ||
} | ||
|
||
def calc(digits: List[Int], carry: Int, acc: List[Int]): List[Int] = { | ||
digits match { | ||
case Nil if carry == 0 => acc | ||
case Nil if carry > 0 => carry::acc | ||
case _ => { | ||
val temp = digits.head + carry | ||
calc(digits.tail, temp/10, temp%10::acc) | ||
} | ||
} | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
plusOne(List(1,2,3)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package algorithms.string | ||
|
||
/** | ||
* Created by before30 on 16/05/2017. | ||
*/ | ||
|
||
/* | ||
Given two binary strings, | ||
return their sum (also a binary string). | ||
|
||
For example, | ||
a = "11" | ||
b = "1" | ||
Return "100". | ||
*/ | ||
|
||
object AddBinary { | ||
|
||
// "1111", "1" -> 10000 | ||
|
||
def add(list1: List[Int], list2: List[Int], carry: Int, acc: List[Int]): List[Int] = { | ||
if (list1.isEmpty && list2.isEmpty && carry != 0) carry::acc | ||
else if(list1.isEmpty && list2.isEmpty && carry == 0) acc | ||
else { | ||
val (h1, t1) = if (!list1.isEmpty) (list1.head, list1.tail) else (0, Nil) | ||
val (h2, t2) = if (!list2.isEmpty) (list2.head, list2.tail) else (0, Nil) | ||
val c1 = (h1 + h2 + carry) / 2 | ||
val v = (h1 + h2 + carry) % 2 | ||
add(t1, t2, c1, v :: acc) | ||
} | ||
|
||
|
||
} | ||
def apply(a: String, b: String): String = { | ||
|
||
add(a.toList.map(x => x - '0').reverse, b.toList.map(x => x - '0').reverse, 0, Nil).mkString("") | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
println("10000 == " + AddBinary("1111", "1")) | ||
println("0 == " + AddBinary("0", "0")) | ||
println("10 == " + AddBinary("1", "1")) | ||
println("10000000001 == " + AddBinary("10000000000", "1")) | ||
println("10000 == " + AddBinary("1101", "11")) | ||
println("10000 ==" + AddBinary("1", "1111")) | ||
println("101 ==" + AddBinary("001", "100")) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package algorithms.array | ||
|
||
/** | ||
* User: before30 | ||
* Date: 2017. 5. 10. | ||
* Time: PM 12:02 | ||
* | ||
* Difficulty : Medium | ||
* Given a string, find the length of the longest substring without repeating characters. | ||
* | ||
* Examples: | ||
* Given "abcabcbb", the answer is "abc", which the length is 3. | ||
* Given "bbbbb", the answer is "b", with the length of 1. | ||
* Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. | ||
* | ||
* site : https://leetcode.com/problems/longest-substring-without-repeating-characters/#/description | ||
*/ | ||
object LongestSubstring { | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이부분은 구현이 빠져있네요 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package algorithms.array | ||
|
||
import algorithms.UnitSpec | ||
|
||
/** | ||
* User: before30 | ||
* Date: 2017. 5. 8. | ||
* Time: AM 9:16 | ||
*/ | ||
class PlusOneTest extends UnitSpec { | ||
"PlusOne" should { | ||
|
||
"success1" in { | ||
assert(PlusOne(List(1, 2, 3)) == List(1, 2, 4)) | ||
} | ||
|
||
"success2" in { | ||
assert(PlusOne(List(9, 9, 9)) == List(1, 0, 0, 0)) | ||
} | ||
|
||
} | ||
|
||
"PlusDigit" should { | ||
|
||
"success1" in { | ||
assert(PlusOne(List(1, 2, 3), 2) == List(1, 2, 5)) | ||
} | ||
|
||
"success2" in { | ||
assert(PlusOne(List(9, 9, 9), 2) == List(1, 0, 0, 1)) | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
master 브랜치에 업데이트된 부분이 되돌아가있네요. 수정부탁드려요