剑指offer题目1-10
面试题3:二维数组中的查找
public class Solution {
public boolean Find(int [][] array,int target) {
boolean isFound = false;
int m = array.length;
int n = array[0].length;
if(m>0 && n>0 ){
int row = 0;
int col = n-1;
while(row<m && col>=0){
if(array[row][col] > target){
col = col - 1;
}else if(array[row][col] <target){
row = row + 1;
}else{
isFound = true;
break;
}
}
}
return isFound;
}
}
面试题4:替换空格
public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuffer res = new StringBuffer("");
char[] strChar = str.toString().toCharArray();
int len = strChar.length;
for(int i=0;i<len;i++){
if(strChar[i] == ' '){
res.append("%20");
}else{
res.append(strChar[i]);
}
}
return res.toString();
}
}
面试题5:从尾到头打印链表
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack = new Stack<Integer>();
while(listNode != null){
stack.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> res = new ArrayList<Integer>();
while(!stack.isEmpty()){
res.add(stack.peek());
stack.pop();
}
return res;
}
}
面试题6:重建二叉树
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre.length != in.length) return null;
if(pre.length == 0 || pre==null || in == null){
return null;
}
return construct(pre,0,pre.length-1,in,0,in.length-1);
}
public TreeNode construct(int[] pre,int startPre,int endPre,int[] in,int startIn,int endIn){
int rootVal = pre[startPre];
TreeNode root = new TreeNode(rootVal);
root.left = null;
root.right= null;
if(startPre == endPre && startIn == endIn){
return root;
}
int posIn = startIn;
while(posIn <= endIn && in[posIn] != rootVal) posIn++;
int leftLen = posIn - startIn ;
if(leftLen>0){
root.left = construct(pre,startPre+1 , startPre+leftLen,in,startIn,posIn-1);
}
if(leftLen<endPre-startPre){
root.right = construct(pre,startPre+leftLen+1,endPre,in,posIn+1,endIn);
}
return root;
}
}
面试题7:用两个栈实现队列
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
stack1.push(node);
}
public int pop() {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
}
面试题8:旋转数组的最小数字
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length == 0) return 0;
int l = 0;
int r = array.length-1;
while(l<r){
int mid = l + (r - l)/2;
if(array[mid]>array[r] ){
l = mid + 1;
}else if(array[mid]<array[r]){
r = mid ;
}else{
r--;
}
}
return array[l];
}
}
面试题9:斐波那契数列
public class Solution {
public int Fibonacci(int n) {
int twoBeforeF = 1;
int oneBeforeF = 1;
int thisF = 0;
if (n==0) return 0;
if (n==1) return 1;
if (n==2) return 1;
if(n>2){
for(int i=3;i<=n;i++){
thisF = twoBeforeF + oneBeforeF;
twoBeforeF = oneBeforeF;
oneBeforeF = thisF;
}
}
return thisF;
}
}
面试题9:跳台阶
public class Solution {
public int JumpFloor(int target) {
if(target == 0 ) return 0;
if(target == 1 ) return 1;
if(target == 2 ) return 2;
int twoBefore = 1;
int oneBefore = 2;
int res = 0;
for(int i=3;i<=target;i++){
res = twoBefore + oneBefore;
twoBefore = oneBefore;
oneBefore = res;
}
return res;
}
}
面试题9:变态跳台阶
public class Solution {
public int JumpFloorII(int target) {
if(target == 0) return 0;
int[] arr = new int[target+1];
if(target>0){
arr[0] = 0;
for(int i=1;i<=target;i++){
int tmpSum = 1;
for(int j=0;j<i;j++){
tmpSum += arr[j];
}
arr[i] = tmpSum;
}
}
return arr[target];
}
}
面试题10:二进制中1的个数
public class Solution {
public int NumberOf1(int n) {
int count = 0;
while(n!=0){
count++;
n = n & (n-1);
}
return count;
}
}
剑指offer题目1-10的更多相关文章
- 代码题 — 剑指offer题目、总结
剑指offer题目总结: https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...
- 剑指offer题目系列三(链表相关题目)
本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...
- 再来五道剑指offer题目
再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- 剑指 Offer 题目汇总索引
剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格 ...
- 剑指offer题目java实现
Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...
- 剑指offer题目系列一
本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...
- 【剑指Offer】剑指offer题目汇总
本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...
- 牛客网上的剑指offer题目
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...
- 剑指offer题目系列二
本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...
- 剑指offer(leetcode 10.) 正则表达式匹配
这题一年前就做过,当时刚开始刷leetcode,提交了几十次过不去,就放那没管了.今天剑指offer又遇到这题,终于做出来了,用的dp. class Solution { public: bool i ...
随机推荐
- CSS id 选择器
id 选择器 id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式. id 选择器以 "#" 来定义. 下面的两个 id 选择器,第一个可以定义元素的颜色为红色,第二 ...
- .Net操作Excel
先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用. .Net导出代码: /// <summ ...
- C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)
1. 主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...
- Openstack Neutron OVS ARP Responder
ARP – Why do we need it? In any environment, be it the physical data-center, your home, or a virtual ...
- HackerRank "Kruskal (MST): Really Special Subtree"
Kruskal Algorithm is based on Union-Find - quite intuitive. #include <vector> #include <ios ...
- 【MySQL】InnoDB日志机制深入分析
版权声明:尊重博主劳动成果,欢迎转载,转载请注明出处 --爱技术的华仔 Log & Checkpoint Innodb的事务日志是指Redo log,简称Log,保存在日志文件ib_logfi ...
- OGNL语言
OGNL 一.概述 以下内容摘自Ognl的官网: OGNL stands for Ob ...
- Objective-C语法之KVO的使用
简介: 上篇我们讲到了KVC,这篇我们学习KVO,全名为:Key Value Observing,直译为:基于键值的观察者. 那它有什么用呢?KVO主要用于视图交互方面,比如界面的某些数据变化了,界面 ...
- 【Android 系统开发】Android JNI/NDK (三) 之 JNIEnv 解析
jni.h文件 : 了解 JNI 需要配合 jni.h 文件, jni.h 是 Google NDK 中的一个文件, 位置是 $/Android-ndk-r9d/platforms/android-1 ...
- outlook找不到文件Outlook.pst 如何启动
首先注明:这种情况在控制面板-邮件 无法打开的情况下可以使用以下命令打开设置界面 解决方法: 1. 开始 –> 运行 –> cmd 2. 在DOS下,用CD 切换到 Outlook.ex ...