剑指offer-java(4)
来源:互联网 发布:python 延时 编辑:程序博客网 时间:2024/06/05 16:29
实现两个函数,分别用来序列化和反序列化二叉树
public class Solution{ String Serialize(TreeNode root){ if(root==null) return ""; StringBuilder sb=new StringBuilder(); Serialize2(root,sb); return sb.toString(); } void Serialize2(TreeNode root,StringBuilder sb){ if(root==null){ sb.append("#,"); return ; } sb.append(root.val); sb.append(','); Serialize2(root.left,sb); Serialize2(root.right,sb); } int index=-1; TreeNode Deserialize(String str){ if(str.length()==0) return null; String[] strs=str.split(","); return Deserialize2(strs); } TreeNode Deserialize2(String[] strs){ index++; if(!strs[index].equals("#")){ TreeNode root=new TreeNode(0); root.val=Integer.parseInt(strs[index]); root.left=Deserialize2(strs); root.right=Deserialize2(strs); return root; } return null; }}
二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
public class Solution{ int k; TreeNode KthNode(TreeNode pRoot,int k){ this.k=k; return helper(pRoot); } private TreeNode helper(TreeNode pRoot){ TreeNode temp=null; if(pRoot!=null){ if((temp=helper(pRoot.left))!=null)return temp; if(--k==0)return pRoot; if((temp=helper(pRoot.right))!=null)return temp; } return null; }}
中位数
import java.util.*;public class Solution{ ArrayList<Integer> list=new ArrayList<>(); public void Insert(Integer num){ list.add(num); Collections.sort(list); } public Double getMedian(){ int mid=list.size()/2; if(list.size()%2==0){ Integer a=list.get(mid); Integer b=list.get(mid-1); double s=((double)a+(double)b)/2; return s; }else return (double)list.get(mid); }}
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。
import java.util.*;public class Solution{ public ArrayList<Integer> maxInWindow(int[] num,int size){ ArrayList<Integer> list=new ArrayList<Integer>(); if(num==null||size<=0) return list; int len=num.length; ArrayList<Integer> temp=null; if(len<size) return list; else{ for(int i=0;i<num.length-size+1;i++){ temp=new ArrayList<>(); for(int j=i;j<size+i;j++) temp.add(num[j]); Collections.sort(temp); list.add(temp.get(temp.size()-1)); } } return list; }}
矩阵中的路径
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串”bcced”的路径,但是矩阵中不包含”abcb”路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
public class Solution { public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { if(rows<0||cols<0||str.length==0||matrix.length!=rows*cols||matrix.length==0||str.length>matrix.length) return false; int[] mark=new int[matrix.length]; for(int i=0;i<rows;i++) for(int j=0;j<cols;j++) if(hasPath(matrix, rows, cols, str, i, j, 0, mark)) return true; return false; } public boolean hasPath(char[] matrix, int rows, int cols, char[] str,int i,int j,int num,int[] mark) { int Index=i*cols+j; if(i<0||i>=rows||j<0||j>=cols||matrix[Index]!=str[num]||mark[Index]==1) return false; if(num==str.length-1) return true; mark[Index]=1; if(hasPath(matrix, rows, cols, str, i, j+1, num+1, mark) ||hasPath(matrix, rows, cols, str, i, j-1, num+1, mark) ||hasPath(matrix, rows, cols, str, i+1, j, num+1, mark) ||hasPath(matrix, rows, cols, str, i-1, j, num+1, mark)) return true; mark[Index]=0; return false; }}
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
public class Solution { public int movingCount(int threshold, int rows, int cols) { int[][] flag=new int[rows][cols]; return process(0,0,rows,cols, flag, threshold); } private int process (int i,int j, int rows ,int cols ,int[][] flag, int threshold){ if(i<0 || i>=rows || j<0 || j>=cols || numSum(i)+numSum(j) > threshold || flag[i][j]==1) return 0; flag[i][j]=1; return process(i-1,j,rows, cols,flag,threshold)+ process(i+1,j,rows,cols,flag,threshold)+ process(i,j+1,rows,cols,flag,threshold)+ process(i,j-1,rows,cols,flag,threshold)+1; } private int numSum(int i){ int sum=0; while(i>0){ int tmp=i%10; sum+=tmp; i=i/10; } return sum; }}
- 剑指offer-java(4)
- 剑指offer-java(1)
- 剑指offer-java(2)
- 剑指offer-java(3)
- 剑指Offer java代码
- 《剑指offer》:java
- 剑指Offer-java problem2
- 剑指Offer-java problem3
- 剑指Offer-java problem4
- 剑指offer ---java版本
- JAVA版本-剑指Offer
- 剑指offer 算法java
- 剑指offer实践(Java)
- (剑指offer)JAVA实现
- 《剑指offer》笔记(java)
- 剑指Offer(java答案)
- 剑指offer(java版)
- 剑指Offer Java版 链表
- 一、svg的四种使用方式
- 不同平台之间的 DB2 数据库迁移一例
- mongodb各种导出导入数据的方法
- iOS应用架构谈(一):架构设计的方法论
- [INS-30131] 执行安装程序验证所需的初始设置失败
- 剑指offer-java(4)
- 在CentOS6.5中安装与配置J2EE环境
- serialize序列化和json
- 关于超视距控制无人机的资料
- 第12周—阅读程序,写出运行结果。 (2)
- java中notify()和notifyall的区别
- 仿微博 @好友 @xx 高亮 变色
- [技术前沿]Hybrid App
- swift dictionary 是否存在key