1、1.BSTpublic class BSTMinLength public static void main(String args) TreeNode tNode11 = new TreeNode(10, null, null);TreeNode tNode12 = new TreeNode(50, null, null);TreeNode tNode13 = new TreeNode(5, null, null);TreeNode tNode14 = new TreeNode(30, null, null);TreeNode tNode21 = new TreeNode(30, tNode
2、11, tNode12);TreeNode tNode22 = new TreeNode(30, tNode13, tNode14);TreeNode tNodeRoot = new TreeNode(100, tNode21, tNode22);System.out.println(minlength(tNodeRoot);private static int minlength(TreeNode tNode)if (tNode != null) return getlength(tNode,0);return -1;private static int getlength(TreeNode
3、 tNode,int curLength) int minLeft=-1;int minRight=-1;if (tNode.leftNode!=null)minLeft=getlength(tNode.leftNode, curLength+tNode.value);if (tNode.rightNode!=null) minRight=getlength(tNode.rightNode, curLength+tNode.value);if (tNode.leftNode=null if (tNode.leftNode=null) return minRight;if (tNode.righ
4、tNode=null) return minLeft;return minLeftminRight? minRight:minLeft;class TreeNode int value;TreeNode leftNode;TreeNode rightNode;TreeNode(int value, TreeNode lefeNode, TreeNode rightNode) this.value = value;this.leftNode = lefeNode;this.rightNode = rightNode;2.lru#include using namespace std; int l
5、ruCountMiss(int max_cache_size, int *pages, int len) int count = 0; int i,j,k,n; bool flag = false; int *a = new intmax_cache_size; /初始化高速缓存数组 for(i = 0; i next;curr-next = prev;while(next != NULL)prev = curr;curr = next;next = next-next;curr-next = prev;return curr;elsereturn head;lnode *reverseLin
6、kedList(lnode *list)if(list)lnode *ori = list;lnode *half = list;lnode *prev = list;while(list list = list-next;half = half-next;if(half)half = half-next;if(list)prev-next = reverse(list);return ori;return list;4. SJFfloat waitingTimeSJF(int * requestTimes, int * durations,int n)int *flags = new int
7、n;float sums = 0;for(int i = 0 ;i=0 )flagscount+ = k;elseif(durationsk =0 nowtime = requestTimesj;int idx = flags0;int minreq = requestTimes flags0 ;int mindrus = durationsidx;if(count 1)for(int j = 1; j #include#includeconst int N=10000, M=100000;bool edgeNN; / 数组记录两点是否存在边bool visitN; / 标记该节点是否访问过b
8、ool DFS_check(int x, int y=-1)if (visitx)return false;visitx = true;int i;for (i=0;iusing namespace std;int isPath(int *grid, int m, int n);struct _TraversedNodeint x;int y;_TraversedNode *next;struct _Nodeint x;int y;int main(int argc, const char * argv)int *grid= new int*8;for(int i=0;ix=0;Travers
9、edNode-y=0;head=TraversedNode;p=TraversedNode;p-next=NULL;int count_node=0;int num_node=1;_Node *node=new _Noden+m;_Node *node_next=new _Noden+m;node0.x=0;node0.y=0;while(1)for(int i=0;ix=nodei.x+1)flag_down_success=false;elsep_check=p_check-next;if(flag_down_success)TraversedNode=new _TraversedNode
10、;TraversedNode-x=nodei.x+1;TraversedNode-y=nodei.y;p-next=TraversedNode;p=TraversedNode;p-next=NULL;node_nextcount_node.x=nodei.x+1;node_nextcount_node.y=nodei.y;count_node+;flag_down_success=true;if(nodei.x-1=0)if(gridnodei.x-1nodei.y!=0)if(gridnodei.x-1nodei.y=9)step+;coutx=nodei.x-1)flag_up_succe
11、ss=false;elsep_check=p_check-next;if(flag_up_success)TraversedNode=new _TraversedNode;TraversedNode-x=nodei.x-1;TraversedNode-y=nodei.y;p-next=TraversedNode;p=TraversedNode;p-next=NULL;node_nextcount_node.x=nodei.x-1;node_nextcount_node.y=nodei.y;count_node+;flag_up_success=true;if(nodei.y+1x=nodei.
12、x)flag_right_success=false;elsep_check=p_check-next;if(flag_right_success)TraversedNode=new _TraversedNode;TraversedNode-x=nodei.x;TraversedNode-y=nodei.y+1;p-next=TraversedNode;p=TraversedNode;p-next=NULL;node_nextcount_node.x=nodei.x;node_nextcount_node.y=nodei.y+1;count_node+;flag_right_success=t
13、rue;if(nodei.y-1=0)if(gridnodei.xnodei.y-1!=0)if(gridnodei.xnodei.y-1=9)step+;coutx=nodei.x)flag_left_success=false;elsep_check=p_check-next;if(flag_left_success)TraversedNode=new _TraversedNode;TraversedNode-x=nodei.x;TraversedNode-y=nodei.y-1;p-next=TraversedNode;p=TraversedNode;p-next=NULL;node_n
14、extcount_node.x=nodei.x;node_nextcount_node.y=nodei.y-1;count_node+;flag_left_success=true;if(count_node=0)cout1);term2=(byte)(term1);if(n=1)return 1;elsereturn0;8. #include using namespace std;void myPrint(int n)if(1 = n)cout n)myPrint(n);return 0;9 短作业优先调度算法(SJF)public class ShortJobFirst public s
15、tatic void main(String args) int requestTimes = 0, 2, 4, 5;int durations = 7, 4, 1, 4;float averageWaitingTime = ShortJobFirst.minWaitingTime(requestTimes, durations);System.out.println(averageWaitingTime);/* * param requestTimes 任务提交时间* param durations 任务服务时间* return*/public static float minWaiting
16、Time(int requestTimes, int durations) if(requestTimes = null | durations = null)return -1;if(requestTimes.length != durations.length)return -1;int n = requestTimes.length;int durationTimes = copyArray(durations); / 复制一份服务时间int startTimes = new intn; / 开始时间int endTime = new intn; / 结束时间int waitingTim
17、e = new intn; / 等待时间int cycleTime = new intn; / 周转时间/ 第一个执行任务的开始时间、结束时间、等待时间startTimes0 = requestTimes0;endTime0 = startTimes0 + durations0;waitingTime0 = 0;/* 核心代码 */int lastIndex = 0; / 上一次执行任务的索引int minIndex = 0; / 最短任务的索引for(int i = 1; i #include#define N 5typedef struct node int data;struct nod
18、e * next;SN;SN * creatlink ( int a )/tail 即所设置的尾指针,p 即程序中遍历整个链表的指针。SN * tail = NULL, * p;int i;for ( i = 0; i data = ai;if ( !tail ) /当 tail 为 NULL 是= 时,表示当前节点时所创建的第一个结点。tail-next = tail = p;else /直接将 p 结点插入在尾结点后成为新的尾结点即可。p-next = tail-next;tail = tail-next = p;return tail;void printlink ( SN * tail
19、 )SN * p = tail;printf(“);do printf ( “%d-“, p-next-data );p = p-next; while ( p != tail );printf ( “n“ );SN * insertnode ( SN * tail, int n )/形参 tail 即尾指针,s 即待插结点/设单向循环链表非空且数据域值升序有序,要求使得插入新结点之后,新链表依然升序有序SN * p = tail;SN * s;s=(SN *)malloc(sizeof(SN);s-data=n;do if ( p-next-data = s-data ) break;els
20、e p = p-next; while ( p != tail );if ( p = tail /接下来只需要进行插入算法即可s-next = p-next;p-next = s;return tail;int main ( void )SN * tail;int tmp;int aN = 3, 4, 6, 1, 2 ;/创建单向循环链表tail = creatlink ( a );/输出单向循环链表printlink ( tail );scanf(“%d“,insertnode ( tail, tmp );printlink ( tail );return 0;3.字符串格式化,去掉首尾的空
21、格,以及字符串中间连续的空格,但中间的只保留最后一个空格。比如:“ i love meituan “,格式化后:“ilove meituan“.我说一下我的思路:1. 用 string 的 substring 方法很轻松去掉首尾的空格。2,将去掉首尾空格的字符串转化为一个字符数组,方便对空格的查找。3,将找到的空格的下标记录在一个 arrayList(长度可变的优点等)中去。4,将第 1 步骤得到的字符串进行切割,最后一个空格前的字符串作为前缀字符串将(包括最后一个空格的下标)从最后一个下标开始到字符串结尾作为后缀字符串。5.将前缀字符串中的所有的空格使用 replaceAll 方法替换为没有“”。6.将 5 步骤得到的字符串与后缀字符串进行拼接。那么就得到了想要的字符串。代码实现如下;java view plain copy