I. Huawei interview questions-1-N sorting
There are n natural numbers (1--n) of varying sizes. Sort them in ascending order.
Program Algorithm required: the time complexity is O (n), and the space complexity is O (1 ).
I did not notice the first half of the sentence.
Algorithm: N natural numbers ranging from 1 to 1 ~ N, after sorting is completed, it must be 1 ~ N. So we can traverse it at a time and encounter a [I]! If I is set to I, a [I] And a [A [I] are exchanged.
Void sort (int A [], int N)
{
Int I;
Int t;/* Temporary Variable: space complexity O (1 )*/
For (I = 1; I <n + 1; I ++)/* time complexity O (n )*/
{
While (A [I]! = I)
{
T = A [A [I];
A [A [I] = A [I]; // arrange an element
A [I] = T;
}
}
}
2. Search for the nth node of the linked list in one Traversal
For an interview question, James and Chen saw and told me the answer. It is required to find the nth node in the linked list through a traversal. The linked list may be quite large and can use auxiliary space. However, the number of auxiliary space must be fixed and cannot be related to N.
Algorithm idea: two pointers, the first starting, n steps apart, and the second starting. Then, step at the same time until the first pointer reaches the end.
Struct inode {
Int value;
Inode * next;
};
Inode * getresult (inode * head, int N)
{
Inode * pfirst;
Inode * initialize cond;
Pfirst = head;
Int counter;
For (counter = 0; counter <n; counter ++ ){
Pfirst = pfirst-> next;
}
Required cond = head;
While (pfirst! = NULL ){
Pfirst = pfirst-> next;
Consumer cond = Consumer cond-> next;
}
Return response cond;
}
Iii. VC ++ Study Notes
1. Convert date to string:
Coledatetime WW;
WW = coledatetime: getcurrenttime ();
Afxmessagebox (WW. Format ("% Y-% m-% d % H: % m: % s "));
2. Convert string to date:
Coledatetime DT;
DT. parsedatetime ("08:08:08 ");
3. Resource file
Resource file name: XXX. Rc. The main files to be included are resource. h and afxres. h.
4. When no prompt is displayed in the VC development environment:
Delete the NCB file in the directory and then open it.
5. Use _ variant_t to retrieve database data:
_ Variant_t Ibb;
Ibb = (_ variant_t) RS-> getcollect ("INTA ");
If (Ibb. VT! = Vt_null)
{
M_ B = Ibb. lval;
}
6. method for getting the record set field values at ordinary times:
(Lpctstr) (_ bstr_t) RS-> getcollect ("datea ")
7. domodal () can return two results: idok and idcancel, both of which are int type, respectively: 1, 2. Returned by using enddialog (idok.
8. Generally, the database connection information is stored in the app. Afxgetapp () is very important, for example;
Cado2app * Mapp = (cado2app *) afxgetapp ();
Map-> Conn-> execute (SQL, null, ad1_text );
9. declare_dyncreate (class name) and implement_dyncreate (Class Name, base class name) Allow the classes inherited by cobject to be dynamically created when the program is running.
10. declare_dynamic (class name), implement_dynamic (Class Name, base class name) can get information about this class at runtime
11. declare_serial (class name) and implement_serial (Class Name, base class name, 0) generate necessary C ++ title code for a serializable cobject derived class
12. method to obtain the document: cmainframe * pframe = (cmainframe *) afxgetmainwnd ();
Cpclientdoc * pdoc = (cpclientdoc *) pframe-> getactivedocument ();
13. method for obtaining the view: cmainframe * pframe = (cmainframe *) afxgetmainwnd ();
Myview = (cpclientview *) pframe-> getactiveview ();
14. If you want to reference global variables or global methods, you must introduce the extern name in the current class;