一.链表相加
考点:leetcode原题:
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
注意点:1.只能是个位,注意代码对取个位和进位的处理
2.单链表结点概念掌握.next指向下一个结点数
/*** Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public
class Solution { publicListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1==null) returnl2; if(l2==null) returnl1; ListNode head =new
ListNode(0); ListNode p = head; inttemp =
0; while(l1!=null||l2!=null||temp!=0){ if(l1!=null){ temp+=l1.val; l1=l1.next; } if(l2!=null){ temp+=l2.val; l2=l2.next; } p.next=newListNode(temp%10); p=p.next; temp/=10; } returnhead.next; }}二.两个有序数组归并
以2个排序好的数组a和数组b为例,i=0和j=0分别为M和N的起始下标。
1.a第一个元素与b第一个元素比较,如果a的第一个元素小于b的第一个元素值,此时新数组am的第一个元素的值为a[i],下标为[i+j]
2.此时a的下标i+1,与b的第一个元素继续比较,如果a的第二元素大于b的第一元素值,此时新数组am的第二个元素的值为b[j],下标为[i+j]
public class Test {
public static int[] arrayMerge(int[] a, int[] b) {
int[] am = new int[a.length + b.length];
int ai = 0;
int bj = 0;
while ((ai<a.length) && (bj<b.length)) {
if(a[ai] < b[bj]) {
am[ai + bj] = a[ai];
ai++;
} else {
am[ai + bj] = b[bj];
bj++;
}
}
while (ai < a.length) {
am[ai + bj] = a[ai];
ai++;
}
while (bj < b.length) {
am[ai + bj] = b[bj];
bj++;
}
return am;
}
public static void main(String[] args) {
int[] a = {0,1,2,3,4,5,6,6};
int[] b = {1,2,3,4,5,6,7,8,9};
int[] am = new int[a.length + b.length];
am = arrayMerge(a, b);
for (int i=0;i < am.length;i++) {
System.out.print(am[i] + " ");
}
}
}
三 集合类区别1:集合
Collection(单列集合)
List(有序,可重复)
ArrayList
底层数据结构是数组,查询快,增删慢
线程不安全,效率高
Vector
底层数据结构是数组,查询快,增删慢
线程安全,效率低
LinkedList
底层数据结构是链表,查询慢,增删快
线程不安全,效率高
Set(无序,唯一)
HashSet
底层数据结构是哈希表。
哈希表依赖两个方法:hashCode()和equals()
执行顺序:
首先判断hashCode()值是否相同
是:继续执行equals(),看其返回值
是true:说明元素重复,不添加
是false:就直接添加到集合
否:就直接添加到集合
最终:
自动生成hashCode()和equals()即可
LinkedHashSet
底层数据结构由链表和哈希表组成。
由链表保证元素有序。
由哈希表保证元素唯一。
TreeSet
底层数据结构是红黑树。(是一种自平衡的二叉树)
如何保证元素唯一性呢?
根据比较的返回值是否是0来决定
如何保证元素的排序呢?
两种方式
自然排序(元素具备比较性)
让元素所属的类实现Comparable接口
比较器排序(集合具备比较性)
让集合接收一个Comparator的实现类对象
Map(双列集合)
A:Map集合的数据结构仅仅针对键有效,与值无关。
B:存储的是键值对形式的元素,键唯一,值可重复。
HashMap
底层数据结构是哈希表。线程不安全,效率高
哈希表依赖两个方法:hashCode()和equals()
执行顺序:
首先判断hashCode()值是否相同
是:继续执行equals(),看其返回值
是true:说明元素重复,不添加
是false:就直接添加到集合
否:就直接添加到集合
最终:
自动生成hashCode()和equals()即可
LinkedHashMap
底层数据结构由链表和哈希表组成。
由链表保证元素有序。
由哈希表保证元素唯一。
Hashtable
底层数据结构是哈希表。线程安全,效率低
哈希表依赖两个方法:hashCode()和equals()
执行顺序:
首先判断hashCode()值是否相同
是:继续执行equals(),看其返回值
是true:说明元素重复,不添加
是false:就直接添加到集合
否:就直接添加到集合
最终:
自动生成hashCode()和equals()即可
TreeMap
底层数据结构是红黑树。(是一种自平衡的二叉树)
如何保证元素唯一性呢?
根据比较的返回值是否是0来决定
如何保证元素的排序呢?
两种方式
自然排序(元素具备比较性)
让元素所属的类实现Comparable接口
比较器排序(集合具备比较性)
让集合接收一个Comparator的实现类对象
2.关于集合选取原则
是否是键值对象形式:
是:Map
键是否需要排序:
是:TreeMap
否:HashMap
不知道,就使用HashMap。
否:Collection
元素是否唯一:
是:Set
元素是否需要排序:
是:TreeSet
否:HashSet
不知道,就使用HashSet
否:List
要安全吗:
是:Vector
否:ArrayList或者LinkedList
增删多:LinkedList
查询多:ArrayList
不知道,就使用ArrayList
不知道,就使用ArrayList
来源:CSDN
作者:吉吉桑
链接:https://blog.csdn.net/knife430/article/details/77509439