面试题3 二维数组中的查找 Leetcode--74 Search a 2D Matrix

 /*Java
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row. For example, Consider the following matrix: [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
] Given target = 3, return true.
本题最简单的方法循环遍历行与列 但是时间复杂度较高。
*/
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
boolean find = false;
int rows,cols;
if (matrix == null || matrix.length==0) {
rows = 0;
cols = 0;
} else {
rows = matrix.length;
cols = matrix[0].length;
} if (matrix != null && rows > 0 && cols > 0) {
int row = 0;
int col = cols-1;
while(row<rows && col>=0){
if(matrix[row][col]==target){
find=true;
break;
} else if(matrix[row][col]>target){
col--;
}else{
row++;
}
}
} return find;
}
public static void main(String[] args) {
int[][] matrix1 = { { 1, 3, 5, 7 }, { 10, 11, 16, 20 }, { 23, 30, 34, 50 } }; int[][] matrix = {};
int target = 55;
SearchA2DMatrix cc = new SearchA2DMatrix();
boolean find = cc.searchMatrix(matrix, target);
System.out.println(find);
}
}

面试题5 从尾到头打印链表 -------Leetcode 206题 Reverse Linked List

package easy;

import java.util.Stack;
import easy.ListNode; public class L206ReverseLinkedList {
/*
* Reverse a singly linked list. 先读入,后输出。典型的先进后出,可用栈来实现。
*/ /**
* Definition for singly-linked list.
*
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/ public ListNode reverseList(ListNode head) {
if (head == null)
return head;
Stack<Integer> myStack = new Stack<Integer>();
ListNode pNode = head;
while (pNode != null) {
myStack.push(pNode.val);
pNode = pNode.next;
}
int size = myStack.size();
ListNode newHead = new ListNode(myStack.peek());
myStack.pop();
if (size == 1)
return newHead;
ListNode node = new ListNode(myStack.peek());
newHead.next = node;
myStack.pop();
while (!myStack.empty()) {
node.next = new ListNode(myStack.peek());
myStack.pop();
node = node.next;
}
while (newHead != null) {
System.out.println(newHead.val);
newHead = newHead.next;
}
return newHead; }
public static void main(String[] args) {
ListNode head1 = new ListNode(1);
ListNode head2 = new ListNode(2);
ListNode head3 = new ListNode(3);
ListNode head4 = new ListNode(4);
head1.next = head2;
head2.next = head3;
head3.next = head4;
head4.next = null;
L206ReverseLinkedList cc = new L206ReverseLinkedList();
cc.reverseList(head1); }
}

面试题6:重建二叉树,由二叉树的前序遍历和中序遍历重建二叉树——Leetcode 105.Construct Binary Tree from Preorder and Inorder Traversal

package medium;
//面试题6
public class L105ConstructBinaryTreefromPreorderandInorderTraversal {
/*
* 根据前序遍历与中序遍历重建一个二叉树
*/
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || inorder == null) {
return null;
}
int length = preorder.length;
if(length==0){
TreeNode root =null;
return root;
}
//System.out.println(length);
int startPre = 0;
int endPre = length - 1;
int startIn = 0;
int endIn = length - 1;
return buildTreeCore(preorder, inorder, startPre, endPre, startIn, endIn);
} public TreeNode buildTreeCore(int[] preorder, int[] inorder, int startPre, int endPre, int startIn, int endIn) {
// 前序遍历的第一个结点为根节点。
int rootValue=preorder[startPre];
TreeNode root = new TreeNode(rootValue);
root.left = root.right = null;
System.out.println("前序遍历第一个节点"+preorder[startPre]);
if (preorder[startPre] == preorder[endPre] )
if(inorder[startIn] == inorder[endIn]) {
return root;
} else {
System.out.println("非法输入");
}
//在中序遍历中找到根节点
int rootInorderIndex=startIn;
while(startIn<=endIn && inorder[rootInorderIndex]!=rootValue){
rootInorderIndex++;
}
int leftLen=rootInorderIndex-startIn;
int leftPreEnd=startPre+leftLen;
if(leftLen>0){
root.left=buildTreeCore(preorder,inorder,startPre+1,leftPreEnd,startIn,rootInorderIndex-1);
}
if(leftLen<endPre-startPre){
root.right=buildTreeCore(preorder,inorder,leftPreEnd+1,endPre,rootInorderIndex+1,endIn);
}
return root;
}
public static void main(String[] args) {
L105ConstructBinaryTreefromPreorderandInorderTraversal cc= new L105ConstructBinaryTreefromPreorderandInorderTraversal();
int[] preorder1={1,2,4,7,3,5,6,8};
int[] inorder1={4,7,2,1,5,3,8,6};
int[] preorder={1,2};
int[] inorder={2,1};
cc.buildTree(preorder, inorder);
} }

