algorithm-pattern-dart
  • README
  • 入门篇
    • dart 语言入门
    • 算法快速入门
  • 数据结构篇
    • 二叉树
    • 链表
    • 栈和队列
    • 二进制
  • 基础算法篇
    • 二分搜索
    • 排序算法
    • 动态规划
  • 算法思维
    • 递归思维
    • 滑动窗口思想
    • 二叉搜索树
    • 回溯法
Powered by GitBook
On this page
  • 基本语法
  • 1.数组 List
  • 2.字典
  • 3.Set
  • 4.常用的一些算法
  1. 入门篇

dart 语言入门

PreviousREADMENext算法快速入门

Last updated 2 years ago

基本语法

由于leetcode现在还是有些题不支持dart语言(以后肯定会支持的),大家可以在上调试程序.

1.数组 List

数组在算法中使用场景最多的数据结构,所以基本的遍历,和一些常用的函数,变量尽量多掌握一些


  List<int> emptyList = [];
  final list = ['one', 'two', 'three'];
  for (var i = 0; i < list.length; i++) {
    print('${list[i]}');
  }
  for (var item in list) {
    print('$item');
  }
  list.forEach((element) {
    print('$element');
  });
  list.add('four');
  list.insert(1, '1');
  list.removeAt(1);
  list.removeLast();
  
  int length = list.length;
  var last = list.last;
  var first = list.first;
  bool isContains = list.contains('one');
  bool isEmpty = list.isEmpty;
  bool isNotEmpty = list.isNotEmpty;

2.字典

字典在算法中使用的比数组少一些,一般用来存储数据。字典一般掌握初始化,遍历,遍历key,判断是否包含key

  Map map = {};
  map['0'] = 'zero';
  map['1'] = 'one';
  map.forEach((key, value) {
    print('key = $key : value = $value');
  });
  bool isEmpty = map.isEmpty;
  List keys = map.keys.toList();
  List value = map.values.toList();
  bool isContainsKey = map.containsKey('1');
  map.remove('1');

3.Set

set在算法中用到最少。一般用于去重,掌握初始化和判断包含元素既可。

  Set set = {'one'};
  set.add('two');
  set.remove('one');
  bool isContains = set.contains('one');

4.常用的一些算法

  //字符串转浮点数整数
  double oneDouble = double.parse('1.1');
  int twoInt = int.parse('2');

  //浮点数整数转字符串
  String oneStr =   oneDouble.toString();
  String twoStr =   twoInt.toString();

  //获取最大值 最小值
  int maxNum = max(1, 2);
  int minNum = min(1, 2);
  //获取数组的最大值
  int max = [1,2,3,4,5].reduce(max);
  
  //求绝对值
  int num = (-1).abs();

  //取余数
  print(oneDouble % twoInt);
  //除法
  print(oneDouble / twoInt);
  //整除
  print(oneDouble ~/ twoInt);
}
Dart语言官网
dartpad