一、递归
定义:
- 一个问题的解可以分解为几个子问题的解。
- 这个问题与分解之后的子问题,除了数据规模不同,求解思路完全一样。
- 存在基线/终止条件。
1.1爬楼梯
题目描述:
1.1.1递归解法
解决思路:
f
(
n
)
=
{
1
n
=
1
2
n
=
2
f
(
n
−
1
)
+
f
(
n
−
2
)
n
>
2
f(n)=\begin{cases} 1 & n=1\\ 2 & n=2\\ f(n-1)+f(n-2) & n>2 \end{cases}
f(n)=⎩
⎨
⎧12f(n−1)+f(n−2)n=1n=2n>2
具体代码:
public int climbStairsWithRecursive(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
return climbStairsWithRecursive(n - 1) + climbStairsWithRecursive(n - 2);
}
缺点:复杂度较高 O(n2)
优化:
使用HashMap存储已经求出的结果,可以直接使用
private Map<Integer, Integer> storeMap = new HashMap<>();
public int climbStairs(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
if (null != storeMap.get(n)) {
return storeMap.get(n);
} else {
int result = climbStairs(n - 1) + climbStairs(n - 2);
storeMap.put(n, result);
return result;
}
}
1.1.2循环解法,自底向上累加
示例代码:
public int climbStairs(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
int result = 0;
int pre = 2;
int prePre = 1;
for (int i = 3; i <= n; ++I) {
result = pre + prePre;
prePre = pre;
pre = result;
}
return result;
}
时间复杂度:O(n)
二、数组
2.1 两数之和
题目描述:
解法思路:
2.1.1暴力穷举
public int[] twoSum(int[] nums, int target) {
int[] result = 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) {
result[0] = i;
result[1] = j;
return result;
}
}
}
return result;
}
时间复杂度:O(n2)
2.1.2引用HashMap,避免二次扫描
public int[] twoSum(int[] nums, int target) {
/*key为元素值,value为每个元素对应的下标*/
Map<Integer, Integer> storeNums = new HashMap<>(nums.length, 1);
int[] result = new int[2];
for (int i = 0; i < nums.length; i++) {
int another = target - nums[i];
Integer anotherIndex = storeNums.get(another);
if (null != anotherIndex) {
result[0] = anotherIndex;
result[1] = i;
break;
} else {
storeNums.put(nums[i], i);
}
}
return result;
}
时间复杂度:O(n)
2.2 合并两个有序数组
题目描述:
解决思路:
2.2.1使用Arrays排序
public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i = 0; i < n; ++i) {
nums1[m + i] = nums2[i];
}
Arrays.sort(nums1);
}
时间复杂度:使用的快速排序,O((m+n)log(m+n))
2.2.2 使用双指针,创建一个临时数组
public void merge(int[] nums1, int m, int[] nums2, int n) {
int k = m+n;
int[] temp = new int[k];
for (int index = 0, nums1Index = 0, nums2Index = 0; index < k; index++) {
if (nums1Index >= m) {
/*nums1数组已经取完,完全取nums2数组的值即可*/
temp[index] = nums2[nums2Index++];
} else if (nums2Index >= n) {
/*nums2数组已经取完,完全取nums1数组的值即可*/
temp[index] = nums1[nums1Index++];
} else if (nums1[nums1Index] < nums2[nums2Index]) {
/*nums1数组的元素值小于nums2数组,取nums1数组的值*/
temp[index] = nums1[nums1Index++];
} else {
/*nums2数组的元素值小于等于nums1左边数组,取nums2数组的值*/
temp[index] = nums2[nums2Index++];
}
}
for (int i = 0; i < k; i++) {
nums1[i] = temp[i];
}
}
时间复杂度:O(m+n)
空间复杂度:O(m+n)
2.2.3使用双指针,不创建一个临时数组
public void merge(int[] nums1, int m, int[] nums2, int n) {
int k = m+n;
for (int index = k-1, nums1Index = m-1, nums2Index = n-1; index >= 0; index--) {
if (nums1Index < 0) {
/*nums1数组已经取完,完全取nums2数组的值即可*/
nums1[index] = nums2[nums2Index--];
} else if (nums2Index < 0) {
/*nums2数组已经取完,完全取nums1数组的值即可*/
break;
} else if (nums1[nums1Index] > nums2[nums2Index]) {
/*nums1数组的元素值大于nums2数组,取nums1数组的值*/
nums1[index] = nums1[nums1Index--];
} else {
/*nums2数组的元素值大于等于nums1左边数组,取nums2数组的值*/
nums1[index] = nums2[nums2Index--];
}
}
}
时间复杂度:O(m+n)
空间复杂度:O(1)
2.3 移动零
题目描述:
解法思路:
2.3.1 双指针移动零
public void moveZeroes(int[] nums) {
if (nums == null) {
return;
}
//第一次遍历的时候,j指针记录非0的个数,只要是非0的统统都赋值给nums[j]
int j = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] != 0) {
nums[j++] = nums[i];
}
}
//非0元素统计完了,剩下的都是0了
//
for (int i = j; i < nums.length; ++i) {
nums[i] = 0;
}
}
2.4 找到所有数组中消失的数字
题目描述:
解法思路:
2.4.1标记法
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
for (int num : nums) {
int x = (num - 1) % n;/*对n取模来还原出它本来的值*/
nums[x] += n;
}
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
if (nums[i] <= n) {
result.add(i + 1);
}
}
return result;
}
三、链表
3.1 合并两个有序链表
题目描述:
解法思路:
3.1.1循环+双指针
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
ListNode resultNode = new ListNode(0);
ListNode p = resultNode;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if (l1 != null) {
p.next = l1;
}
if (l2 != null) {
p.next = l2;
}
return resultNode;
}
时间复杂度:O(m+n)
空间复杂度:O(1)
3.1.2递归解决
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
}
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
时间复杂度:O(m+n)
空间复杂度:O(m+n)
3.2 删除排序链表中的重复元素
题目描述:
解法思路:
3.2.1 遍历
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return head;
}
ListNode currentNode = head;
while (null != currentNode.next) {
if (currentNode.next.val == currentNode.val) {
currentNode.next = currentNode.next.next;
} else {
currentNode = currentNode.next;
}
}
return head;
}
时间复杂度:O(n)
空间复杂度:O(1)
3.2.2 递归
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}