面试题6-2:重建二叉树,由二叉树的中序遍历和后序遍历重建二叉树——Leetcode 106.Construct Binary Tree from Inorder and Postorder Traversal

package medium;

public class L106ConstructBinaryTreefromInorderandPostorderTraversal {
/*
* 根据前序遍历与中序遍历重建一个二叉树
*/
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (postorder == null || inorder == null) {
return null;
}
int length = inorder.length;
if (length == 0) {
TreeNode root = null;
return root;
}
// System.out.println(length);
int startPost = 0;
int endPost = length - 1;
int startIn = 0;
int endIn = length - 1; return buildTreeCore(inorder, postorder, startIn, endIn, startPost, endPost);
} public TreeNode buildTreeCore(int[] inorder, int[] postorder, int startIn, int endIn, int startPost, int endPost) {
// 后序遍历的最后一个结点为根节点。
int rootValue = postorder[endPost];
TreeNode root = new TreeNode(rootValue);
root.left = root.right = null;
if (postorder[startPost] == postorder[endPost]) {
if (inorder[startIn] == inorder[endIn]) {
return root;
} else {
System.out.println("非法输入");
}
}
// 在中序遍历中找到根节点
int rootInorderIndex = startIn;
while (startIn <= endIn && inorder[rootInorderIndex] != rootValue) {
rootInorderIndex++;
}
int leftLen = rootInorderIndex - startIn;
int leftPostEnd = startPost + leftLen-1;
if (leftLen > 0) {
root.left = buildTreeCore(inorder, postorder, startIn, rootInorderIndex - 1, startPost, leftPostEnd);
}
if (leftLen < endPost - startPost) {
root.right = buildTreeCore(inorder, postorder, rootInorderIndex + 1, endIn, leftPostEnd + 1, endPost - 1); }
return root;
} public static void main(String[] args) {
L106ConstructBinaryTreefromInorderandPostorderTraversal cc = new L106ConstructBinaryTreefromInorderandPostorderTraversal();
int[] inorder = { 4, 7, 2, 1, 5, 3, 8, 6 };
int[] postorder={ 7, 4, 2, 5, 8, 6, 3, 1 };
cc.buildTree( inorder,postorder);
} }

面试题7 用两个栈,实现队列-----Leetcode 232题 Implement Queue using Stacks

package easy;

import java.util.Stack;

public class L232ImplementQueueUsingStacks {
Stack<Integer> queue = new Stack(); public void push(int x) {
Stack<Integer> stack = new Stack();
while (!queue.empty()) {
stack.push(queue.pop());
}
queue.push(x);
while (!stack.empty()) {
queue.push(stack.pop());
}
} public int pop() {
return queue.pop();
} public int peek() {
return queue.peek();
} public boolean empty() {
return queue.empty();
} public static void main(String[] args) {
MyQueue obj = new MyQueue();
obj.push(2);
obj.push(1);
obj.push(3);
obj.push(4);
int param_2 = obj.pop();
int param_3 = obj.peek();
boolean param_4 = obj.empty();
System.out.println("pop:"+param_2);
System.out.println("peek:"+param_3);
System.out.println("empty:"+param_4);
}
}

