LeetCode热题hot100
1. 两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] + nums[j] == target) {
return new int[]{
i, j};
}
}
}
return new int[0];
}
}
2.两数相加
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null, tail = null;//声明一个链表, 给定头、尾结点
int carry = 0;//进位
while (l1 != null || l2 != null) {
//若没有,默认为0
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
if (head == null) {
//开始时
head = tail = new ListNode(sum % 10);//尾结点指向头结点
} else {