介绍
最近抽空闲时间 刷了一遍flutter 官网教程 ,也接触到一门新语言 dart 就用它来刷下leetcode 熟悉它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);
}
}