## Remove duplicates from sorted list

### 题目描述

Given a sorted linked list, delete all duplicates such that each element appear only once.For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.（保留1个）

### 题目解析

 if(p1->val != p2->val) { p1 = p1->next; continue; }

• 不断判断下面有几个点和当前点相等，代码如下：
while(p2->next != NULL && p1->val == p2->next->val) p2 = p2->next;
• 找到最后一个点和当前点相等的时候，直接把那个点的next赋值给当前点的next，然后那些点。即：
p1->next = p2->next;delete p2;

### 代码实现

 ListNode *deleteDuplicates(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode* p1 = head; while(p1 != NULL && p1->next != NULL) { ListNode* p2 = p1->next; if(p1->val != p2->val) { p1 = p1->next; continue; } while(p2->next != NULL && p1->val == p2->next->val) p2 = p2->next; p1->next = p2->next; delete p2; p1 = p1->next; } return head; }

## 删除链表中重复的节点（全部删除）

### 代码实现

ListNode* deleteDuplication(ListNode* pHead) { if(pHead == NULL) return NULL; if(pHead->next== NULL) return pHead; if(pHead->val==pHead->next->val) { ListNode* pNode = pHead->next; while(pNode!=NULL && pNode->val==pHead->val) pNode=pNode->next; return deleteDuplication(pNode); } else { pHead->next=deleteDuplication(pHead->next); return pHead; } }

## Jump Game

### 题目说明

Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:
A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse.

### 题目解析

• 当前是第i个元素，但是i已经大于n-1了（下标从0开始，所以第n个数是第n-1）。
• A[i]为0：这里面又分两种情况，一种是如题目所给的第2种情况，在中间为0的这个位置不走了，还有一种情况是我这个数组一共1个元素，就是0。那么跳出。

### 代码展示

bool canJump(int A[], int n) { int i = 0; while (A[i] != 0) { int jumpNum = A[i]; i += jumpNum; if (i >= n-1) break; } return i >= n-1 ? true : false;}

## rotate image

### 题目描述

You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:
Could you do this in-place?

### 代码实现

 void rotate(vector<vector<int> > &matrix) { //对角线对称 int n=matrix.size(); for(int i=0;i<n;i++) for(int j=0;j<n-i;j++) swap(matrix[i][j],matrix[n-1-j][n-1-i]); //上下交换 for(int i=0;i<n/2;i++) for(int j=0;j<n;j++) swap(matrix[i][j],matrix[n-1-i][j]); }

## Trapping Rain Water

### 题目描述

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,
Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcosfor contributing this image!

### 题目分析

if (A[i] < left) suml += left - A[i]; else left = A[i];

 if (A[i] < right) sumr += right - A[i]; else right = A[i];

### 代码实现

int trap(int A[], int n) { int maxHeight = 0; int left = 0;//由左往最大值计算 int right = 0;//由右往最大值计算 int suml = 0; int sumr = 0; //寻找到高度的最大值，将区间分为左侧和右侧两部分进行分别处理 for (int i = 0; i < n; i++) if (A[i] > A[maxHeight]) maxHeight = i; //先计算左侧的容量 for (int i = 0; i < maxHeight; i++) { if (A[i] < left) suml += left - A[i]; else left = A[i]; } for (int i = n-1; i > maxHeight; i--) { if (A[i] < right) sumr += right - A[i]; else right = A[i]; } return suml + sumr;}