1.两个链表相加
2.判断树是否为镜像二叉树
3.从一个无序数组中找出倒数第k大的数
4.合并两个无序数组为一个有序数组
5.在一个数组中,找到最长子序列
6.爬楼梯问题,打印所有结果
1.两个链表相加
3->1->5->null 5->9->2->null 返回8->0->8->null 513+295=808
public ListNode addList(ListNode l1,ListNode l2){
ListNode dummy = new ListNode(-1);
ListNode p1=l1;
ListNode p2=l2;
ListNode p =dummy;
int flag = 0;
while(p1!=null||p2!=null){
int temp = flag;
if(p1!=null){
temp+=p1.val;
p1=p1.next;
}
if(p2!=null){
temp+=p2.val;
p2=p2.next;
}
if(temp>9){
temp-=10;
flag=1;
}else{
flag=0;
}
p.next=new ListNode(temp);
p=p.next;
}
if(flag==1)
p.next=new ListNode(1);
return dummy.next;
}
6->1->7 2->9->5 返回9-1-2 617+295=912
public ListNode addList(ListNode l1,ListNode l2){
ListNode l11 = reverse(l1);
ListNode l22 = reverse(l2);
ListNode dummy = new ListNode(-1);
ListNode p1=l11;
ListNode p2=l22;
ListNode p=dummy;
int flag = 0;
while(p1!=null||p2!=null){
int temp=flag;
if(p1!=null){
temp+=p1.val;
p1=p1.next;
}
if(p2!=null){
temp+=p2.val;
p2=p2.next;
}
if(temp>9){
temp-=10;
flag=1;
}else{
flag=0;
}
p.next=new ListNode(temp);
p=p.next;
}
if(flag==1)
p.next=new ListNode(1);
return reverse(dummy.next);
}
public ListNode reverse(ListNode haed){
ListNode cur=head,pre=null;
while(cur!=null){
ListNode temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}
return pre;
}
2.判断树是否为镜像二叉树
public boolean isSymmetric(TreeNode root) {
return root==null?true:recur(root.left,root.right);
}
boolean recur(TreeNode L,TreeNode R){
if(L==null&&R==null) return true;
if(L==null||R==null||L.val!=R.val) return false;
return recur(L.left,R.right)&&recur(L.right,R.left);
}
public boolean isSymmetric(TreeNode root) {
//使用队列
if(root==null) return true;
Queue<TreeNode> q= new LinkedList<>(){{add(root.left);add(root.right);}};
while(!q.isEmpty()){
TreeNode l = q.poll();
TreeNode r = q.poll();
if(l==null&&r==null) continue;
if(l==null||r==null||l.val!=r.val) return false;
q.offer(l.left);
q.offer(r.right);
q.offer(l.right);
q.offer(r.left);
}
return true;
}
3.从一个无序数组中找出倒数第k大的数
3.1最小的k个数
(1)快排
public int[] getLeastNumbers(int[] arr, int k) {
if (k >= arr.length) return arr;
quick_sort(arr,0,arr.length-1,k);
return Arrays.copyOf(arr, k);
}
public static void quick_sort(int[] q,int l,int r,int k){
//base case
if(l>=r){return;}
int mid=q[l],i=l-1,j=r+1;
while(i<j){
do i++;while(q[i]<mid);
do j--;while(q[j]>mid);
if(i<j){swap(q,i,j);}
}
if(k-1>j){
quick_sort(q,j+1,r,k);
quick_sort(q,l,j,k);
}else{
quick_sort(q,l,j,k);
}
}
public static void swap(int[] q,int i,int j){
int temp=q[i];
q[i]=q[j];
q[j]=temp;
}
(2)堆排序
// 保持堆的大小为K,然后遍历数组中的数字,遍历的时候做如下判断:
// 1. 若目前堆的大小小于K,将当前数字放入堆中。
// 2. 否则判断当前数字与大根堆堆顶元素的大小关系,如果当前数字比大根堆堆顶还大,这个数就直接跳过;
// 反之如果当前数字比大根堆堆顶小,先poll掉堆顶,再将该数字放入堆中。
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if (k == 0 || arr.length == 0) {
return new int[0];
}
// 默认是小根堆,实现大根堆需要重写一下比较器。
Queue<Integer> pq = new PriorityQueue<>((v1, v2) -> v2 - v1);
for (int num: arr) {
if (pq.size() < k) {
pq.offer(num);
} else if (num < pq.peek()) {
pq.poll();
pq.offer(num);
}
}
// 返回堆中的元素
int[] res = new int[pq.size()];
int idx = 0;
for(int num: pq) {
res[idx++] = num;
}
return res;
}
}
手动建堆--大根堆
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if (k == 0 || arr.length == 0) {
return new int[0];
}
int [] res =new int[k];
System.arraycopy(arr, 0, res, 0, k);
buildHeap(res);
for(int i=k;i<arr.length;i++){
if(arr[i]<res[0])
res[0]=arr[i];
adjust(res,0);
}
return res;
}
public void buildHeap(int[] arr){
for(int i = arr.length / 2 - 1; i >= 0; i--){
adjust(arr, i);
}
}
public void adjust(int[] arr, int i){
int maxIndex = i;
int len = arr.length;
if(2*i+1 < len && arr[2*i+1] > arr[maxIndex]) maxIndex = 2*i+1;
if(2*i+2 < len && arr[2*i+2] > arr[maxIndex]) maxIndex = 2*i+2;
if(maxIndex != i){
swap(arr, maxIndex, i);
adjust(arr, maxIndex);
}
}
public void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
(3)二叉搜索树
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if (k == 0 || arr.length == 0) {
return new int[0];
}
// TreeMap的key是数字, value是该数字的个数。
// cnt表示当前map总共存了多少个数字。
TreeMap<Integer, Integer> map = new TreeMap<>();
int cnt = 0;
for (int num: arr) {
// 1. 遍历数组,若当前map中的数字个数小于k,则map中当前数字对应个数+1
if (cnt < k) {
map.put(num, map.getOrDefault(num, 0) + 1);
cnt++;
continue;
}
// 2. 否则,取出map中最大的Key(即最大的数字), 判断当前数字与map中最大数字的大小关系:
// 若当前数字比map中最大的数字还大,就直接忽略;
// 若当前数字比map中最大的数字小,则将当前数字加入map中,并将map中的最大数字的个数-1。
Map.Entry<Integer, Integer> entry = map.lastEntry();
if (entry.getKey() > num) {
map.put(num, map.getOrDefault(num, 0) + 1);
if (entry.getValue() == 1) {
map.pollLastEntry();
} else {
map.put(entry.getKey(), entry.getValue() - 1);
}
}
}
// 最后返回map中的元素
int[] res = new int[k];
int idx = 0;
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
int freq = entry.getValue();
while (freq-- > 0) {
res[idx++] = entry.getKey();
}
}
return res;
}
}
3.2 数组中第k个最大元素
class Solution {
public int findKthLargest(int[] nums, int k) {
if (k > nums.length) return -1;
quick_sort(nums,0,nums.length-1,nums.length-k+1);
return nums[nums.length-k];
}
public static void quick_sort(int[] q,int l,int r,int k){
//base case
if(l>=r){return;}
int mid=q[l],i=l-1,j=r+1;
while(i<j){
do i++;while(q[i]<mid);
do j--;while(q[j]>mid);
if(i<j){swap(q,i,j);}
}
if(k-1>j){
quick_sort(q,j+1,r,k);
quick_sort(q,l,j,k);
}else{
quick_sort(q,l,j,k);
}
}
public static void swap(int[] q,int i,int j){
int temp=q[i];
q[i]=q[j];
q[j]=temp;
}
}
3.3 堆
建堆----大根堆
public void buildHeap(int[] arr){
for(int i = arr.length / 2 - 1; i >= 0; i--){
adjust(arr, i);
}
}
public void adjust(int[] arr, int i){
int maxIndex = i;
int len = arr.length;
if(2*i+1 < len && arr[2*i+1] > arr[maxIndex]) maxIndex = 2*i+1;
if(2*i+2 < len && arr[2*i+2] > arr[maxIndex]) maxIndex = 2*i+2;
if(maxIndex != i){
swap(arr, maxIndex, i);
adjust(arr, maxIndex);
}
}
public void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
小根堆
public void buildHeap(int[] arr){
for(int i = arr.length / 2 - 1; i >= 0; i--){
adjust(arr, i);
}
}
public void adjust(int[] arr, int i){
int minIndex = i;
int len = arr.length;
if(2*i+1 < len && arr[2*i+1] < arr[minIndex]) maxIndex = 2*i+1;
if(2*i+2 < len && arr[2*i+2] < arr[minIndex]) maxIndex = 2*i+2;
if(maxIndex != i){
swap(arr, maxIndex, i);
adjust(arr, maxIndex);
}
}
public void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
4.合并两个无序数组为一个有序数组
合并两个数组,然后快排或者堆排序,香不
5.在一个数组中,找到最长子序列
再等等
6.爬楼梯问题,打印所有结果
class Solution {
public static List<List<Integer>> res = new LinkedList<>();
public int numWays(int n) {
if(n==0) return 1;
LinkedList<Integer> track = new LinkedList<Integer>();
int[] nums = new int[]{1, 2};
backtrack(nums, n, track);
System.out.println(res);
return res.size();
}
private static void backtrack(int[] nums, int n, LinkedList<Integer> track) {
if(n<0)
return;
if(n==0) {
res.add(new LinkedList(track));
System.out.println(track);
return;
}
for(int i=0; i<nums.length; i++) {
track.add(nums[i]);
backtrack(nums, n-nums[i], track);
track.removeLast();
}
}
}