DescriptionGiven a polynomial, output the result of its simplification.InputA string that contains only the polynomial of the letter x, without parentheses and fractions, with no extra spaces.OutputA string that, after simplifying the polynomial, outputs the items in the order of the times from large to small.Input SampleX^3+3*x^4-2*x^3+1-xOutput Sample3*x^4-x^3-x+1HintEach coefficient The annoying analog code is as follows:#include Yzoi Easy Round 2_ Simplification (Simplify.c/
title :Given a singly linked list l: l0→l1→ ... →ln-1→lN,Reorder it to: l0→ln→l1→ln-1→l2→L n-2→ ...You must does this in-place without altering the nodes ' values.For example,Given {1,2,3,4} , reorder it to {1,4,2,3} .Code :/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: voidReorderlist (ListNode *head) { if(!head)return; ListNode Dummy (-1); Dummy.next=Head; ListNode*P1 = d
title :Given a list, rotate the list to the right by K -places, where K is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2 ,Return 4->5->1->2->3->NULL .Code :/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode* Rotateright (listnode* head,intk) {if(!head | |! (Head->next))returnHead; ListNode*p =Head; intLen =1; for(;p->next;++len,p=p->next) {} ListNode*end =p; K= k%
title :You are given a n x n 2D matrix representing an image.Rotate the image by degrees (clockwise).Follow up:Could do this in-place?Code :classSolution { Public: voidRotate (vectorint> > matrix) { ConstUnsignedintLen =matrix.size (); if(Len 2)return; for(introw =0; Row row) { for(intCol =0; Col 1-row; ++Col) {Std::swap (Matrix[row][col], Matrix[len-1-col][len-1-row]); } } for(introw =0; Row 2; ++row) { for(intCol =0; Col Col
)]++; //substring occur more than times in words if(Found[s.substr (I,len_w)]>ori[s.substr (i,len_w)]) {found.clear (); Match_num=0; Begin++; I=begin; Continue; } I= i+Len_w; Match_num++; //cout //match all substrings in words and push back a starting indices if(match_num==words.size ()) {Ret.push_back (i-end); Found.clear (); Begin++; I=begin; Match_num=0; } } //Not match
In CPP, a static variable in a class does not belong to any one instance.Below, the two methods are used to prove the situation.1.classstudent{Private: stringname; intAge ; intheight; Static intMoney ; Public: Student (stringInputName,intinputage): Name (InputName), age (Inputage) {}voiddisplaystudent () {coutEndl; coutEndl; }};intMain () {FStream fs ("Out.dat"Ios:: out|ios::binary); Student STU1 ("He", -); Fs.write (reinterpret_castChar*> (AMP;STU
Topic:Given a binary tree, return the bottom-up level order traversal of its nodes ' values. (ie, from the left-to-right, the level by level from the leaf to root).For example:Given binary Tree {3,9,20,#,#,15,7} , 3 / 9 / 7Return its bottom-up level order traversal as:[ [15,7], [9,20], [3]]Code:/** Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right ; * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Pu
"Endl; A -V.erase (V.end ()-5, V.end ()); -cout "After erase:"Endl; thecout "size of v is"Endl; -cout "capacity of V is"Endl; - -vectorint>(v). Swap (v); +cout "After swap (shrink to fit):"Endl; -cout "size of v is"Endl; +cout "capacity of V is"Endl; A atvectorint>(). Swap (v); -cout "After swap (clear):"Endl; -cout "size of v is"Endl; -cout "capacity of V is"Endl; - - return 0; in}The output is:Initial:size of v is Tencapacity of V is TenAfter erase:size of v is 5capacity of V i
Topic:Write a function to find the longest common prefix string amongst an array of strings.Code:classSolution { Public: stringLongestcommonprefix (vectorstring>STRs) { if(strs.size () = =0)return ""; STD::stringTmp_str = strs[0]; for(size_t i =1; I i) {size_t J=0; for(; J j)if(Tmp_str[j]!=strs[i][j]) Break; Tmp_str= Tmp_str.substr (0, J); } returnTmp_str; }};TipsSpace complexity O (N1), Time complexity O (n1+n2 ...)Each string string in the STRs is compared sequential
=""; } Else{Tmp_part+=Path[i]; } } if(Ret.size () >1 ret[ret.size ()-1]=="/") Ret.erase (Ret.end ()-1); for(size_t i =0; I Ret[i]; returnresult; }};TipsThe main idea is the data structure of stack stacks.1. Use "/" as the interval judgment symbol2. Tmp_part is a string part between two "/" (Note that the input path may not end with "/", the workaround is to manually fill in one after the path)3. If the Tmp_part is "..", two elements are out of the stack (pro
is two:A. What was the previous character (that is, Prechar in the code)B. Dot,sign,exp,digit, have you ever seenThe advantage of this code logic is that if you look for two core judgment logic and tinker with the two core logic, you can constantly brush the test case until AC.If there are similar problems in the future, the condition conditions are very many, but the input conditions are relatively fixed, and they need to be judged according to the serialized input criteria.You can set a numbe
thatPart_a = Ia==a.rend ()? 0: *ia-' 0 '; (int)Result.insert (Result.begin (), sum%2+ ' 0 '); (char)The same is the addition and subtraction between int and char, why is the returned type different?The guess should be this: the arithmetic is converted to ASCII code of the value of the addition and subtraction, and finally if the requirement is an int to maintain the int type, need to be char to convert to char type.C + + Primer Plus (Fourth edition) 45~47 explains this conversion.' 0 ' can be u
title :Suppose a sorted array is rotated on some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).You is given a target value to search. If found in the array is return its index, otherwise return-1.Assume no duplicate exists in the array.Code :classSolution { Public: intSearchintA[],intNinttarget) { intBegin =0; intEnd = N1; while(Begin! =end) { if(begin+1==end) { if(A[begin]==target)returnbegin; if(A[end]==target)returnend;
title :Follow up for "Remove duplicates":What if duplicates is allowed at the most twice?For example,Given sorted Array A = [1,1,1,2,2,3] ,Your function should return length = 5 , and A is now [1,1,2,2,3] .Code :classSolution { Public: intRemoveDuplicates (intA[],intN) {if(N 2)returnN; intindex =2; for(intI=2; i) { if(a[index-2]!=A[i]) {A[index++] =A[i]; } } returnindex; }};Tips:1. Index always points to the next location to insert the element2. Determine whet
Memory leaks are a common problem in C + + programs, especially in service class programs, where it is difficult to see memory leaks through code when there are too many system modules or complex logic.Valgrind is an open source, detection of C + + program memory leak effective tool, compile with the-G option to navigate to the line of code, but also check the ' Wild pointer ', check whether malloc and free match and other functions.Reference:1. Memory leak checking tool in several C + + program
Buffoon::update();This shows, which update () is a member function of the Buffoon class, so update () has class scope. Buffoon::update () is the qualified name of the function, update (), on the other hand, is the unqualified name, WH Ich could only being used in class scopeExample of class method implementation file:Stock00.cpp-Implementing the Stock class//version 00#include ) any function with a definition of the class declaration becomes an inlin
Prepare the environment.
Install mingw and set the system path.
Mingw. The gun programming environment in windows.
Function of system variables-the search path of executable files. In this way, you can directly enter g ++ in cmd to call D: \ Program Files (x86) \ codeblocks \ mingw \ bin \ gw.mongomongo.exe.
The default working directory after CMD is opened is c: \ Users \ administrator.
G ++ and GCC.
GCC is only responsible for compiling and not linking to the standard library. Ther
component is 0, and the U component is 1. You can imagine ). The following code contains comments. In this way, this dynamic triangle band is generated.
This special effect can be customized, so you need to set texture coordinates, as mentioned above. The general idea is to re-set the V component based on the number of vertices in the triangle band after each update. The U component is relatively simple. If it is not 1, it is 0. (This U is equivalent to X, V is equivalent to Y, and the range is
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.