将大问题转化为小问题,通过递归依次解决各个小问题
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组
char[]的形式给出。
这道题直接交换更快
class Solution {
public void reverseString(char[] s) {
int len = s.length;
int start = 0, end = len - 1;
while (start <= end) {
char c = s[start];
s[start++] = s[end];
s[end--] = c;
}
}
}给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
直接迭代空间复杂度小,速度快
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode newHead = new ListNode(-1);
newHead.next = head;
ListNode pre = newHead;
while(pre.next != null && pre.next.next != null) {
ListNode l1 = pre.next, l2 = pre.next.next;
ListNode next = l2.next;
l1.next = next;
l2.next = l1;
pre.next = l2;
pre = l1;
}
return newHead.next;
}
}给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<TreeNode> generateTrees(int n) {
if (n == 0) {
return new LinkedList<TreeNode>();
} else {
return allTrees(1, n);
}
}
private List<TreeNode> allTrees(int start, int end) {
List<TreeNode> ans = new LinkedList<>();
if (start > end) {
ans.add(null);
return ans;
}
for (int i = start; i <= end; ++i) {
List<TreeNode> left = allTrees(start, i - 1);
List<TreeNode> right = allTrees(i + 1, end);
for (TreeNode leftNode : left) {
for (TreeNode rightNode : right) {
TreeNode root = new TreeNode(i);
root.left = leftNode;
root.right = rightNode;
ans.add(root);
}
}
}
return ans;
}
}斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 给定 N,计算 F(N)。
迭代解法,自底向上
class Solution {
public int fib(int N) {
if (N <= 1) {
return N;
}
return memoize(N);
}
private int memoize(int n) {
int[] cache = new int[n + 1];
cache[1] = 1;
for (int i = 2; i <= n; ++i) {
cache[i] = cache[i - 1] + cache[i - 2];
}
return cache[n];
}
}递归解法,自顶向下
class Solution {
private Integer[] cache = new Integer[31];
public int fib(int N) {
if (N <= 1) {
return N;
}
cache[0] = 0;
cache[1] = 1;
return memoize(N);
}
private int memoize(int n) {
if (cache[n] != null) {
return cache[n];
}
cache[n] = memoize(n - 1) + memoize(n - 2);
return memoize(n);
}
}