歪比歪比
两数之和
复杂度
https://zhuanlan.zhihu.com/p/52896960
题
class Solution {
public int[] twoSum(int[] nums, int target) {
}
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
for(int i = 0; i< nums.length; i++){
for(int j = i+1; j< nums.length; j++){
if (nums[i]+nums[j] == target) {
arr[0] = i;
arr[1] = j;
}
}
}return arr;
}
}
两数相加
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.
输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:[8,9,9,9,0,0,0,1]
链表
https://blog.csdn.net/tec_1535/article/details/82219339
https://blog.csdn.net/lilong117194/article/details/83759106
链表def(java)
class ListtNode {
int val;
ListNode next;
ListNode(){ }
ListNode(int val){
this.val = val;
}
ListNode(Int val, ListNode Next){
this.next = next;
this.val = val;
}s
}
链表遍历
//
class Bianli(ListNode l,){
ListNode head = l;
while(ListNode != null ){
head = head.next;
}
}
题
链表相加
/**
ex:
1->2->3
4->5->6
=> 321 +654 = 975
**/
直接对应相加
public class twoAdd(ListNode l1, ListNode l2){
ListNode head1 = l1;
ListNode head2 = l2;
while(ListNode != null ){
if(head2 != null ){
head1.val += head2.val;
head2 = head2.next;
}
head1 = head1.next;
}
merge(l1);
return l1;
}
进位问题
public void merge(ListNode head){
while(head != null ){
if(head.val>= 10){
head.val = head.val % 10;
head.next.val+= 1;
}
}
}
l1 l2长度不同
l1 >= l2 (上面代码不需要改)
l2 >= l1 (即 l1遍历完了之后l2 没有遍历完)
public class twoAdd(ListNode l1, ListNode l2){
ListNode head1 = l1;
ListNode head2 = l2;
while(ListNode != null ){
if(head2 != null ){
head1.val += head2.val;
head2 = head2.next;
}
// 添加以下代码
if(head1.next == null && head2.next != null){
head1.next = head2;
break;
}
head1 = head1.next;
}
merge(l1);
return l1;
}
末位进位情况
[1,2,5]
[1,2,6]
public class merge(ListNode head){
while(head != null ){
if (head.val >= 10){
head.val = head.val %10;
if(head.next == null){
head.next = new ListNode(0)
}
head.next.val +=1;
}
}
}
无重复字符的最长子串
示例
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
输入: s = ""
输出: 0
hashmap_hashset
https://blog.csdn.net/woshimaxiao1/article/details/83661464
hashmap 唯一键
hashset 唯一值
add,remove,contains和size