1. 替换空格
(1)题目描述
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
(2)题目分析
这个题没有可分析的必要,小学生都会做…
(3)代码
```java
package swordOffer;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-21 13:32
* @desc 替换空格
*/
public class t6 {
public static String replaceSpace(String s) {
StringBuilder ss = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (" ".equals(String.valueOf(s.charAt(i)))) {
ss.append("%20");
} else {
ss.append(s.charAt(i));
}
}
return ss.toString();
}
public static void main(String[] args) {
String str = "saa sds sdsad";
System.out.println(replaceSpace(str));
}
}
2. 从头到尾打印链表
(1)题目描述
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
(2)题目分析
这个题有多种思路,首先可以用递归或者栈来实现,但是使用了额外的空间复杂度。或者可以通过反转链表来实现,但是改变了原有链表的结构。因此最优解是通过获取链表的长度,然后遍历链表,从尾到头将链表的节点值放入数组中。
(3)代码
package swordOffer;
import charpter2.ListNode;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-21 13:38
* @desc 从尾到头打印链表
*/
public class t7 {
public static void main(String[] args) {
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(3);
ListNode listNode4 = new ListNode(4);
ListNode listNode5 = new ListNode(5);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
int res[] = reversePrint(listNode1);
for (int i : res) {
System.out.println(i);
}
}
public static int[] reversePrint(ListNode head) {
ListNode temp = head;
int num = 0;
while (temp != null) {
num++;
temp = temp.next;
}
int[] res = new int[num];
while (head != null) {
res[--num] = head.val;
head = head.next;
}
return res;
}
}
3. 用两个栈实现队列
(1)题目描述
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
示例 1:
输入:
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
输出:[null,null,3,-1]
(2)题目分析
当插入数字时,先判断栈A是否为空,为空则直接插入栈A,如果不为空,则将栈A中的数字全部放入栈B中,然后插入到栈A。当删除数字时,先判断栈B是否为空,为空则直接从栈A进行删除,如果不为空,则将栈B的数字全部放入栈A中,然后对栈A进行删除。
(3)代码
package swordOffer;
import java.util.Stack;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-22 12:26
* @desc 用两个栈实现队列
*/
public class t8 {
Stack<Integer> stack1 = new Stack();
Stack<Integer> stack2 = new Stack();
public t8() {
}
public void appendTail(int value) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
stack1.push(value);
}
public int deleteHead() {
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
return stack1.empty() ? -1 : stack1.pop();
}
public static void main(String[] args) {
t8 tt = new t8();
tt.appendTail(1);
tt.appendTail(2);
tt.appendTail(3);
tt.deleteHead();
}
}
4. 斐波那契数列
(1)题目描述
写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项。斐波那契数列的定义如下:
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
(2)题目分析
本题为动态规划的经典问题,每一步的结果等于前两步的结果之和,因此只需要设定三个变量,然后依次更新即可。
(3)代码
package swordOffer;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-22 12:45
* @desc 斐波那契数列
*/
public class t9 {
public static int fib(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
int a = 0;
int b = 1;
int c = 0;
for (int i = 2; i <= n; i++) {
c = (a + b) % 1000000007;
a = b;
b = c;
}
return c;
}
public static void main(String[] args) {
System.out.println(fib(7));
}
}
5. 青蛙跳台阶问题
(1)题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
示例 1:
输入:n = 2
输出:2
(2)题目分析
本题的解题思路同上题,不再赘述。
(3)代码
package swordOffer;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-22 12:56
* @desc 青蛙跳台阶
*/
public class t10 {
public static int numWays(int n) {
if (n <= 1) {
return 1;
}
int a = 1;
int b = 1;
int c = 0;
for (int i = 2; i <= n; i++) {
c = (a + b) % 1000000007;
a = b;
b = c;
}
return c;
}
public static void main(String[] args) {
System.out.println(numWays(7));
}
}