面试题7-2 用两个队列,实现栈-----L225题 Implement Stacks using Queue

package easy;

import java.util.LinkedList;
import java.util.Queue; public class L225_ImplementStackUsingQueues {
/*
* 用两个栈实现队列 先入先出
*
* Implement the following operations of a stack using queues.
*/
Queue<Integer> queue1 = new LinkedList<Integer>();
Queue<Integer> queue2 = new LinkedList<Integer>();
      // push(x) -- Push element x onto stack.
public void push(int x) {
if (queue1.isEmpty() && queue2.isEmpty()) {
queue1.add(x);
} else if (!queue1.isEmpty()) {
queue1.add(x);
} else {
queue2.add(x);
}
} // pop() -- Removes the element on top of the stack.
public int pop() {
int size1 = queue1.size();
int size2 = queue2.size();
if (size1 > 0) {
while (size1 > 1) {
queue2.add(queue1.poll());
size1--;
}
return queue1.poll();
} else if (size2 > 0) {
while (size2 > 1) {
queue1.add(queue2.poll());
size2--;
}
return queue2.poll();
} else {
System.out.println("栈为空");
}
return 0;
} // top() -- Get the top element.
public int top() {
int size1 = queue1.size();
int size2 = queue2.size();
int x=0;
if (size1 > 0) {
while (size1 > 1) {
queue2.add(queue1.poll());
size1--;
}
x=queue1.peek();
queue2.add(queue1.poll());
} else if (size2 > 0) {
while (size2 > 1) {
queue1.add(queue2.poll());
size2--;
}
x=queue2.peek();
queue1.add(queue2.poll());
} else {
System.out.println("栈为空");
}
return x;
} // empty() -- Return whether the stack is empty.
public boolean empty() {
int size = queue1.size() + queue2.size();
return size > 0 ? false : true;
} public static void main(String[] args) {
//["MyStack","push","push","push","top",
//"pop","top","pop","top","empty","pop","empty"]
L225_ImplementStackUsingQueues obj = new L225_ImplementStackUsingQueues();
obj.push(1);
System.out.println("top1:" +obj.top());
obj.push(2);
System.out.println("top2:" +obj.top());
obj.push(3);
System.out.println("top3:" +obj.top());
System.out.println("pop:" +obj.pop() );
System.out.println("top:" +obj.top());
System.out.println("pop:" +obj.pop() );
System.out.println("top:" +obj.top());
System.out.println("empty:" + obj.empty());
System.out.println("pop:" +obj.pop() );
System.out.println("empty:" + obj.empty());
}
}

面试题8:旋转数组的最小数字—leetcode 153题 Find Minimum in Rotated Sorted Array

class Solution {
public int findMin(int[] nums) {
int min=nums[0];
int len=nums.length;
int low=0;
int high=len-1;
int mid=(low+high)/2;
if(nums[low]<nums[high]){
return nums[low];
}
while(low<=high){
if(high-low==1 || high==low){
System.out.println("min:"+min);
return nums[high]<=nums[high] ? nums[high]:nums[low];
}
if(nums[mid]>nums[high]){
min=nums[high];
low=mid;
}else{
min=nums[mid];
high=mid;
}
mid=(low+high)/2;
}
return min; }
}

面试题9:菲波那切数列

package jianzhiOffer;

public class I9Fibonacci {
public int calFi1(int n) {
// 递归
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return calFi1(n - 1) + calFi1(n - 2);
} public int calFi2(int n) {
// 循环
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
int one = 0;
int two = 1;
int sum = 0;
for (int i = 2; i <= n; i++) {
sum = one + two;
one = two;
two = sum;
}
return sum;
} public static void main(String[] args) {
I9Fibonacci cc = new I9Fibonacci();
int sum1 = cc.calFi1(3);
int sum2 = cc.calFi2(3);
System.out.println(sum1 + ":" + sum2);
}
}

