Решение двух сумм в Dart — LeetCode


Решение двух сумм в Дарте

LeetCode

Первое решение

class Solution {
  // BRUTE FORCE SOLUTION - SIMPLE
  // Runtime of this solution is 362ms
  List<int> twoSum(List<int> numbers, int target) {
    // it indicates the whole length of the list
    int n = numbers.length;

// for loop for tracking the first value
    for (var i = 0; i < n - 1; i++) {
      // for loop for keep tracking the second value inside the list
      for (var j = i + 1; j < n; j++) {
        // condition that dictates the first of any value inside the list and second inside the
        // list should be equal to target
        if (numbers[i] + numbers[j] == target) {
          // then we return the list of first value and second
          return [i, j];
        }
      }
    }

    return [];
  }
}
Войдите в полноэкранный режим Выйти из полноэкранного режима

Второе решение

Использование карты для отслеживания значений списка
К сожалению, в LeetCode HashMap не работает, но он прекрасно работает вне LeetCode.
Поэтому я использую Map вместо Hash Map


class Solution {
  // In LeetCode the HashMap is not fully implemented
  // Runtime 503
  List<int> twoSum(List<int> nums, int target) {


// Map to keep an eye on the close range, simply correlation
    final Map<int, int> correspondence = Map<int, int>();

    // loop through the entire list values
    for (var i = 0; i < nums.length; i++) {
      // saving value inside a variable
      final int value = nums[i];

      // we are getting key in a very tricky way like target value which
      // we will enter and than we will subtract the single value
      //that we got from looping from the list.
      //
      final int key = target - value;
      if (correspondence.containsKey(key)) {
        // than we will return if int of the map and the second int
        // which shows the position in a list which two value will result the target value
        return [correspondence[key]!, i];
      }
      // here we defining that our key will i the digit inside of our list
      // if we don't do  this than it will return the value of the list which is inside the list
      correspondence[value] = i;

      // Remember = correspondence[key] is Our key , correspondence[value] is Our Value

    }
    return [];
  }
}

Вход в полноэкранный режим Выйти из полноэкранного режима

Третье решение

Это работает как шарм в терминале, потому что dart не отслеживает номер индекса в списке. Я создал расширение для работы со списком, но для leetCode не будем усложнять.

class Solution
// Working perfect in Terminal
// but runtime error inclusive range error

  List<int> twoSum(List<int> nums, int target) {
    List<int> result = <int>[];
    for (var i = 0; i < nums.length; i++) {
      int complement = target - nums[i];
      var index = nums.indexOf(complement, i + 1);
      if (nums[index] + nums[i] == target) {
        return result = [i, index];
      }
      break;
    }
    return result;
  }
}

Войдите в полноэкранный режим Выход из полноэкранного режима
// to keep track on the index
extension SubScript<T> on List<T>{
T? operator [](int index)=> length > index ? elementAt(index) : null;
}

Войти в полноэкранный режим Выход из полноэкранного режима

Оцените статью
devanswers.ru
Добавить комментарий