1.二维数组中的查找
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

public class Solution {
public boolean Find(int target, int [][] array) {
if(array == null||array.length==0) return false;
int rowIdx = 0,colIdx = array[0].length-1;
while(rowIdx<array.length&&colIdx>=0){
if(array[rowIdx][colIdx] == target)
return true;
else if(target>array[rowIdx][colIdx])
rowIdx++;
else if(target<array[rowIdx][colIdx])
colIdx--;
}
return false;
}
}

  

2.替换空格
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuilder sb = new StringBuilder();
for(int i = 0;i<str.length();i++){
if(str.charAt(i)==' '){
sb.append("%20");
}
else
sb.append(str.charAt(i));
}
return sb.toString();
}
}

3.从尾到头打印链表
输入一个链表,从尾到头打印链表每个节点的值。

/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> result = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
if(listNode == null) return result;
while(listNode != null){
stack.push(listNode.val);
listNode = listNode.next;
}
while(!stack.isEmpty())
result.add(stack.pop());
return result;
}
}

 

4.重建二叉树
使用递归
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
if (pre == null || in == null || pre.length != in.length) return null;
return reConstructBinaryTreeCore(pre, 0, pre.length - 1, in, 0, in.length - 1);
}
public TreeNode reConstructBinaryTreeCore(int[] pre, int preStartIdx, int preEndIdx, int[] in, int inStartIdx, int inEndIdx) {
TreeNode node = new TreeNode(pre[preStartIdx]);
if (preStartIdx == preEndIdx)
if (inStartIdx != inEndIdx || pre[preStartIdx] != in[inStartIdx])
System.out.println("Invalid input.");
int i = 0;
while (pre[preStartIdx] != in[inStartIdx + i])
i++;
if (i == 0)//证明没有左子树
node.left = null;
else
node.left = reConstructBinaryTreeCore(pre, preStartIdx + 1, preStartIdx + i, in, inStartIdx, inStartIdx + i - 1);
if (inStartIdx + i == inEndIdx)//证明没有右子树
node.right = null;
else
node.right = reConstructBinaryTreeCore(pre, preStartIdx + i + 1, preEndIdx, in, inStartIdx + i + 1, inEndIdx);
return node;
}
}

  

5.用两个栈实现队列
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() throws Exception {
if(stack1.isEmpty()&&stack2.isEmpty())
throw new Exception("Queue is empty.");
if(!stack2.isEmpty())
return stack2.pop();
while(!stack1.isEmpty())
stack2.push(stack1.pop());
return stack2.pop();
}
}

  

6.旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array == null||array.length == 0)return 0;
int idx1 = 0,idx2 = array.length-1;
//如果不能进入while循环,则证明第一个元素小于最后一个元素,而且数组为非递减排序,最小值即为首位。
while(array[idx1]>=array[idx2]){
//第一个指针指向前半段递增序列的末尾,第二个指针指向后半段递增序列的首位。
if(idx2-idx1==1)return array[idx2];
//二分法查找临界点
int mid = (idx1+idx2)/2;
//考虑特例:{1,0,1,1,1}
if(array[idx1] == array[idx2]&& array[mid] == array[idx1]){
for(int i = idx1;i<=idx2;i++)
if(array[i]<array[mid])
return array[i];
//特例:{1,1,1,1,1,1,1}
return array[mid];
}
//更新指针,直至idx2-idx1==1;
if(array[mid]>=array[idx1])
idx1 = mid;
else if(array[mid]<=array[idx2])
idx2 = mid;
}
//此时数组为递增排列,第一个元素最小
return array[0];
}
}

  

7.斐波那契数列(这个数列从第3项开始,每一项都等于前两项之和)
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
n<=39

public class Solution {
public int Fibonacci(int n) {
if(n<1) return 0;
int[] fibonacci = new int[2];
fibonacci[0] = 1;
fibonacci[1] = 1;
n-=2;
while(n>0){
int temp = fibonacci[0]+fibonacci[1];
fibonacci[0] = fibonacci[1];
fibonacci[1] = temp;
n--;
}
return fibonacci[1];
}
}

  

8.跳台阶(动态规划)
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

public class Solution {
public int JumpFloor(int target) {
if(target < 1) return 0;
int[] DP = new int[3];
DP[0] = 1;
DP[1] = 2;
DP[2] = DP[0]+DP[1];
if(target<=3)
return DP[target-1];
for(int i =4;i<=target;i++){
DP[0] = DP[1];
DP[1] = DP[2];
DP[2] = DP[0]+DP[1];
}
return DP[2];
}
}

  

