Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3->4->NULL rotate 2 steps to the right: 4->5->1->2->3->NULL
classSol61 { public: ListNode* rotateRight(ListNode* head, int k){ if (head==nullptr) return head; //1. get the length of list ListNode* curr = head; int length=1; while(curr->next){ curr = curr->next; length++; } // 注意这里, int newK = k%length; if (newK == 0) return head;
//2. init ptrs ListNode* dhead = new ListNode(-1, head); ListNode* end = dhead; for (int i=0;i<newK;i++){ end=end->next; } ListNode* pre = dhead; ListNode* newHead = head; while(end->next){ end=end->next; pre=pre->next; newHead=newHead->next; } //3. move ptrs pre->next = nullptr; end->next = head;
//4. clean up delete dhead; dhead=nullptr;
return newHead; } };
328 奇数号结点链表,偶数号结点链表
描述:
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
public: int sum=0; intrangeSumBST(TreeNode* root, constint L, cosnt int R){ dfs(root, L,R); return sum; } voiddfs(TreeNode* root, int L, int R){ if (root==nullptr) return; else{ if (root->val>=L && root->val<=R) sum += root->val; if (root->val>L) dfs(root->left, L,R); if (root->val<R) dfs(root->right, L,R); } } };
// partition 操作 intpartitionOpe(vector<int>& nums, int l, int r){ swap(nums[l], nums[rand()%(r-l+1)+l]); int p = nums[l]; int j = l; for (int i=j+1;i<=r;i++){ if (nums[i]<p){ swap(nums[i], nums[++j]); } } swap(nums[l], nums[j]); return j; }
// 查找 intkthItem(vector<int>& nums, int k){ int n = nums.size(); int l=0, r=n-1, index=0; index = partitionOpe(nums,l,r); while(true){ // 对右边进行partition if (n-index>k){ l = index+1; index = partitionOpe(nums,l,r); } // 对左边进行partition elseif(n-index<k){ r = index-1; index = partitionOpe(nums,l,r); } // 直到找到 elseif(n-index==k){ return nums[index]; } } }
# 解析 I0610 04:53:36.096751 13919 solver.cpp:102] Creating training net from net file: /media/junhui/DATA/caffe_workspace/my_linearReggresion/mylr.prototxt # 这两行前面解释过了,区分哪些层用于TRAIN,哪些层用于TEST I0610 04:53:36.097002 13919 net.cpp:296] The NetState phase (0) differed from the phase (1) specified by a rule in layer mnist I0610 04:53:36.097012 13919 net.cpp:296] The NetState phase (0) differed from the phase (1) specified by a rule in layer accuracy I0610 04:53:36.097085 13919 net.cpp:53] Initializing net from parameters:
找到对应位置:
1 2 3 4 5 6 7 8 9 10
void Solver<Dtype>::InitTrainNet() { ... // param_是一个SolverParameter对象,如果mylr.prototxt文件中定义了Net结构,则如下 if (param_.has_net()) { LOG_IF(INFO, Caffe::root_solver()) // 打印Log << "Creating training net from net file: " << param_.net(); // 解析mylr.prototxt中内容,将其内容存入 NetParameter 对象 net_param 中。 // 转换过程由ProtoBuffer工具完成的。 ReadNetParamsFromTextFileOrDie(param_.net(), &net_param); }
如此就将磁盘中的prototxt描述转换到内存。
将内存中模型存储到磁盘
当需要保存当前模型和快照时:
1 2
I0610 04:53:38.909629 13919 solver.cpp:464] Snapshotting to binary proto file my_lr_iter_5000.caffemodel I0610 04:53:38.910568 13919 sgd_solver.cpp:284] Snapshotting solver state to binary proto file my_lr_iter_5000.solverstate