1. 两数之和
class Solution {
public static int[] twoSum(int[] nums,int target){
int n=nums.length;
HashMap<Integer,Integer> map=new HashMap<>();
map.put(nums[0],0);
for(int i=1,j=n-1;i<=n;i++,j--){
int k=target-nums[i];
if(map.containsKey(k)){
return new int[]{map.get(k),i};
}
map.put(nums[i],i);
k=target-nums[j];
if(map.containsKey(k)){
return new int[]{map.get(k),j};
}
map.put(nums[j],j);
}
return new int[]{0};
}
}
2. 两数相加
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode l=new ListNode(0);
ListNode nextNode=l;
int pro=0;
while(l1!=null || l2!=null){
int n1=l1!=null?l1.val:0;
int n2=l2!=null?l2.val:0;
int s=n1+n2+pro;
nextNode.next=new ListNode(s%10);
nextNode=nextNode.next;
pro=s/10;
if(l1!=null){
l1=l1.next;
}
if(l2!=null){
l2=l2.next;
}
}
if(pro>0){
nextNode.next=new ListNode(pro);
}
return l.next;
}
}