-
剑指 Offer 10- II. 青蛙跳台阶问题
-
- 解
-
剑指 Offer 11. 旋转数组的最小数字
-
- 解
-
剑指 Offer 12. 矩阵中的路径
-
- 解
-
剑指 Offer 13. 机器人的运动范围
-
- 解
-
剑指 Offer 14- I. 剪绳子
-
- 解
-
剑指 Offer 15. 二进制中1的个数
-
- 解
找出数组中重复的数字。
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
限制:
2 <= n <= 100000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zu-z
hong-zhong-fu-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 额外使用一个数据用于统计数组counts初始值全为0,原数组nums的数据对应counts数组的下标,counts数组每次下标++, 如果为2说明出现重复,返回即可
class Solution {
public int findRepeatNumber(int[] nums) {
int[] counts = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
counts[nums[i]]++;
if (counts[nums[i]] == 2) {
return nums[i];
}
}
return -1;
}
}
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
示例:
现有矩阵 matrix 如下:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
给定 target = 5,返回 true。
给定 target = 20,返回 false。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 从第一行最后一个数据开始判断,如果大于目标数据就往下一行走,如果小于目标数据就往前走一个位置,直到边界出现问题,按照这个思路画图自己走一遍很好理解
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) {
return false;
}
int row = matrix.length;
int col = matrix[0].length - 1;
int start = 0;
while (start < row && col >= 0) {
int num = matrix[start][col];
if (num < target) {
start++;
} else if (num > target) {
col–;
} else {
return true;
}
}
return false;
}
}
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
限制:
0 <= s 的长度 <= 10000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 这道题用Java自带的API也开始实现,但是最好还是自写一写,很简单没必要多说
class Solution {
public String replaceSpace(String s) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch != ’ ') {
builder.append(ch);
} else {
builder.append(“%20”);
}
}
return builder.toString();
}
}
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 先遍历一遍链表统计它的长度,然后再遍历一遍让数组中逆序放数据
/**
-
Definition for singly-linked list.
-
public class ListNode {
-
int val;
-
ListNode next;
-
ListNode(int x) { val = x; }
-
}
*/
class Solution {
public int[] reversePrint(ListNode head) {
int count = 0;
ListNode temp = head;
while (temp != null) {
temp = temp.next;
count++;
}
int[] nums = new int[count];
while (–count >= 0) {
nums[count] = head.val;
head = head.next;
}
return nums;
}
}
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如,给出
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
3
/
9 20
/
15 7
限制:
0 <= 节点个数 <= 5000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 这道题还算有点难度, 但是也很好理解,每次去先序遍历的index下标表示当前节点的数据,初始化好节点后index++, 然后在中序数组中找到当前节点的下标pos,然后递归创建其左子树,但是此时left和right要有变化,因为左子树节点一定在pos的左边,右子树节点一定在pos的右边,如果此时left>=right说明此节点不成立
/**
-
Definition for a binary tree node.
-
public class TreeNode {
-
int val;
-
TreeNode left;
-
TreeNode right;
-
TreeNode(int x) { val = x; }
-
}
*/
class Solution {
private int index;
public TreeNode buildTree(int[] preorder, int[] inorder) {
index = 0;
return buildTreeHelper(preorder, inorder, 0, inorder.length);
}
private TreeNode buildTreeHelper(int[] preorder, int[] inorder, int left, int right) {
if (index >= preorder.length) {
return null;
}
if (left >= right) {
return null;
}
TreeNode node = new TreeNode(preorder[index++]);
int pos = findPos(inorder, node.val);
node.left = buildTreeHelper(preorder, inorder, left, pos);
node.right = buildTreeHelper(preorder, inorder, pos + 1, right);
return node;
}
private int findPos(int[] inorder, int val) {
for (int i = 0; i < inorder.length; i++) {
if (inorder[i] == val) {
return i;
}
}
return -1;
}
}
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
示例 1:
输入:
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
输出:[null,null,3,-1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 这道题也很简单,比如栈1和栈2,你将数据全放在栈1,栈2只是起辅助作用,入栈全入栈1,出栈首先将数据出栈倒腾在栈2,然后栈2出栈,最后再将数据放回到栈1即可实现
class CQueue {
Deque stack1;
Deque stack2;
public CQueue() {
stack1 = new LinkedList();
stack2 = new LinkedList();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
// 如果第二个栈为空
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
if (stack2.isEmpty()) {
return -1;
} else {
int deleteItem = stack2.pop();
return deleteItem;
}
}
}
写一个函数,输入 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。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 使用一个for循环就可以搞定,首先让sum等于a+b用于保存后续一个斐波那契数,然后让a = b, b=sum,直到i循坏到达n停止,切记i是从0开始循环的,所以最后直接返回a的值即可.
class Solution {
public int fib(int n) {
int a = 0;
int b = 1;
int sum = 0;
for (int i = 0; i < n; i++) {
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return a;
}
}
一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 这道题和上面一样, 但是有个点不一样就是如果是0个台阶,输出也得是1,这个要注意一下
class Solution {
public int numWays(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;
}
}
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一个旋转,该数组的最小值为1。
示例 1:
输入:[3,4,5,1,2]
输出:1
示例 2:
输入:[2,2,2,0,1]
输出:0
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 使用二分法,如果此时中间下标大于high下标数据, 说明最小值在mid + 1 ~ high之间, 如果此时mid下标数据小于high下标数据, 说明最小值在low~mid之间, 一定要注意此时的mid也有可能是最小值, 所以应该是high = mid,如果mid和high相等了,说明最小值在mid和high之间,mid尽量不要动,让high–即可,知道low和high相遇
class Solution {
public int minArray(int[] numbers) {
int low = 0;
int high = numbers.length - 1;
while (low < high) {
int mid = (low + high) >>> 1;
if (numbers[mid] > numbers[high]) {
low = mid + 1;
} else if (numbers[mid] < numbers[high]) {
high = mid;
} else {
high–;
}
}
return numbers[low];
}
}
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。
[[“a”,“b”,“c”,“e”],
[“s”,“f”,“c”,“s”],
[“a”,“d”,“e”,“e”]]
但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。
示例 1:
输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
输出:true
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解
- 这道题考的就是深度优先探索和回溯的思想, 你要知道这个思想就很简单,每累积相加字符,如果不一样就直接返回,还额外使用一个布尔数组,表示使用过的下标,但是记着这个数组回溯的
class Solution {
private int[][] next = new int[][] {{1,0}, {-1,0}, {0,1}, {0, -1}};
private boolean ret = false;
public boolean exist(char[][] board, String word) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
boolean[][] isUsed = new boolean[board.length][board[0].length];
dfs(isUsed, board, i, j, “” + board[i][j], word, 0);
if (ret) {
return ret;
}
}
}
return false;
}
private void dfs(boolean[][] isUsed, char[][] board, int row, int col, String temp, String word, int index) {
if (temp.charAt(index) != word.charAt(index)) {
return;
}
if (temp.equals(word)) {
ret = true;
return;
}
isUsed[row][col] = true;
for (int i = 0; i < 4; i++) {
int newRow = row + next[i][0];
int newCol = col + next[i][1];
if (newRow < 0 || newRow == board.length || newCol < 0 || newCol == board[0].length) continue;