面试题10:二进制中1的个数——剑指OfferP78 ~~~L191题 Number of 1 Bits

package easy;

public class L191NumberOf1Bits {
// you need to treat n as an unsigned value
//将一个整数减去一个1,这个整数的二进制变化是:最右面的1变为0,这一位之后的所有位变为1.
//如1100 减去一为1011.
//减1之后的整数与原来的整数做与运算,会把原整数最右面一位变为0.
//1011 & 1100=1000
public int hammingWeight(int n) {
int number=0;
while(n!=0){
number++;
n=(n-1)&n;
}
return number;
}
//解法2 较慢的解法,如果n&1结果为1这表示n的最后一位为1,count++;之后将1左移,判断前面位。
public int solu2(int n){
int number=0;
int flag=1;
while(flag!=0){
if((n & flag) !=0){
number++;
}
flag=flag<<1;
}
return number;
}
public static void main(String[] args) {
L191NumberOf1Bits cc= new L191NumberOf1Bits();
int number=cc.hammingWeight(2);
System.out.println(number);
}
}

面试题11:数值的整数次方——Leetcde 50.Pow(x, n)  详解

面试题14:调整数组顺序使奇数位于偶数前面——Leetcode 328. Odd Even Linked List 详解

面试题15:链表中倒数第k个结点~~~~Leetcode 19. Remove Nth Node From End of List.详解

面试题16:反转链表~~~~~Leetcode 92. Reverse Linked List II   详解

            Leetcode 206.Reverse Linked List 详解

面试题17:合并两个排序的链表~~~~~Leetcode 21. Merge Two Sorted Lists 详解

面试题20:顺时针打印矩阵~~~~Leetcode 54.Spiral Matrix 详解

              59题 Spiral MatrixII 详解

面试题24:二叉搜索树的后序遍历序列~~牛客网详解

面试题25:二叉树中和为某一值的路径~~~~Leetcode 112 Path Sum 详解

                   Leetcode 113 PathSumII 详解

面试题29:数组中出现次数超过一半的数字~~~~~Leetcode 169.Majority Element 详解

面试题33:把数组排成最小的数~~~~~Leetcode 179.Largest Number 详解

面试题37:两个链表中的第一个公共节点~~~~~Leetcode 160. Intersection of Two Linked Lists 详解

面试题39:二叉树的深度~~~~~Leetcode 110. Balanced Binary Tree 详解

面试题50:把字符串变成整数(atoi函数)~~~~Leetcode 8.String to Integer (atoi)  详解

面试题51:树中两个结点的最低公共祖先~~~~~~Leetcode 235. Lowest Common Ancestor of a Binary Search Tree详解

                       Leetcode 236.Lowest Common Ancestor of a Binary Tree 详解

