Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
  1. relative-sort-array (Solved)


    Code Block
    languagejava
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
            List<Integer> result = new ArrayList<>();
            List<Integer> remains = new ArrayList<>();
    
            for (final int pivot : arr2) {
                for (final int i : arr1) {
                    if (pivot == i) {
                        result.add(i);
                    }
                }
            }
    
            for (final int i : arr1) {
                if (!result.contains(i)) {
                    remains.add(i);
                }
            }
            remains.sort(Comparator.naturalOrder());
    
            result.addAll(remains);
    
            return result.stream()
                    .mapToInt(x -> x)
                    .toArray();
    
        }
  2. Largest Number (Fail)


    Code Block
    languagejava
    public String largestNumber(int[] nums) {
            
            List<String> results = new ArrayList<>();
    
            for (int i = 0; i < nums.length; i++) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(nums[i]);
                
                for (int j = 0; j < nums.length; j++) {
                    if (i != j) {
                        stringBuilder.append(nums[j]);
                    }
                }
                results.add(stringBuilder.toString());
            }
            
            results.sort(Comparator.reverseOrder());
    
            return results.get(0);
        }
    Code Block
    languagejava
    title회고
    public String largestNumber(int[] nums) {
    
            final int sum = Arrays.stream(nums)
                    .sum();
            if (sum == 0) {
                return "0";
            }
    
            for (int i = 0; i < nums.length; i++) {
                for (int j = 1; j < nums.length; j++) {
                    long basicNumber = Long.parseLong(nums[j - 1] + String.valueOf(nums[j]));
                    long comparedNumber = Long.parseLong(nums[j] + String.valueOf(nums[j - 1]));
    
                    if (basicNumber < comparedNumber) {
                        int temp = nums[j - 1];
                        nums[j - 1] = nums[j];
                        nums[j] = temp;
                    }
                }
            }
            return Arrays.stream(nums)
                    .boxed()
                    .map(String::valueOf)
                    .collect(Collectors.joining());
        }
  3. find-and-replace-in-string


    Code Block
    languagejava
    public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
    
            String[] replacedWord = new String[indexes.length];
    
            for (int i = 0; i < indexes.length; i++) {
                replacedWord[i] = String.valueOf(S.charAt(indexes[i]));
            }
    
            for (int i = 0; i < sources.length; i++) {
                if (sources[i].contains(replacedWord[i]) && S.contains(sources[i])) {
                    S = S.replace(sources[i], targets[i]);
                }
            }
    
            return S;
        }
    Code Block
    languagejava
    		for (int i = 0; i < indexes.length; ++i) {
                int ix = indexes[i];
                if (S.substring(ix, ix + sources[i].length()).equals(sources[i]))
                    match[ix] = i;
            }