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 b181c2e

Browse files
Update
1 parent d1dd585 commit b181c2e

File tree

4 files changed

+159
-2
lines changed

4 files changed

+159
-2
lines changed

‎README.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
## Algorithm basic List
77

8-
### Basic part
8+
### String basic part
99
* 주어진 문자열을 int 형으로 변환한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/algorithm/basic/StringParseToInt.java)
1010
* 주어진 문자열을 역순으로 출력한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/algorithm/basic/StringReverse.java)
1111
* 주어진 문자열에서 문자열을 구성하고 있는 각각의 문자열들이 고유한지를 판단한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/algorithm/basic/UniqueCharacterInString.java)
@@ -18,6 +18,8 @@
1818
* n개의 서로 다른 원소 중 r개의 원소를 순서없이 선택하는 방법의 수를 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/algorithm/basicMath/BasicCombination.java)
1919
* 주어진 수보다 작은 소수의 개수를 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/algorithm/basicMath/FindPrimeNumTest.java)
2020
* Fibonacci 를 계산하는 함수를 작성한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/algorithm/basicMath/Fibonacci.java)
21+
* 주어진 정수의 각 자리 수의 합을 구한다. [code]
22+
* 사다리를 한 칸 또는 두 칸씩 오를 수 있다고 했을 때 사다리 높이에 따른 사다리 오르기 방법의 경우의 수를 구한다. [code]
2123

2224
### Recursion part
2325
* 주사위로 이동 가능한 경우의 수를 모두 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/algorithm/recursion/Dice.java)
@@ -41,6 +43,7 @@
4143
* ArrayList를 사용하여 Stack을 구현한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/datastructure/stack/MyStackWithArrayListTest.java)
4244
* Stack에 저장된 값들 중 최소값을 반환하는 minStack() 함수를 구현한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/datastructure/stack/MinimumStackTest.java)
4345
* Stack 자료구조를 사용하여 회문을 판별한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/datastructure/stack/IsPalindromeTest.java)
46+
* 괄호의 유효성을 체크한다. [code]
4447

4548
### Queue
4649
* Stack을 사용하여 queue를 stack처럼 만든다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/datastructure/queue/ReverseQueueTest.java)
@@ -84,12 +87,16 @@
8487
### Dynamic Programming
8588
* Jump game [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/algorithm/dp/JumpGame.java)
8689

87-
### Others
90+
### Exercise
8891
* 주어진 배열에서 양 쪽의 합이 동일해지는 index의 값을 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/SearchEquilibrium.java)
8992
* n!의 결과값에서 0의 개수를 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/FactorialZeroCount.java)
9093
* temp 를 사용하지 않고 두 변수를 swap 한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/NoTempSwapTest.java)
9194
* 어느날의 월, 일을 입력받아 요일을 반환하는 함수를 구현한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/CalenderExample.java)
9295
* 주어진 배열에서 합이 최대가 되는 sub array의 합을 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/FindMaxSumInArray.java)
96+
* 주어진 두 수 사이에 존재하는 수 중 제곱수가 되는 것을 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/FindSquareNum.java)
97+
* 주어진 배열로 구성된 링크드 리스트의 길이를 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/LinkedListLength.java)
98+
* 주어진 배열에서 두 자리수만 골라서 합한 값을 return 한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/OnlyTwoDigitSum.java)
99+
* 각각의 주사위들이 모두 같은 면을 보이기 위한 최소 rotate 횟수를 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/SameFaceDice.java)
93100