【剑指offer】Java实现(持续更新中)的更多相关文章

  1. 《剑指Offer》笔记(更新中)

    这几天为了找工作开始看<剑指offer>,到现在也大概浏览一遍了,前两天看作者博客中提到九度OJ,就去看了一下,发现上面有书上的题目,就想可以自己写代码练习一下,而不仅仅是看解题思路,毕竟 ...

  2. 剑指Offer——Java实现栈和队列的互模拟操作

    剑指Offer--Java实现栈和队列的互模拟操作 栈模拟队列   题目:JAVA实现用两个栈来实现一个队列,完成队列的Push和Pop操作.队列中的元素为int类型.   思路:其实就是把队列正常入 ...

  3. 剑指Offer - 九度1373 - 整数中1出现的次数(从1到n整数中1出现的次数)

    剑指Offer - 九度1373 - 整数中1出现的次数(从1到n整数中1出现的次数)2014-02-05 23:03 题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直 ...

  4. 剑指Offer - 九度1348 - 数组中的逆序对

    剑指Offer - 九度1348 - 数组中的逆序对2014-01-30 23:19 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个 ...

  5. 剑指Offer - 九度1517 - 链表中倒数第k个结点

    剑指Offer - 九度1517 - 链表中倒数第k个结点2013-11-30 02:57 题目描述: 输入一个链表,输出该链表中倒数第k个结点.(hint: 请务必使用链表.) 输入: 输入可能包含 ...

  6. 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字

    剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...

  7. 剑指Offer - 九度1351 - 数组中只出现一次的数字

    剑指Offer - 九度1351 - 数组中只出现一次的数字2013-11-23 01:23 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. ...

  8. 剑指offer:二维数组中的查找

    目录 题目 解题思路 具体代码 题目 题目链接 剑指offer:二维数组中的查找 题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺 ...

  9. 剑指 Offer 04. 二维数组中的查找 (思维)

    剑指 Offer 04. 二维数组中的查找 题目链接 本题的解法是从矩阵的右上角开始寻找目标值. 根据矩阵的元素分布特性, 当目标值大于当前位置的值时将row行号++,因为此时目标值一定位于当前行的下 ...

  10. 【剑指Offer】孩子们的游戏(圆圈中最后剩下的数) 解题报告(Python)

    [剑指Offer]孩子们的游戏(圆圈中最后剩下的数) 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-in ...

随机推荐

  1. 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl

    基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl 0. 导语 在嵌入式的道路上寻寻觅觅很久,进入嵌入式这个行业也有几年的时间了,从2011年后 ...

  2. BurpSuite—-Repeater模块(中继器)

    一.简介 Burp Repeater 是一个手动修改并补发个别 HTTP 请求,并分析他们的响应的工具.它最大的用途就是和其他 Burp Suite 工具结合起来.你可以从目标站点地图,从 Burp ...

  3. 改脚本之dbscaner

    默认的DBscaner只是用了ipy模块支持一个段的解析,但是我想让他加载脚本进行检测 所以,直接看 def __init__(self, target, thread): self.target = ...

  4. 111. Climbing Stairs 【LintCode easy】

    Description You are climbing a stair case. It takes n steps to reach to the top. Each time you can e ...

  5. eclipse安装hadoop插件

    我想还有很多人没有听说过ZModem协议,更不知道有rz/sz这样方便的工具. 好东西不敢独享.以下给出我知道的一点皮毛. 下面一段是从SecureCRT的帮助中copy的: ZModem is a ...

  6. 20155204 《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    实验一 Java开发环境的熟悉 一.实验内容及步骤 1.使用JDK编译.运行简单的java程序 步骤一:在linux界面下运行终端 步骤二:在终端中打开待编译文件的文件夹 步骤三:使用 javac 文 ...

  7. 20145226夏艺华 《Java程序设计》实验报告四

    实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试 了解Android组件.布局管理器的使用 掌握Android中事件处理机制 Andro ...

  8. C#基础之Equals和Dispose

    1.equal()和运算符==的区别 由于C#中有值类型和引用类型,那么相等也分为值相等和引用相等.先来看一个值类型简单的例子,顺便也写了string类型的比较. static void Main(s ...

  9. spring_cloud多个微服务访问时偶发forward_error问题

    1.问题: 最近在做SpringBoot项目的时候,有多个分开的微服务,偶发forward error 问题 2.猜想: 个人理解为服务跳转错误,可能本身没找到目标服务,或者目标服务损坏 3.解决: ...

  10. 在tomcat5中发布项目时,用IP地址+端口不能访问项目,而用localhost加端口时可以访问成功

    最近在开发项目中,遇到的一个问题是: 在 tomcat中发布一个web项目,但是发布成功后,只能用http://localhost:8080/fm访问项目,不能用 http://127.0.0.1:8 ...