剑指offer编程题-1
2018-06-18 01:37:02来源:未知 阅读 ()
1.二维数组中的查找
题目描述
利用二维数组由上到下,由左到右递增的规律,那么选取右上角a[row][col]与target进行比较,如果等于就直接找到,当target小于元素a[row][col]时,那么target必定在元素a所在行的左边,即col--;当target大于元素a[row][col]时,那么target必定在元素a所在列的下边,即row++;public class Solution {
public boolean Find(int target, int [][] array) { int row=0; int col=array[0].length-1; while(row<=array.length-1&&col>=0){ if(target==array[row][col]) return true; else if(target>array[row][col]) row++; else col--; } return false; } }
注意:
对于一个二维数组:
int[][] arr = { {2,3,4}, {4,5,6}, {7,8,9} };
int rows = i.length;//行数
int columns = i[0].length;//列数
[[]]这样一个数组代表行数是1,列数时0.
2.替换空格
题目描述
public class Solution { public String replaceSpace(StringBuffer str) { StringBuffer strCopy= new StringBuffer(); for(int i=0 ; i<str.length();i++){ String c = String.valueOf(str.charAt(i)); if(c.equals(" ")){ strCopy.append("%20"); }else { strCopy.append(c); } } return strCopy.toString(); } }
注意:
Java中字符数组、String类、StringBuffer三者的相互转换
3.从尾到头打印链表
题目描述
思路:遇到先进后出的情况,可以采取两种思路:
1.利用堆栈的思想
2.利用递归的思想
下面是采用堆栈的思想:
/** * 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) { Stack<ListNode> stack = new Stack<ListNode>(); while(listNode!=null){ stack.push(listNode); listNode = listNode.next; } ArrayList<Integer> list = new ArrayList<Integer>(); while(!stack.isEmpty()){ list.add(stack.pop().val); } return list; } }
下面是采用递归的思想 :
import java.util.ArrayList; public class Solution { ArrayList<Integer> list = new ArrayList<Integer>(); public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { if(listNode!=null){ this.printListFromTailToHead(listNode.next); list.add(listNode.val); } return list; } }
4.重建二叉树
题目描述
import java.util.*; /** * 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.length == 0||in.length == 0){ return null; } TreeNode node = new TreeNode(pre[0]); for(int i = 0; i < in.length; i++){ if(pre[0] == in[i]){ node.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(in, 0, i)); node.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(in, i+1,in.length)); break;
} } return node; } }
这是形式最为简洁的方法,首先前序的第一个数字肯定是根节点,然后根据这个元素在中序中找到根节点位置i,
那么中序左边0,i是左子树,右边i+1,in.length是右子树。
并且也能在前序中确定左子树是从1,i+1的元素,右子树是i+1,pre.length。(注意这里是左边界包含,右边界不包含)
然后从原数组中分别复制前序和中序的左子树和右子树,递归调用即可。直到pre.length或者in.length(当然这里他俩本来就一样)为0表示递归到叶子节点。
注:Arrays.copyOfRange()方法
startPre>endPre||startIn>endIn为表示已经递归完成
public class Solution { public TreeNode reConstructBinaryTree(int [] pre,int [] in) { TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1); return root; } //前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6} private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) { if(startPre>endPre||startIn>endIn) return null; TreeNode root=new TreeNode(pre[startPre]); for(int i=startIn;i<=endIn;i++) if(in[i]==pre[startPre]){ root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1); root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn); break; } return root; } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 因为命名被diss无数次。简单聊聊编程最头疼的事情之一:命名 2020-06-10
- Java3个编程题整理 2020-06-09
- (易忘篇)java基础编程难点4 2020-06-08
- 终于拿到了美团offer了,没有辜负了这三个月的努力啊 2020-06-06
- (易忘篇)java基础编程难点3 2020-06-05
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
