Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 유닉스의탄생
- GlobalInterprintLock
- flake8
- python
- printer_helper
- vscode
- Lint
- opensouce
- codewars
- organizeImports
- ProxyServer
- springboot
- conf
- restfulapi
- typevar
- goalng
- pyenv
- pep8
- Algorithm
- codewar
- 글쓰기가필요하지않은인생은없다
- maxlinelength
- Golang
- 독후감
- 오큘러스퀘스트2
- 코로나백신
- 규칙없음
- 조엘온소프트웨어
- loadimpact
- httppretty
Archives
- Today
- Total
일상적 이야기들.
codewar: Fun with lists:indexOf 본문
문제
- https://www.codewars.com/kata/581c6b075cfa83852700021f
문제풀이
- 주어진 node를 순회하면서, 찾고자하는 value의 위치를 찾는 문제입니다.
- while을 통해서 head가 null 인지를 판별하고, null 이 아니라면 순회를 시작하게 됩니다.
- 해당 index의 data 와 같다면, 해당 index 값을 전달 해주고
- 그렇지 않다면, head를 다음 node로 변경을 합니다.
- 끝까지 순회를 ( head가 null 로 설정이 됨 ) 하였다면, 찾고자하는 값이 없으므로 -1 를 return 시켜줍니다.
class Solution {
static int indexOf(Node head, Object value) {
int idx = -1;
while( null != head ) {
idx++;
if(head.data == value) {
break;
} else {
head = head.next;
}
}
if( null == head ) {
idx = -1;
}
return idx;
}
}
'프로그래밍 > 알고리즘 문제풀이' 카테고리의 다른 글
codewar: Where is THB? (0) | 2019.07.02 |
---|---|
codewar: Remove First and Last Character (0) | 2019.06.25 |
codewar: int32 to IPv4 (0) | 2019.06.21 |
codewar: Errors: histogram (0) | 2019.06.21 |
codewar: Scalling Squared Strings. (0) | 2019.06.18 |
Comments