プログラマーLv.1 (3)
220103 ~ 220106
最初のインデックスエラー
答えとtempの配列長設定にエラーが発生しました.outindexエラーが発生しましたか? 配列に[i]をインデックスとして追加する場合は、予め長さを設定する必要があると判断して、それをそのように設定すると、これは低いエラーを招くのではないでしょうか.
重複文を使用して配列をクリップおよびソートする全体的な流れは正しいが、配列を正しく適用できなかったインデックスはn番目より小さく、これは失敗の原因である.
他の人の解答を見て、最新の問題ライブラリを書いた人もいましたが、私は基本的な勉強が足りないので、まずアルゴリズムで解答しましょう^ㅠ
(int i:numbers)順序で返される構文を効率的に作成できます...
そして、一つの方法は、答えを合わせなくても、10の数字の総和を45から外すことです.
世界には賢い人が本当にたくさんいます.私はまだ長い道のりがあります.
:各文字が1つずつ分解されているので、数字も「2」という文字形式です.これが数字かどうかを比較するには、数字に変換する必要がありますが、どのような方法/機能を使用すればいいか分かりません.
(2)文字が混ざっているとNumberFormatExceptionが発生するため、このエラーを使用して例外を除外します.
しかし、このような異常処理には多くのリソースが必要なのか、実際の異常がある場合にのみ適用されるのか...まずアルゴリズムで解き,次に例外処理で解く.
文字列では基本データ型の理解や関連メソッドを忘れているため、使用能力が非常に不足しています.行くべき道は9万里ある.
k個の数字を検索
イニシャルコード
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = {};
return answer;
}
}
私が書いたコード
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
int[] temp = new int[array.length];
for (int i = 0; i < commands.length; i++) {
temp = Arrays.copyOfRange(array, commands[i][0], commands[i][1]+1);
Arrays.sort(temp);
answer[i] = temp[commands[i][2]];
/*answer.add(temp[commands[i][2]]); arrayList 전용 메서드.*/
}
return answer;
}
}
さまよう部分
正しいコード
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
int[] temp = new int[array.length];
for (int i = 0; i < commands.length; i++) {
temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
/* 배열에서의 인덱스 =/= 현실 세상의 몇 번째라는 이름 */
Arrays.sort(temp);
answer[i] = temp[commands[i][2]-1];
}
return answer;
}
}
こんどの問題の教訓
重複文を使用して配列をクリップおよびソートする全体的な流れは正しいが、配列を正しく適用できなかったインデックスはn番目より小さく、これは失敗の原因である.
平均値を求める
イニシャルコード
class Solution {
public double solution(int[] arr) {
double answer = 0;
return answer;
}
}
私が作成したコード=正しいコード
class Solution {
public double solution(int[] arr) {
double answer = 0;
int temp = 0;
for (int i = 0; i < arr.length; i++) {
temp += arr[i];
}
answer = temp / (double) arr.length;
return answer;
}
}
この問題は基礎的な部分なので、特に苦がなければ解けます.他の人の解答を見て、最新の問題ライブラリを書いた人もいましたが、私は基本的な勉強が足りないので、まずアルゴリズムで解答しましょう^ㅠ
欠落した数値を追加
イニシャルコード
class Solution {
public int solution(int[] numbers) {
int answer = 0;
return answer;
}
}
私が書いたコード
class Solution {
public int solution(int[] numbers) {
int answer = 0;
int[] arr = {0,1,2,3,4,5,6,7,8,9};
boolean temp;
for (int i = 0; i < 10; i++) {
temp = true;
for (int j = 0; j < numbers.length; j++) {
if (arr[i] == numbers[j]) {
temp = false;
break;
}
}
if (temp == true) {
answer += arr[i];
}
}
return answer;
}
}
arrの中の数字を1つずつ数字の中の数字と比較し、同じならfalse処理、違うならtrue処理-->true値のない数字であることを確認したら、その数字を解答に追加します.他人の解答
class Solution {
public int solution(int[] numbers) {
int sum = 45;
for (int i : numbers) {
sum -= i;
}
return sum;
}
}
私のように2つの複文を繰り返し使うのではなく、(int i:numbers)順序で返される構文を効率的に作成できます...
そして、一つの方法は、答えを合わせなくても、10の数字の総和を45から外すことです.
世界には賢い人が本当にたくさんいます.私はまだ長い道のりがあります.
文字列ベース
イニシャルコード
class Solution {
public boolean solution(String s) {
boolean answer = true;
return answer;
}
}
私が書いたコード
class Solution {
public boolean solution(String s) {
boolean answer = true;
String[] str = s.split("(?!^)");
if (str.length == 4 || str.length == 6) {
for (String n : str) {
if (n instanceof Integer ) {
break;
}
}
}
return answer;
}
}
私のコードの問題:各文字が1つずつ分解されているので、数字も「2」という文字形式です.これが数字かどうかを比較するには、数字に変換する必要がありますが、どのような方法/機能を使用すればいいか分かりません.
解答コード1
class Solution {
public boolean solution(String s) {
if(s.length() == 4 || s.length() == 6){
try{
int x = Integer.parseInt(s); /*(1)*/
return true;
} catch(NumberFormatException e){ /*(2)*/
return false;
}
}
else return false;
}
}
(1)文字列をIntegerに1つずつ変換するParseInt()を用いてxという変数に割り当てる.sが数字ならここは本当に見分けがつく(2)文字が混ざっているとNumberFormatExceptionが発生するため、このエラーを使用して例外を除外します.
しかし、このような異常処理には多くのリソースが必要なのか、実際の異常がある場合にのみ適用されるのか...まずアルゴリズムで解き,次に例外処理で解く.
解答コード2
class Solution {
public boolean solution(String s) {
if(s.length() != 4 && s.length() != 6) {
return false;
}
for (int i=0;i<s.length();i++) {
if (s.charAt(i) < '0' || s.charAt(i) > '9') { /*(1)*/
return false;
}
}
return true;
}
}
(1)s.charAt(i):選択した文字をインデックスとしてcharタイプに変換します.このように変換された値を「0」、「9」のように「数字」と比較することはできますか?初めて知りました.あるいは忘れた...こんどの問題の教訓
文字列では基本データ型の理解や関連メソッドを忘れているため、使用能力が非常に不足しています.行くべき道は9万里ある.
Reference
この問題について(プログラマーLv.1 (3)), 我々は、より多くの情報をここで見つけました https://velog.io/@nn1co1/프로그래머스-Lv.1-3テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol