일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 | 31 |
- Lesson3
- 백준알고리즘
- 파이도 환불
- FK 설정
- 프로그래머스
- 레노보노트북
- 벤쿠버집구하기
- 1463번
- 벤쿠버렌트
- Java
- 부산입국
- 자바
- 언마운트
- 리눅스
- 설탕문제
- BC렌트
- 데이터의 무결성
- FLEX5
- 벤쿠버 렌트
- Lesson2
- Linux
- 캐나다워홀
- QA엔지니어
- IntelliJ
- codility
- FIDO 환불
- 외래키설정
- 엔테크서비스
- binaray_gap
- database연결
- Today
- Total
대충이라도 하자
Arrays -chapter1 본문
An Array is a collection of items. The items could be integers, strings, DVDs, games, books—anything really. The items are stored in neighboring (contiguous) memory locations. Because they're stored together, checking through the entire collection of items is straightforward.
In Java, we use the following code to create an Array to hold up to 15 DVDs. Note that we've also included a simple definition of a DVD for clarity.
/ The actual code for creating an Array to hold DVD's.
DVD[] dvdCollection = new DVD[15];
// A simple definition for a DVD.
public class DVD {
public String name;
public int releaseYear;
public String director;
public DVD(String name, int releaseYear, String director) {
this.name = name;
this.releaseYear = releaseYear;
this.director = director;
}
public String toString() {
return this.name + ", directed by " + this.director + ", released in " + this.releaseYear;
}
}
If you wanna store more than 15 dvds, you should make another array. Then, why don't we just make a array which can store 100,000 in the first place?
Because we start off the array with 100,000 spaces, the computer will reserve memory to hold 1000000 DVDs. That memory can't be used for anything else in the meantime.
DVD incrediblesDVD = new DVD("The Incredibles", 2004, "Brad Bird");
DVD findingDoryDVD = new DVD("Finding Dory", 2016, "Andrew Stanton");
DVD lionKingDVD = new DVD("The Lion King", 2019, "Jon Favreau");
// Put "The Incredibles" into the 4th place: index 3.
dvdCollection[3] = incrediblesDVD;
// Put "Finding Dory" into the 10th place: index 9.
dvdCollection[9] = findingDoryDVD;
Java always initializes empty Array slots to null if the Array contains objects, or to default values if it contains primitive types. For example, the array int [] would contain the default value of 0 for each element, float[] would contain default values of 0.0, and bool[] would contain default values of false.
Array Capacity Vs Array Length
The Array's capacity must be decided when the Array is created. The capacity cannot be changed later.
We can figure out the capacity using .length, however, the real length could be different with capacity.
Length indicates how many items are in the array right now.
Max Consecutive Ones
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int len = nums.length;
int max =0;
int count = 0;
for(int i =0; i<len;i++){
if(nums[i] == 1){
count++;
}else {
max = Math.max(count, max);
count = 0;
}
}
max = Math.max(count, max);
return max;
}
}
Find Numbers with Even Number of Digits ( 96.74%)
class Solution {
public int findNumbers(int[] nums) {
int count = 0;
for(int num :nums){
String change = Integer.toString(num);
if(change.length()%2 ==0){
count++;
}
}
return count;
}
}
Squares of a Sorted Array (33.37%)
import java.util.*;
class Solution {
public int[] sortedSquares(int[] nums) {
//제곱을 하고 Arrays.sort를 할 것인가
int len = nums.length;
for(int i =0; i<len;i++) {
nums[i] = nums[i] * nums[i];
}
Arrays.sort(nums);
return nums;
}
}
내가 한 방법이 단순 구현이라면, 더 빠르게 해결할 방법으로 투포인터를 사용한 경우가 많았다. (33.37%)
int [] sortedSquares = new int[nums.length];
int low =0;
int high=nums.length -1;
int index=nums.length-1;
while(low <= high){
if(Math.abs(nums[low]) <= Math.abs(nums[high])){
sortedSquares[index]=nums[high] * nums[high];
high--;
}
else {
sortedSquares[index]=nums[low] * nums[low];
low++;
}
index--;
}
return sortedSquares;
}
'꼬꼬마 개발자 노트 > Coding Problems' 카테고리의 다른 글
Arrays -chapter3 (0) | 2022.01.12 |
---|---|
Arrays- Chapter2 (0) | 2022.01.10 |
프로그래머스-[1차] 추석 트래픽 (0) | 2022.01.06 |
프로그래머스 - 거리두기 확인하기 (0) | 2022.01.05 |
프로그래머스-멀쩡한 사각형 (0) | 2022.01.04 |