03 - 数组中重复的数字
找出数组中重复的数字。
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
限制:
2 <= n <= 100000
解法一 计数器思路
维护一个新的数组 用下标代表元素 遍历数组元素 将对应下表的计数器增加
如果大于1 即为重复的数据
缺点:空间占有率高
代码实现
class Solution {
public int findRepeatNumber(int[] nums) {
int[] a = new int[nums.length];
for(int i=0;i<nums.length;i++){
a[nums[i]]++;
if(a[nums[i]]>1){
return nums[i];
}
}
return 0;
}
}
解法二 hashSet 去重
将元素放入set中 如果有重复的就返回
public int findRepeatNumber(int[] nums) {
for(int i=0;i<nums.length;i++){
Set<Integer> a1 = new HashSet<>();
if(a1.contains(nums[i])){
return nums[i];
}else {
a1.add(nums[i]);
}
}
return 0;
}
04 二维数组中的查找
在一个 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。
限制:
0 <= n <= 1000
解题思路
从右上角开始比对 比他大就往下找,比他小就往左找
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int l = matrix[0].length;
int h = matrix.length;
int i = l-1;
int j = 0;
while(i>=0 && j<h){
if(matrix[j][i] == target){
return true;
}
if(matrix[j][i]<target){
j++;
}else
if(matrix[j][i]>target){
i--;
}
}
return false;
}
}
05 替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
思路
遍历整个数组 遇到空格就替换
class Solution {
public String replaceSpace(String s) {
int length = s.length();
char[] chars = s.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
String replace = "%20";
for (int i = 0; i < length; i++) {
char aChar = chars[i];
if (aChar == ' '){
stringBuilder.append(replace);
}
else {
stringBuilder.append(aChar);
}
}
return stringBuilder.toString();
}
}
06 从头到尾打印链表
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
**输入:**head = [1,3,2]
输出:[2,3,1]
思路
用栈存放数据,最后倒序出来放进数组中
代码实现
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> result = new Stack<>();
while (head!=null){
result.push(head.val);
head = head.next;
}
int[] sum = new int[result.size()];
int size = result.size();
for (int i = 0; i < size; i++) {
sum[i]=result.pop();
}
return sum;
}
}
// 注意 不能使用result.size进for循环 因为pop会影响result.size
07 重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如,给出
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
3
/
9 20
/
15 7
思路
递归解决
前序遍历第一个为root节点
根据root节点在中序遍历中找到值
然后确定左子树个数,再递归生成左子树
代码实现
class Solution {
private Map<Integer,Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
map = new HashMap<Integer,Integer>();
int length = preorder.length;
for(int i=0;i<length;i++){
map.put(inorder[i],i);
}
return buildTree(preorder,inorder,0,length-1,0,length-1);
}
private TreeNode buildTree(int[] preorder,int[] inorder,
int pleft,int pright,int ileft,int iright){
if(pleft>pright){
return null;
}
int rootindex = pleft;
TreeNode root = new TreeNode(preorder[rootindex]);
int iroot = map.get(preorder[rootindex]);
int lsize = iroot-ileft;
root.left = buildTree(preorder,inorder,pleft+1,pleft+lsize,ileft,iroot-1);
root.right = buildTree(preorder,inorder,pleft+lsize+1,pright,iroot+1,iright);
return root;
}
}
剑指09
剑指 Offer 09. 用两个栈实现队列
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
示例 1:
输入:
[“CQueue”,”appendTail”,”deleteHead”,”deleteHead”]
[[],[3],[],[]]
输出:[null,null,3,-1]
思路1 用a栈模拟 每次都移到b栈再移回来
class CQueue {
Stack<Integer> a;
Stack<Integer> b;
public CQueue() {
a = new Stack<Integer>();
b = new Stack<Integer>();
}
public void appendTail(int value) {
while(a.size()!=0){
b.push(a.pop());
}
a.push(value);
while(b.size()!=0){
a.push(b.pop());
}
}
public int deleteHead() {
if (a.size()==0){
return -1;
}else {
return a.pop();
}
}
}
思路2 用b栈为删除栈,如果b站为空就移动数据到b栈
class CQueue {
Stack<Integer> a;
Stack<Integer> b;
public CQueue() {
a = new Stack<Integer>();
b = new Stack<Integer>();
}
public void appendTail(int value) {
a.push(value);
}
public int deleteHead() {
if(b.size()==0){
while(a.size()!=0){
b.push(a.pop());
}
}
if (b.size()==0) return -1;
return b.pop();
}
}
10 斐波拉契数列
写一个函数,输入 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。
思路1 递归解决问题 用hashmap提升计算效率
代码实现
class Solution {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
public int fib(int n) {
return dfs(n);
}
private int dfs(int n) {
if (n == 0 || n == 1) {
return n;
}
if (map.containsKey(n)) {
return map.get(n);
}
int a = dfs(n - 1) % 1000000007;
map.put(n - 1, a);
int b = dfs(n - 2) % 1000000007;
map.put(n - 2, b);
map.put(n, (a + b) % 1000000007);
return map.get(n);
}
}
思路2 迭代
public int fib(int n) {
if(n==0||n==1) return 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;
}