Description
0부터 9까지의 숫자 중 일부가 들어있는 정수 배열 numbers가 매개변수로 주어집니다. numbers에서 찾을 수 없는 0부터 9까지의 숫자를 모두 찾아 더한 수를 return 하도록 solution 함수를 완성해주세요.
제한사항
- 1 ≤ numbers의 길이 ≤ 9
- 0 ≤ numbers의 모든 원소 ≤ 9
- numbers의 모든 원소는 서로 다릅니다.
입출력 예
numbers | result |
[1,2,3,4,6,7,8,0] | 14 |
[5,8,4,0,6,7,9] | 6 |
class Solution {
public int solution(int[] numbers) {
int answer = 45;
for(int num : numbers){
answer-=num;
}
return answer;
}
}
numbers 안에 있는 num을 0~9까지 더한 answer=45에서 하나하나 빼주면
numbers 안에 없는 수를 더한 값만 남는다!
import java.util.Arrays;
class Solution {
public int solution(int[] numbers) {
return 45-Arrays.stream(numbers).sum();
}
}
다른 사람이 stream으로 푼 풀이인데 아직 stream을 공부하지 않아서 잘 모름. 메모해두기!
colon : 처음만날 때 살짝 당황스러울 수도 있어서 개념 메모!
What does a colon mean in Java Programming
According to Oracle docs, When you see the colon(:) read it as "in".
Let's dive deep into more detail:
1. Enhanced for loops (for-each loop)
The enhanced for loop was introduced in Java 5. It was mainly introduced to traverse the collection of elements including arrays. The main advantage of using for each loop is that it makes the code more readable and eliminates the possibility of bugs.
Syntax:
For-each loop syntax consists of datatype with the variable followed by a colon(:) then collection or array.
for(dataType variable : collection | array)
{
// statements
}
Example:
1. Array
Suppose we have givenArray, then we can traverse it using conventional for loop or using new enhanced for loop(for-each loop) as shown below in the image:
public class ColonJava {
public static void main(String args[]) {
// Declaring the Array
int[] givenArray = {11, 7, 45, 19};
// Traversing the array using for-each loop
for (int val : givenArray)
{
System.out.println(val);
}
}
}
Output:
11
7
45
19
One of the downsides of the for-each loop is that you can not traverse elements in reverse order. Also, you can not skip any number of elements.
2. Ternary expression
As the name suggests, the ternary operator takes three terms and its syntax is:
Syntax:
BooleanExpression ? Expression1 : Expression2
The value of the BooleanExpression is determined. If it is true, then the value of the whole expression is Expression1, else, if it is false then the value of the whole expression is Expression2.
Example:
public class ColonJava2 {
public static void main(String args[]) {
boolean val = true;
int x = val ? 9 : 2;
System.out.println(x);
}
}
Output:9
3. Labeling of the loops
In Java, a label is a valid variable name. The label denotes the name of the loop to where the control of execution should jump. It is simple to label a loop, place the label before the loop with a colon at the end as shown below in the example:
public class ColonJava3 {
public static void main(String args[]) {
int i, j=0;
//outer label
outerloop:
for(i=0; i < 3; i++) {
System.out.print(i+" ");
// inner label
innerloop:
for(j=0; j < 4;j++) {
System.out.print(j + " ");
if (j==1)
break innerloop;
}
}
}
}
Output:0 0 1 1 0 1 2 0 1
4. Switch statements after case or default
The simple syntax of the switch statement is given below:
switch(expression){
case a:
// statements
break;
case b:
// statements
break;
default:
//statements
}
You can see that we have used colon(:) after the case and default keywords. You can find in detail the switch statements
.
That's all for today, please mention in the comments in case you have any questions related to what does a colon means in Java programming.
About The Author
Subham Mittal has worked in Oracle for 3 years.Enjoyed this post? Never miss out on future posts by subscribing JavaHungry
stream 설명 완전 굿 블로그 발견
Java 스트림 Stream (1) 총정리
이번 포스트에서는 Java 8의 스트림(Stream)을 살펴봅니다. 총 두 개의 포스트로, 기본적인 내용을 총정리하는 이번 포스트와 좀 더 고급 내용을 다루는 다음 포스트로 나뉘어져 있습니다. Java 스트
futurecreator.github.io
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
'👨🏻💻Computer Science > 💻Algorithm' 카테고리의 다른 글
[프로그래머스] 평균구하기 - Java (0) | 2022.12.15 |
---|---|
[프로그래머스] 음양더하기 - Java (0) | 2022.12.14 |
[프로그래머스] 문자열을 정수로 바꾸기 - Java (2) | 2022.12.12 |
[프로그래머스] 두 정수 사이의 합 - Java (0) | 2022.12.09 |
[프로그래머스] 가운데 글자 가져오기 - Java (0) | 2022.12.08 |