显而易见并没有一天10题,希望两天十题多刷几遍
- 替换空格
package offer;
import java.util.Scanner;
//替换空格
//s = "We are happy."
public class Test3 {
public static String replaceSpace(String s) {
//所有代码先来要判空
if (s.length() == 0 || s == null)
return "";
//暴力解决 用StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
StringBuffer sb = new StringBuffer();
//是把字符串的每个字符拆开来添加更改
for (int i = 0; i < s.length(); i++) {
//""为字符串 ''为单个字符
if (s.charAt(i) == ' ') {
sb.append("%20");
} else sb.append(s.charAt(i));
}
return sb.toString();
}
//用两个下标相当于指针
public static String replaceSpace2(StringBuffer s) {
//传入StringBuffer s 直接在自身更改
if (s == null)
return null;
//旧数组的下标
int oldLength = s.length();
//扩充字符串的长度 空格字符串占2位
for (int i = 0; i < oldLength; i++) {
if (s.charAt(i) == ' ') {
s.append(" ");
}
}
//新的字符串长度
int newLength = s.length();
int p1 = oldLength - 1, p2 = newLength - 1;
while (p1 != p2) {
if (s.charAt(p1) == ' ') {
//设置数值 位置向前移动 并且赋值
s.setCharAt(p2--, '0');
s.setCharAt(p2--, '2');
s.setCharAt(p2--, '%');
p1--;
} else {
s.setCharAt(p2--, s.charAt(p1--));
}
}
return s.toString();
}
public static void main(String[] args) {
System.out.println(replaceSpace("We are happy."));
}
}
从尾到头打印两边
package offer;
import java.util.Stack;
class Node {
int val;
Node next;
public Node(int val) {
this.val = val;
}
public Node() {
}
}
public class Test4 {
//打印链表从头往后打印
public static void print(Node head) {
Node temp = head;
while (temp != null) {
System.out.println(temp.val);
temp = temp.next;
}
}
//利用栈结构反向输出链表的值
public static void reversePrint(Node head) {
//从前往后获取一个值 我就压到栈里
Node temp = head;
Stack stack = new Stack<>();
while (temp != null) {
stack.push(temp.val);
temp = temp.next;
}
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
public static void printListReverByRecurdion(Node head) {
if (head != null) {
if (head.next != null) {
printListReverByRecurdion(head.next);
}
System.out.println(head.val);
}
}
public static void main(String[] args) {
//新建链表
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
node1.next = node2;
node2.next = node3;
node3.next = node4;
// print(node1);
// reversePrint(node1);
printListReverByRecurdion(node1);
}
}
顺道复习一下用 构建单链表 增删改查
斐波那契额数列 时间复杂度函数相加<递归
package offer;
import java.util.Scanner;
//斐波那契数列
public class Test5 {
//递归写法
public static int fn(int n) {
if (n < 1)
return -1;
if (n == 1 || n == 2)
return 1;
else {
return fn(n - 1) + fn(n - 1);
}
}
//不用递归
public static int fn1(int n) {
int fib = 0, pre1 = 0, pre2 = 1;
if (n < 2 && n > 0)
return 1;
//后一个为前两项加和
for (int i = 2; i <= n; i++) {
fib = pre1 + pre2;
pre1 = pre2;
pre2 = fib;
}
return fib;
}
//青蛙跳台阶递归问题的解释与延申 一样道理 看成整体块状
// https://blog.csdn.net/weixin_52606524/article/details/113037147?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165434764316782391858771%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165434764316782391858771&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-113037147-null-null.142^v11^control,157^v13^control&utm_term=%E9%9D%92%E8%9B%99%E8%B7%B3%E5%8F%B0%E9%98%B6&spm=1018.2226.3001.4187
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(fn(sc.nextInt()));
System.out.println(fn1(sc.nextInt()));
}
}