дарт 刷 лит-код

представлять

Недавно я нашел немного свободного времени, чтобы прочитать руководство на официальном сайте Flutter, а также познакомился с новым языком, поэтому использовал его, чтобы обновить лит-код и ознакомиться с его API.

сумма двух чисел

/**
 * Definition for singly-linked list.
 * class ListNode {
 *   int val;
 *   ListNode? next;
 *   ListNode([this.val = 0, this.next]);
 * }
 */
class Solution {
    
    
  ListNode? addTwoNumbers(ListNode? l1, ListNode? l2) {
    
    
     if(l1==null) return l2;
     if(l2==null) return l1;
     ListNode res = ListNode();
     ListNode res_temp = res;
     int temp = 0;
     while(true){
    
    
         if(l1!=null&&l2!=null){
    
    
             temp = temp + l1.val + l2.val;
             if(temp>=10){
    
    
                 res_temp.val = temp-10;
                 temp = 1;
             }else {
    
    
                 res_temp.val = temp;
                 temp = 0;
             }
             l1 = l1.next;
             l2 = l2.next;
             
         }else if(l1!=null){
    
    
             temp = temp + l1.val;
             if(temp>=10){
    
    
                 res_temp.val = temp-10;
                 temp = 1;
             }else {
    
    
                 res_temp.val = temp;
                 temp = 0;
             }
             l1 = l1.next;
             
         }else if(l2!=null){
    
    
             temp = temp + l2.val;
             if(temp>=10){
    
    
                 res_temp.val = temp-10;
                 temp = 1;
             }else {
    
    
                 res_temp.val = temp;
                 temp = 0;
             }
             l2 = l2.next;
             
         }

         if(l1==null&&l2==null){
    
    
             if(temp>0){
    
    
                 res_temp.next =  ListNode(1);
             }
             return res;
         }else {
    
    
             res_temp.next =  ListNode();
             res_temp = res_temp.next!;
         }
     }

  }
}

самая длинная строка палиндрома

class Solution {
    
    
  String longestPalindrome(String s) {
    
    
    int len = s.length;
  if (len <= 1) return s;
  int begin = 0;
  int end = 0;
  List<List<num>> temp = List.generate(len, (index) => List.filled(len, 0));
  void handle(int i, int j) {
    
    
    if (s[i] == s[j]) {
    
    
      temp[i][j] = 1;
      if (j - i > end - begin) {
    
    
        begin = i;
        end = j;
      }
    }
  }

  for (int i = 0; i < len; i++) {
    
    
    temp[i][i] = 1;
  }
  for (int j = 1; j < len; j++) {
    
    
    for (int i = 0; i < j; i++) {
    
    
      if (j - i == 1) {
    
    
        if (temp[i][j - 1] == 1) {
    
    
          handle(i, j);
        }
      } else {
    
    
        if (temp[i + 1][j - 1] == 1) {
    
    
          handle(i, j);
        }
      }
    }
  }
  return s.substring(begin, end + 1);
  }
}

Guess you like

Origin blog.csdn.net/weixin_45485922/article/details/128473907