9.矩形覆盖
我们可以用21的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

public class Solution {
public int RectCover(int target) {
if(target<1) return 0;
int[] DP = new int[3];
DP[0] = 1;
DP[1] = 2;
DP[2] = DP[1]+DP[0];
if(target<4)
return DP[target-1];
for(int i = 4;i<=target;i++){
int temp = DP[1]+DP[2];
DP[0] = DP[1];
DP[1] = DP[2];
DP[2] = temp;
}
return DP[2];
}
}

10.二进制中1的个数
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

public class Solution {
public int NumberOf1(int n) {
int count = 0;
while(n!=0){
count+=n&1;
n=n>>>1;
}
return count;
}
}

剑指offer(1)的更多相关文章

  1. 剑指Offer面试题:1.实现Singleton模式

    说来惭愧,自己在毕业之前就该好好看看<剑指Offer>这本书的,但是各种原因就是没看,也因此错过了很多机会,后悔莫及.但是后悔是没用的,现在趁还有余力,把这本书好好看一遍,并通过C#通通实 ...

  2. 剑指Offer面试题:14.链表的倒数第k个节点

    PS:这是一道出境率极高的题目,记得去年参加校园招聘时我看到了3次,但是每次写的都不完善. 一.题目:链表的倒数第k个节点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题 ...

  3. 《剑指offer》面试题12:打印1到最大的n位数

    面试题12:打印1到最大的n位数 剑指offer题目12,题目如下 输入数字n,按顺序打印出1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的三位数999 方法一 和面试题11< ...

  4. 《剑指offer》面试题11: 数值的整数次方

    面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...

  5. 剑指 Offer 题目汇总索引

    剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格              ...

  6. 面试题目——《剑指Offer》

    1.把一个字符串转换成整数——<剑指Offer>P29 2.求链表中的倒数第k个结点——<剑指Offer>P30 3.实现Singleton模式——<剑指Offer> ...

  7. 剑指offer习题集2

    1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...

  8. 剑指offer习题集1

    1.打印二叉树 程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊 这里左右子树,要注意是node->left,结果写成root->left vector<int> ...

  9. 剑指Offer:面试题20——顺时针打印矩阵(java实现)

    题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数 字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...

  10. 牛客网上的剑指offer题目

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...

随机推荐

  1. Go实战--也许最快的Go语言Web框架kataras/iris初识(basic认证、Markdown、YAML、Json)

    ris自称是Go语言中所有Web框架最快的,它的特点如下: 1.聚焦高性能 2.健壮的静态路由支持和通配符子域名支持. 3.视图系统支持超过5以上模板 4.支持定制事件的高可扩展性Websocket ...

  2. Centos7安装vsftpd

    1.安装vsftpd yum install vsftpd 2.添加一个ftp用户,一个不能登录系统用户,只用来登录ftp服务,这里如果没设置用户目录.默认是在home下. useradd ftpac ...

  3. 【git学习笔记】

    一.查看git的配置文件 1.在项目下,有一个.git的隐藏文件 2.config为git的配置文件 3.查看config :branch表示分支,此配置文件表示当前有两个分支NNU和master,一 ...

  4. web(二)html

    html编写规范 在输入开始标签时同时输入结束标签,以防丢失标签 保证缩紧格式(一个tab键) 主动添加注释(快捷键 选中后 Ctrl+Shift+/) Html的调试 开发者工具(快捷键F12)是前 ...

  5. 使用apidoc 生成Restful web Api文档——新手问题与解决方法

    使用apidoc工具来给项目做接口文档,不仅有合理的源码注释,还可以生成对应的文档.是给源码写备注的一个极佳实践. 工具名称:apiDoc Git地址:https://github.com/apido ...

  6. 阿里druid数据库连接池配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. pip3 install scrap报错

    mac系统 pip3 install scrapy 失败 No local packages or working download links found for incremental>=1 ...

  8. nginx防盗链、nginx访问控制、nginx解析php相关配制、nginx代理

    1.nginx防盗链编辑:vim /usr/local/nginx/conf/vhost/test.com.conf写入: location ~* ^.+\.(gif|jpg|png|swf|flv| ...

  9. 计算x

    如果x的x次幂结果为10(参见[图1.png]),你能计算出x的近似值吗? 显然,这个值是介于2和3之间的一个数字. 请把x的值计算到小数后6位(四舍五入),并填写这个小数值. 注意:只填写一个小数, ...

  10. html css input定位 文本框阴影 灰色不可编辑

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...