首页 > 编程知识 正文

01背包问题递归算法,01背包问题算法

时间:2023-05-03 20:34:25 阅读:63675 作者:4203

二叉搜索树(用于中继的双向链表publicstatictreenodetransfertree (treenode root ) { TreeNode lastNode=null; lastnode=act node (根,lastnode ); TreeNode head=lastNode; wile (头!=null head.left!=null}{head=head.left; }返回头; }公共静态ht dsj,TreeNode lastNode () if ) root==null ) { return root; (if ) root.left!=null } { lastnode=act node (root.left,lastnode ); } root.left=lastNode; if(lastnode!=null}{lastnode.right=root; } lastNode=root; if(root.right!=null } { lastnode=act node (root.right,lastnode ); } return lastNode; (二叉树正反向序列化) null直接进入队列的publictreenodedeserialize (字符串数据) if ) data==null||data.length )==0) { return null; (} String独特的蜻蜓vals=data.split ),); int len=vals.length; int独特的蜻蜓nums=new int[len]; TreeNode独特的蜻蜓ansNodes=new TreeNode[len]; for(intI=0; i len; I () if (i0 ) ) { nums[i]=nums[i - 1]; }if(vals[I].equals('null ) ) { ansNodes[i]=null; nums[i]; } else { ans nodes [ I ]=new treenode (integer.parseint ) vals[I] ); }for(intI=0; i len; I ) if(ansnodes[I]==null ) { continue; } ans nodes [ I ].left=ans nodes [2* (I-nums [ I ] ) cxdxwz] ); ans nodes [ I ].right=ans nodes [2* (I-nums [ I ] ) 2; }返回ans nodes [0]; }二叉树层次遍历:队列,首先root,然后whilepublicstaticarraylistarraylistarraylistintegereverylevel (treenode root ) arraylistaraylistintion } queuetreenodequeue=newlinkedlisttreenode (; arraylistintegertemplist=newarraylistinteger (; queue.add (根; int nowLevCnt=1; int nextLevCnt=0; while (! queue.isEmpty () ({ TreeNode curr=queue.remove ); nowLevCnt--; templist.add(curr.val; if(curr.left!=null}{queue.add(curr.left ); nextLevCnt; (if ) curr.right!=null}{queue.add(curr.right ); nextLevCnt; }if(nowlevcnt==0) { nowLevCnt=nextLevCnt; nextLevCnt = 0; finalAns.add((ArrayList<Integer>) tempList.clone()); tempList.clear(); } } return finalAns;} 前中后序:栈,两个while public static void preOrder(TreeNode root){ if(root==null){ return ; } Stack<TreeNode> stack=new Stack<TreeNode>(); while(root!=null||!stack.isEmpty()){ while(root!=null){ //前序:根 stack.push(root); root=root.left; } if(!stack.isEmpty()){ root=stack.pop(); //中序:根 root=root.right; } if(root.right!=null&&map.get(root)!=2){ stack.push(root); map.put(root,2); root=root.right; } else{ //后序:根 root=null; } }//while结束} 判断单链表是否存在环 public Node hasCircle(Node head){ Node fast=head; Node slow=head; Node encounter=null; while(fast!=null && fast.next!=null){ fast=fast.next.next; slow=slow.next; if(fast==slow){ encounter=fast; return true; } } return false;}public Node findEntry(Node head,Node encounter){ Node p1=head; Node p2=encounter; while(p1!=p2){ p1=p1.next; p2=p2.next; }} 二分搜索 public int binarySearch(int 独特的蜻蜓nums,int target,int left,int right){
if(left>right){ int middle=(left+right)/2;
if(target==nums[middle]){
return middle;
}
else if(target>nums[middle]){
return binarySearch(nums,target,middle+1,right);
}
else{
return binarySearch(nums,target,left,middle-1);
}
} 归并排序 public void mSort(int独特的蜻蜓 nums, int left, int right){ if (left < right){ int middle = (left + right) / 2; mSort(nums, left, middle); mSort(nums, middle cxdxwz, right); doMerge(nums, left, middle, right); }}public void doMerge(int独特的蜻蜓 nums, int left, int middle, int right){ int独特的蜻蜓 temp = new int[right - left cxdxwz]; int i = left,int j = middle cxdxwz; int m = middle,int n = right; int k = 0; while (i <= m && j <= n){ if (nums[i] < nums[j]){ temp[k++] = nums[i++]; } else{ temp[k++] = nums[j++]; } } while (i <= m){ temp[k++] = nums[i++]; } while (j <= n){ temp[k++] = nums[j++]; } for (i = left; i <= right; i++){ nums[i] = temp[i - left]; }} 快速排序 public void qSort(int独特的蜻蜓 nums, int left, int right){ if (left < right){ int index = getIndex(nums, left, right); qSort(nums, left, index - 1); qSort(nums, index cxdxwz, right); }}public int getIndex(int独特的蜻蜓 nums, int left, int right){ int pivot = nums[left]; while (left < right){ while (left < right && nums[right] >= pivot){ right--; } nums[left] = nums[right]; while (left < right && nums[left] < pivot){ left++; } nums[right] = nums[left]; }//结束外层while nums[left] = pivot; return left;} 冒泡、插入 public void bSort(int独特的蜻蜓 nums){ for (int i = 0; i < nums.length; i++){ for (int j = 0; j < nums.length - 1 - i; j++){ if (nums[j] > nums[j cxdxwz]){ swap(nums, j, j cxdxwz); } }//内层for }//外层for}public void insertSort(int独特的蜻蜓 nums){ int len = nums.length; for (int i = 0; i < len; i++){ int temp = nums[i]; int j = i - 1; while (j >= 0 && nums[j] > temp){ nums[j cxdxwz] = nums[j]; j--; } nums[j cxdxwz] = temp; }} 堆排序 public void heapSort(int独特的蜻蜓 nums){ buildHeap(nums); for (int i=0; i<nums.length; i++){ swap(nums[0], nums[heapSize-1]); heapSize--; adjustHeap(nums,0); }}public void buildHeap(int独特的蜻蜓 nums){ for (int i=heapSize/2-1;i>=0;i--){ adjustHeap(nums,i); }}private void adjustHeap(int独特的蜻蜓 nums, int index){ int left = 2*index+1; int right = left cxdxwz; int largest=index; if (left < heapSize && nums[left]>nums[index]){ largest=left; } if (right <heapSize && nums[right] > nums[largest]){ largest = right; } if (largest != index){ swap(nums[index],nums[largest]); adjustHeap(nums,largest); }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。