대충이라도 하자

Arrays -chapter1 본문

꼬꼬마 개발자 노트/Coding Problems

Arrays -chapter1

Sueeeeee
반응형

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;

}
반응형
Comments