Home > Algorithm > 2024 > ๐Ÿ“ฆ[DS,Algorithm] ๋ฐฐ์—ด์—์„œ ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ.

๐Ÿ“ฆ[DS,Algorithm] ๋ฐฐ์—ด์—์„œ ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ.
DataStructure Algorithm

1๏ธโƒฃ ๋ฐฐ์—ด์—์„œ ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ.

Java์—์„œ ๋ฐฐ์—ด์˜ ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฐฐ์—ด์˜ ๊ตฌ์กฐ ํŠน์„ฑ์ƒ ์ง์ ‘์ ์œผ๋กœ ์ œ๊ณต๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์œผ๋กœ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ๋ฐฐ์—ด์—์„œ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ• 2๊ฐ€์ง€.

1๏ธโƒฃ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ์š”์†Œ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ• :)

โ— ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• 1 : ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑํ•˜์—ฌ ๋ณต์‚ฌ.

// ๋ฐฐ์—ด์˜ ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ• - 1
// ๋ฐฉ๋ฒ•1. ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ์š”์†Œ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•
// - ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.
public class Main {

  public static void main(String[] args) {
    int[] array = {10, 20, 30, 40, 50};
    array = removeElement(array, 0);

    for (int value : array) {
      System.out.println(value + " ");
    }

  }

  // ํŠน์ • ๋ฐฐ์—ด์„ ์ง€์šฐ๋Š” ๋ฉ”์†Œ๋“œ
  public static int[] removeElement(int[] array, int index) {
    if (index < 0 || index >= array.length) {
      throw new IndexOutOfBoundsException("Index out of bounds");
    }
    
    // ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์€ ํŠน์ • ์š”์†Œ๋ฅผ ์ง€์šฐ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์—์„œ -1 ํ•œ ํฌ๊ธฐ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    int[] newArray = new int[array.length - 1];

    for (int i = 0, j = 0; i < array.length; i++) {

      if (i != index) {
        newArray[j++] = array[i];
      }
    }
    return newArray;
  }
}
/* 
=== ์ถœ๋ ฅ ===
20 
30 
40 
50 
*/
  • โ€œ๋ฐฉ๋ฒ•1์˜ ์žฅ.๋‹จ์ โ€
    • ์ƒˆ ๋ฐฐ์—ด ์ƒ์„ฑ : ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ฆ๊ฐ€ํ•˜์ง€๋งŒ, ์›๋ž˜ ๋ฐฐ์—ด์„ ์œ ์ง€ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ๊ธฐ์กด ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ๋ฎ์–ด์“ฐ๋Š” ๋ฐฉ๋ฒ• :)

โ— ํŠน์ • ์ธ๋ฑ์Šค ์ดํ›„์˜ ์š”์†Œ๋“ค์„ ์•ž์œผ๋กœ ํ•œ ์นธ์”ฉ ์ด๋™์‹œ์ผœ ๋ฎ์–ด์”๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• 2 : ๊ธฐ์กด ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์š”์†Œ ๋ฎ์–ด์“ฐ๊ธฐ.

// ๋ฐฐ์—ด์˜ ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ• - 2
// ๋ฐฉ๋ฒ•2. ๊ธฐ์กด ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ๋ฎ์–ด์“ฐ๋Š” ๋ฐฉ๋ฒ•.
// - ํŠน์ • ์ธ๋ฑ์Šค ์ดํ›„์˜ ์š”์†Œ๋“ค์„ ์•ž์œผ๋กœ ํ•œ ์นธ์”ฉ ์ด๋™์‹œ์ผœ ๋ฎ์–ด ์”๋‹ˆ๋‹ค.
public class Main {

  public static void main(String[] args) {
    int[] array = {10, 20, 30, 40, 50};
    array = removeElementInPlace(array, 0);

    for (int value : array) {
      System.out.println(value + " ");
    }
  }

  public static int[] removeElementInPlace(int[] array, int index) {
    if (index < 0 || index >= array.length) {
      throw new IndexOutOfBoundsException("Index out of bounds");
    }

    for (int i = index; i < array.length - 1; i++) {
      array[i] = array[i + 1];
    }

    // ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ 0 ๋˜๋Š” ๋‹ค๋ฅธ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ • (์„ ํƒ ์‚ฌํ•ญ)
    array[array.length - 1] = 0;

    return array;
  }
}
/*
=== ์ถœ๋ ฅ ===
20 
30 
40 
50 
0 
*/ 
  • โ€œ๋ฐฉ๋ฒ•2์˜ ์žฅ.๋‹จ์ โ€
    • ๊ธฐ์กด ๋ฐฐ์—ด ์‚ฌ์šฉ : ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.