94101
### Famous Algorithm
95102
* Karp_Rabin_Algorithm [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/famous_algorithm/Karp_Rabin_Algorithm.java)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package algorithm.basic;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class SumEachNum {
9+
10+
/*
11+
TASK
12+
주어진 정수의 각 자리 수의 합을 구한다.
13+
*/
14+
15+
@Test
16+
public void test() {
17+
assertThat(solution(0), is(0));
18+
assertThat(solution(235), is(10));
19+
assertThat(solution(235678), is(31));
20+
assertThat(solution(-1), is(1));
21+
}
22+
23+
public int solution(int value) {
24+
int base = Math.abs(value);
25+
int result = 0;
26+
27+
while (base > 0) {
28+
result += base % 10;
29+
base = base / 10;
30+
}
31+
32+
return result;
33+
}
34+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package algorithm.basicMath;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class Ladder {
9+
10+
/*
11+
TASK
12+
사다리를 한 칸 또는 두 칸씩 오를 수 있다고 했을 때
13+
사다리 높이에 따른 사다리 오르기 방법의 경우의 수를 구한다.
14+
*/
15+
16+
@Test
17+
public void test() {
18+
assertThat(solution(0), is(1));
19+
assertThat(solution(1), is(1));
20+
assertThat(solution(2), is(2));
21+
assertThat(solution(3), is(3));
22+
assertThat(solution(4), is(5));
23+
assertThat(solution(5), is(8));
24+
assertThat(solution(6), is(13));
25+
assertThat(solution(7), is(21));
26+
}
27+
28+
public int solution(int num) {
29+
return getCountOfCase(num, 0, num);
30+
// return getCountOfCaseRec(num);
31+
}
32+
33+
// Use while statement
34+
private int getCountOfCase(int one, int two, int num) {
35+
int result = 0;
36+
while (one + two * 2 == num && one >= 0) {
37+
result += getByRecursion(one + two, two);
38+
one -= 2;
39+
two += 1;
40+
}
41+
return result;
42+
}
43+
44+
// test code를 작성하다보니 이것이 피보나치 규칙을 따른다는 것을 알게 되었다.
45+
// Recursion version
46+
private int getCountOfCaseRec(int num) {
47+
if (num < 2) {
48+
return 1;
49+
}
50+
return getCountOfCaseRec(num - 1) + getCountOfCaseRec(num - 2);
51+
}
52+
53+
public int getByRecursion(int n, int r) {
54+
int cache[][] = new int[n + 1][r + 1];
55+
if (r == 0 || n == r) {
56+
return 1;
57+
}
58+
if (cache[n][r] != 0) return cache[n][r];
59+
return cache[n][r] = getByRecursion(n - 1, r - 1) + getByRecursion(n - 1, r);
60+
}
61+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package datastructure.stack;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Stack;
6+
7+
import static org.hamcrest.CoreMatchers.is;
8+
import static org.junit.Assert.assertThat;
9+
10+
public class CheckBrace {
11+
12+
/*
13+
TASK
14+
괄호의 유효성을 체크한다.
15+
*/
16+
17+
@Test
18+
public void test() {
19+
assertThat(solution("(())"), is(true));
20+
assertThat(solution("()()"), is(true));
21+
assertThat(solution(")(())("), is(false));
22+
assertThat(solution("(())("), is(false));
23+
assertThat(solution(")(())"), is(false));
24+
assertThat(solution("(()"), is(false));
25+
assertThat(solution("())"), is(false));
26+
27+
assertThat(solution("(asdc;aga;ac;dsc;)"), is(true));
28+
assertThat(solution("(aaa(bbb)ccc)"), is(true));
29+
}
30+
31+
public boolean solution(String braces) {
32+
Stack<Character> stack = new Stack<>();
33+
34+
if (braces == null) return true;
35+
char open = "(".charAt(0);
36+
char close = ")".charAt(0);
37+
38+
for (char c : braces.toCharArray()) {
39+
if (c == open) {
40+
stack.push(c);
41+
} else if (c == close){
42+
if (stack.isEmpty()) {
43+
return false;
44+
}
45+
stack.pop();
46+
}
47+
}
48+
49+
if (stack.isEmpty()) {
50+
return true;
51+
}
52+
53+
return false;
54+
}
55+
}

0 commit comments

Comments
(0)

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