AP-1

scoresIncreasing

  • 배열은 length

  • 문자열은 length()

public boolean scoresIncreasing(int[] scores) {
  for (int i = 0; i < scores.length; i++) {
    if (i == 0) continue;
    if (scores[i] < scores[i - 1]) return false;
  }
  return true;
}

scores100

public boolean scores100(int[] scores) {
  for (int i = 1; i < scores.length; i++) {
    if (scores[i] == 100 && scores[i - 1] == 100) return true;
  }
  return false;
}

scoresClump

  • 요구사항을 잘 이해 못함

  • 완전 틀림

public boolean scoresClump(int[] scores) {
  int cnt = 1;
  // int lastest = scores[0];
  
  for (int i = 1; i < scores.length; i++) {
    if ((scores[i] - 1) == scores[i-1]) {
      cnt++;
    }
    if (scores[i] == scores[i-1]) {
      if (i >= 2  && scores[i] - 2 == scores[i-2]) cnt += 2;
      if (i < scores.length -1 && scores[i] + 2 == scores[i+1]) cnt += 2;
    }
  }
  
  if (cnt >= 3) return true;
  return false;
}

Answer

public boolean scoresClump(int[] scores) {
      for (int i = 1; i < scores.length - 1; i++) {
          if (scores[i + 1] - scores[i] + scores[i] - scores[i - 1] <= 2) return true;
      }
      return false;
  }

scoresAverage

public int scoresAverage(int[] scores) {
    int firstAverage = average(scores, 0, scores.length / 2);
    int secondAverage = average(scores, scores.length / 2, scores.length);
    return Math.max(firstAverage, secondAverage);
}
private int average(int[] scores, int start, int end) {
    int sum = 0;
    int count = 0;
    for (int i = start; i < end; i++) {
        count++;
        sum += scores[i];
    }
    return sum / count;
}

wordsCount

public int wordsCount(String[] words, int len) {
     int count = 0;
     for (int i = 0; i < words.length; i++) {
         if (words[i].length() == len) count++;
     }
     return count;
 }

wordsFront

public String[] wordsFront(String[] words, int n) {
     String[] strings = new String[n];
     for (int i = 0; i < strings.length; i++) {
         strings[i] = words[i];
     }
     return strings;
 }

wordsWithoutList

public List wordsWithoutList(String[] words, int len) {
       List<String> list = new ArrayList<String>();
       for (int i = 0; i < words.length; i++) {
           if (words[i].length() != len) {
               list.add(words[i]);
           }
       }
       return list;
   }

hasOne

public boolean hasOne(int n) {
       while (n > 0) {
           if (n % 10 == 1) return true;
           if (n % 10 != 1) n = n / 10;
       }
       return false;
   }

dividesSelf

public boolean dividesSelf(int n) {
       int rightMostDigit = 0;
       int newN = n;
       while(newN>0){
           rightMostDigit = newN%10;
           if(rightMostDigit == 0 || n%rightMostDigit != 0) return false;
           newN = newN/10;
       }
       return true;
   }

copyEvens

public int[] copyEvens(int[] nums, int count) {
       int[] evenNums = new int[count];
       int indexPos = 0;
       for (int i = 0; i < nums.length; i++) {
           if (nums[i] % 2 == 0 && indexPos < count) {
               evenNums[indexPos] = nums[i];
               indexPos++;
           }
       }
       return evenNums;
   }

copyEndy

public int[] copyEndy(int[] nums, int count) {
        int indexPos = 0;
        int[] endyNums = new int [count];
        for(int i = 0; i<nums.length; i++){
            if(isEndy(nums[i]) && indexPos<count){
                endyNums[indexPos] = nums[i];
                indexPos++;
            }
        }
        return endyNums;
    }
    public boolean isEndy(int n) {
        return ((n>=0 && n<= 10) || (n>=90 && n<= 100) );
    }

matchUp

public int matchUp(String[] a, String[] b) {
       int count = 0;
       for (int i = 0; i < a.length; i++) {
           if (!a[i].isEmpty() && !b[i].isEmpty()) {
               if (a[i].charAt(0) == b[i].charAt(0)) {
                   count++;
               }
           }
       }
       return count;
   }

scoreUp

public int scoreUp(String[] key, String[] answers) {
       int score = 0;
       for (int i = 0; i < key.length; i++) {
           if (key[i].equals(answers[i])) {
               score += 4;
           }
           if (!key[i].equals(answers[i]) && (!answers[i].equals("?"))) {
               score += -1;
           }
       }
       return score;
   }

wordsWithout

public String[] wordsWithout(String[] words, String target) {
       String res = "";
       for (int i = 0; i < words.length; i++) {
           if (!words[i].equals(target)) {
               res += words[i] + ":";
           }
       }
       return res.split(":");
   }

scoresSpecial

public int scoresSpecial(int[] a, int[] b) {
    return findLargest(a) + findLargest(b);
}
public int findLargest(int[] nums) {
    int max = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] % 10 == 0 && nums[i] > max) {
            max = nums[i];
        }
    }
    return max;
}

sumHeights

public int sumHeights(int[] heights, int start, int end) {
       int sum = 0;
       for (int i = start; i < end; i++) {
           sum += Math.abs(heights[i] - heights[i + 1]);
       }
       return sum;
   }

sumHeights2

public int sumHeights2(int[] heights, int start, int end) {
        int sum = 0;
        for(int i = start; i<end; i++){
            if(heights[i]< heights[i+1]){
                sum += Math.abs(heights[i] - heights[i+1])*2;
            }else {
                sum += heights[i] - heights[i+1];
            }
        }
        return sum;
    }

bigHeights

public int bigHeights(int[] heights, int start, int end) {
        int count = 0;
        for (int i = start; i < end; i++) {
            if (Math.abs(heights[i] - heights[i + 1]) >= 5) {
                count++;
            }
        }
        return count;
    }

userCompare

public int userCompare(String aName, int aId, String bName, int bId) {
        if (aName.compareTo(bName) < 0) {
            return -1;
        }
        if (aName.compareTo(bName) > 0) {
            return 1;
        }
        if (aName.compareTo(bName) == 0) {
            if (aId < bId) {
                return -1;
            }
            if (aId > bId) {
                return 1;
            }
        }
        return 0;
    }

mergeTwo

public String[] mergeTwo(String[] a, String[] b, int n) {
    String[] result = new String[n];
    int aIndex = 0;
    int bIndex = 0;
    for (int i = 0; i < n; i++) {
        int compare = a[aIndex].compareTo(b[bIndex]);
        if (compare <= 0) {
            result[i] = a[aIndex];
            aIndex++;
            if (compare == 0) {
                bIndex++;
            }
        }
        if (compare > 0) {
            result[i] = b[bIndex];
            bIndex++;
        }
    }
    return result;
}

commonTwo

public int commonTwo(String[] a, String[] b) {
    int count = 0;
    String duplicate = "";
    for (int i = 0; i < a.length; i++) {
        if (!duplicate.equals(a[i])) {
            for (int j = 0; j < b.length; j++) {
                if (a[i].equals(b[j])) {
                    count++;
                    duplicate = a[i];
                    break;
                }
            }
        }
    }
    return count;
}

Last updated