1.请实现一个函数,把字符串 s 中的每个空格替换成"%20"
/**
*
*
* 输入:s = "We are happy."
* 输出:"We%20are%20happy."
*/
public class A_01_ReplaceSpace {
public static void main(String[] args) {
System.out.println(replaceSpace("We are happy."));
}
public static String replaceSpace(String s) {
char[] chars = s.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i<chars.length;i++){
if(chars[i]==' '){
stringBuilder.append("%20");
}else {
stringBuilder.append(chars[i]);
}
}
return stringBuilder.toString();
}
}
2.输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
/**
* <p>
* 输入:head = [1,3,2]
* 输出:[2,3,1]
*/
public class A_02_reversePrint {
public static void main(String[] args) {
ListNode listNode1 = new ListNode(1);
ListNode listNode3 = new ListNode(3);
ListNode listNode2 = new ListNode(2);
listNode1.next = listNode2;
listNode2.next = listNode3;
Solution solution = new Solution();
System.out.println(solution.reversePrint(listNode1)[2]);
}
}
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> stack = new Stack<>();
while (head != null) {
stack.push(head.val);
head = head.next;
}
int[] vals = new int[stack.size()];
int index = 0;
while (!stack.isEmpty()) {
vals[index++] = stack.pop();
}
return vals;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
3.用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
/**
*
* 输入:
* ["CQueue","appendTail","deleteHead","deleteHead"]
* [[],[3],[],[]]
* 输出:[null,null,3,-1]
*/
public class A_03_CQueue {
public static void main(String[] args) {
CQueue cQueue = new CQueue();
cQueue.appendTail(1);
System.out.println(cQueue.deleteHead());
System.out.println(cQueue.deleteHead());
}
}
class CQueue {
private List<Integer> list;
public CQueue() {
list = new ArrayList<>();
}
public void appendTail(int value) {
list.add(value);
}
public int deleteHead() {
return list.isEmpty() ? -1 : list.remove(0);
}
}
4.定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
/**
*
*
*
*
* 示例:
*
* 输入: 1->2->3->4->5->NULL
* 输出: 5->4->3->2->1->NULL
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9pdjbm/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
public class A_05_reverseNodeList {
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode listNode22 = new ListNode(2);
ListNode listNode23 = new ListNode(3);
head.next = listNode22;
listNode22.next = listNode23;
if(head==null){
return;
}
Stack<ListNode> s = new Stack<>();
ListNode tempNode = head;
while (tempNode.next != null) {
s.push(tempNode);
tempNode = tempNode.next;
}
ListNode listNode2 = tempNode;
ListNode lis = tempNode;
while (!s.isEmpty()) {
tempNode = s.pop();
listNode2.next = tempNode;
listNode2 = tempNode;
}
tempNode.next=null;
}
}
package demo;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
* 定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。
*
* MinStack minStack = new MinStack();
* minStack.push(-2);
* minStack.push(0);
* minStack.push(-3);
* minStack.min(); --> 返回 -3.
* minStack.pop();
* minStack.top(); --> 返回 0.
* minStack.min(); --> 返回 -2.
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/50bp33/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
public class A_06_MinStack {
public static void main(String[] args) {
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min();
minStack.pop();
minStack.top();
minStack.min();
}
}
class MinStack {
public LinkedList<Integer> allList;
private LinkedList<Integer> minList;
public MinStack() {
allList = new LinkedList<>();
minList = new LinkedList<>();
}
public void push(int x) {
allList.add(x);
/**
* 只存比自己小的,为了保持长度一致,所以条件不成立时,要存自己
*/
if(!minList.isEmpty()&&minList.getLast()<=x){
minList.addLast(minList.getLast());
}else {
minList.addLast(x);
}
}
public void pop() {
allList.removeLast();
minList.removeLast();
}
public int top() {
return allList.getLast();
}
/**
* 实际上是这个方法特俗,所以专门拿个数组来存这个数
* @return
*/
public int min() {
return minList.getLast();
}
}
package demo;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
/**
* 请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9p0yy1/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
public class A_07_copyLinkedList {
public static void main(String[] args) {
}
}
// Definition for a Node.
class Node1 {
int val;
Node1 next;
Node1 random;
public Node1(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
class Solution1 {
public Node1 copyRandomList(Node1 head) {
if(head==null){
return null;
}
/**
* key 和value是两组数据,value是复制数据
*/
Map<Node1,Node1> map = new HashMap<>();
Node1 currentNode = head;
while (currentNode!=null){
Node1 newNode = new Node1(currentNode.val);
map.put(currentNode,newNode);
currentNode = currentNode.next;
}
for(Map.Entry<Node1,Node1> entry: map.entrySet()){
entry.getValue().next = map.get(entry.getKey().next);
entry.getValue().random = map.get(entry.getKey().random);
}
return map.get(head);
}
}
package demo;
public class A_08_reverseLeftWords {
public static void main(String[] args) {
System.out.println(new Solution2().reverseLeftWords("abcdefg",2));
}
}
class Solution2 {
public String reverseLeftWords(String s, int n) {
char[] chars = s.toCharArray();
char[] tempChar = new char[n];
int index = 0;
for(int i=0;i<chars.length;i++){
if(i<n){
tempChar[i] = chars[i];
}
if(i<chars.length-n){
chars[i] = chars[i+n];
}
if(i>=chars.length-n){
chars[i] = tempChar[index++];
}
}
return new String(chars);
}
}
package demo;
/**
* 给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
* 示例:
*
* 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
* 输出: [3,3,5,5,6,7]
* 解释:
*
* 滑动窗口的位置 最大值
* --------------- -----
* [1 3 -1] -3 5 3 6 7 3
* 1 [3 -1 -3] 5 3 6 7 3
* 1 3 [-1 -3 5] 3 6 7 5
* 1 3 -1 [-3 5 3] 6 7 5
* 1 3 -1 -3 [5 3 6] 7 6
* 1 3 -1 -3 5 [3 6 7] 7
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58o46i/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
public class A_09_maxSlidingWindow {
public static void main(String[] args) {
int[] nums = {1,3,-1,-3,5,3,6,7};
int k=3;
xx(nums,k);
}
private static int[] xx(int[] nums,int k){
if(null==nums||nums.length==0){
int[] aim = {};
return aim;
}
int[] aim = new int[nums.length-k+1];
for(int i=0;i<aim.length;i++){
aim[i] = getTheMaxNum(nums,i,k);
}
return aim;
}
private static int getTheMaxNum(int[] nums,int index,int k){
int max = nums[index];
for(int i = index;i<index+k;i++){
if(max<nums[i]){
max=nums[i];
}
}
return max;
}
}
package demo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
/**
* 请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是O(1)。
*
* 若队列为空,pop_front 和 max_value 需要返回 -1
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/e2t5ug/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
public class A_10_MaxQueue {
public static void main(String[] args) {
MaxQueue maxQueue = new MaxQueue();
maxQueue.push_back(-9);
maxQueue.push_back(-1);
maxQueue.push_back(0);
maxQueue.push_back(-2);
for(int i=0;i<100;i++){
maxQueue.pop_front();
}
for(Integer i:maxQueue.maxList){
System.out.println(i);
}
}
private static class MaxQueue {
LinkedList<Integer> list;
List<Integer> maxList;
public MaxQueue() {
list = new LinkedList<>();
// 用arrayu快一点
maxList = new ArrayList<>();
}
public int max_value() {
if(maxList.isEmpty()){
return -1;
}else {
return maxList.get(0);
}
}
public void push_back(int value) {
list.addLast(value);
// 单调递增数组
if(maxList.contains(value)){
return;
}
int oldSize = maxList.size();
for(int i=0;i<oldSize;i++){
if(value==maxList.get(i)){
return;
}else if(value>maxList.get(i)){
maxList.add(i,value);
return;
}
}
maxList.add(maxList.size(),value);
}
public int pop_front() {
if(list.isEmpty()){
return -1;
}else {
int i = list.removeFirst();
if(!maxList.isEmpty()){
for(int index=0;index<maxList.size();index++){
if(i==maxList.get(index)){
maxList.remove(index);
break;
}
}
}
return i;
}
}
}
}
package demo;
/**
* 输入: "42"
* 输出: 42
* 示例 2:
*
* 输入: " -42"
* 输出: -42
* 解释: 第一个非空白字符为 '-', 它是一个负号。
* 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。
* 示例 3:
*
* 输入: "4193 with words"
* 输出: 4193
* 解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。
* 示例 4:
*
* 输入: "words and 987"
* 输出: 0
* 解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
* 因此无法执行有效的转换。
* 示例 5:
*
* 输入: "-91283472332"
* 输出: -2147483648
* 解释: 数字 "-91283472332" 超过 32 位有符号整数范围。
* 因此返回 INT_MIN (−231) 。
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/58pq8g/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
public class A_11_Solution {
public static void main(String[] args) {
SolutionStr solutionStr = new SolutionStr();
System.out.println(solutionStr.strToInt("-01324000"));
System.out.println();
}
private static class SolutionStr {
public int strToInt(String str) {
str = str.trim();
if("".equals(str)||null==str){
return 0;
}
char[] chars = str.toCharArray();
char fastChar = chars[0];
int i=0;
if('-'==fastChar||'+'==fastChar){
i=1;
}
StringBuilder stringBuilder = new StringBuilder();
while (i<chars.length){
if(48<=chars[i]&&chars[i]<=57){
stringBuilder.append(chars[i]);
}else {
break;
}
i++;
}
char[] chars1 = stringBuilder.toString().toCharArray();
int z;
for(z=0;z<chars1.length;z++){
if(chars1[z]!='0'){
break;
}
}
stringBuilder.delete(0,z);
if(stringBuilder.length()==0){
return 0;
}
System.out.println(stringBuilder);
char[] maxNumber = String.valueOf(Integer.MAX_VALUE).toCharArray();
if(stringBuilder.length()>maxNumber.length){
if('-'==fastChar){
return Integer.MIN_VALUE;
}else {
return Integer.MAX_VALUE;
}
}
// 懒得写了,用long吧
long l = Long.parseLong(stringBuilder.toString());
if('-'==fastChar){
l=-1*l;
}
if(l>Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else if(l<Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
return (int) l;
}
}
}
package demo;
import java.math.BigInteger;
/**
* 写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下:
*
* F(0) = 0, F(1) = 1
* F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
* 斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。
*
* 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
*
*
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/50fxu1/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*
*/
public class B_01_Fibonacci {
public static void main(String[] args) {
System.out.println(new B_01_Fibonacci_Solution().fib1(95));
}
private static class B_01_Fibonacci_Solution {
public int fib1(int n){
if(n<2) {
return n;
}
int a = 0, b = 1;
for(int i = 0; i < n; i++){
// 跳棋模板
int sum = (a + b)%1000000007;
a = b;
b = sum;
}
return a;
}
public int fib(int n) {
if(n<2) {
return n;
}
BigInteger[] bigIntegers = new BigInteger[n+1];
bigIntegers[0] = BigInteger.valueOf(0);
bigIntegers[1] = BigInteger.valueOf(1);
for(int i=2;i<=n;i++){
bigIntegers[i] = BigInteger.valueOf(0);
}
for(int i=2;i<=n;i++){
bigIntegers[i]=bigIntegers[i-1].add(bigIntegers[i-2]);
}
bigIntegers[n] = bigIntegers[n].mod(new BigInteger("1000000007"));
return bigIntegers[n].intValue();
}
}
}
package demo;
/**
* 一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。
*
* 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
*
* 示例 1:
*
* 输入:n = 2
* 输出:2
* 示例 2:
*
* 输入:n = 7
* 输出:21
* 示例 3:
*
* 输入:n = 0
* 输出:1
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/57hyl5/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
public class B_02_qingwatiaotai {
public static void main(String[] args) {
}
/**
* f(0)=1
* f(1)=1
* f(2)=2
* f(3)=3
*
*
* @param n
* @return
*/
private static int run(int n){
int a = 1, b = 1, sum;
for(int i = 0; i < n; i++){
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return a;
}
}
package demo;
/**
* 请实现一个函数用来匹配包含'. '和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但与"aa.a"和"ab*a"均不匹配。
*
* 示例 1:
*
* 输入:
* s = "aa"
* p = "a"
* 输出: false
* 解释: "a" 无法匹配 "aa" 整个字符串。
* 示例 2:
*
* 输入:
* s = "aa"
* p = "a*"
* 输出: true
* 解释: 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
* 示例 3:
*
* 输入:
* s = "ab"
* p = ".*"
* 输出: true
* 解释: ".*" 表示可匹配零个或多个('*')任意字符('.')。
* 示例 4:
*
* 输入:
* s = "aab"
* p = "c*a*b"
* 输出: true
* 解释: 因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。
* 示例 5:
*
* 输入:
* s = "mississippi"
* p = "mis*is*p*."
* 输出: false
* s 可能为空,且只包含从 a-z 的小写字母。
* p 可能为空,且只包含从 a-z 的小写字母以及字符 . 和 *,无连续的 '*'。
*
* 作者:Krahets
* 链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9a1ypc/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
public class B_03_isMatch {
public static void main(String[] args) {
String tems = "";
}
private static boolean isMatch(String s, String p) {
int m = s.length() + 1, n = p.length() + 1;
boolean[][] dp = new boolean[m][n];
dp[0][0] = true;
for (int i = 2; i < n; i++) {
dp[0][i] = dp[0][i - 2] && p.charAt(i - 1) == '*';
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (p.charAt(j - 1) == '*') {
if (dp[i][j - 2]) {
dp[i][j] = true;
} else if (dp[i - 1][j] && s.charAt(i - 1) == p.charAt(j - 2)) {
dp[i][j] = true;
} else if (dp[i - 1][j] && p.charAt(j - 2) == '.') {
dp[i][j] = true;
}
} else {
if (dp[i - 1][j - 1] && s.charAt(i - 1) == p.charAt(j - 1)) {
dp[i][j] = true;
} else if (dp[i - 1][j - 1] && p.charAt(j - 1) == '.') {
dp[i][j] = true;
}
}
}
}
return dp[m-1][n-1];
}
}