(Daily algorithm) Leetcode -- Simplify Path (simple Path)
A Unix path is simplified. So that it does not change the path result, but removes useless characters in the middle.
Because the system is also segmented during execution, the most intuitive way is to use the stack to simplify the process /.. when /. ignored; at that time, only the stack was added.
Given an absolute path for a file (Unix-style), simplify it.
For example,
Path ="/home/", =>"/home"
Path ="/a/./b/../../c/", =>"/c"
It is not difficult to write the following code:
Class Solution {public: string simplifyPath (string path) {vector
Result; for (auto I = path. begin (); I! = Path. end ();) {++ I; auto j = find (I, path. end (), '/'); // query string dir = string (I, j); // construct the string if (! Dir. empty () & dir! = ".") // When it is //, the dir is null {if (dir = "..") {if (! Result. empty () result. pop_back ();} else result. push_back (dir);} I = j;} stringstream out; if (result. empty () out <"/"; else for (auto item: result) out <"/" <
There are three knowledge points for everyone to learn:
1) Use of the find Function
The algorithm does not directly manipulate the container, but traverses an element range specified by the two iterators.
find(vec.begin(), vec.end(), val)
Find the element val in the range of [vec. begin (), vec. end () (left-closed and right-open.
If the result is found, the iterator of the element is returned. If the result is not found, the second parameter is returned.
2) construct a string using the iterator
String dir = string (I, j)
Initialize dir as a copy of the elements between the iterator I and j (left closed and right open ).
Class initialization through the = operator is the same as dir (string (I, j.
3) knowledge points of stringstream
Allow reading and writing data to the string stream, out. str () -- returns the string copy saved by out; out. str (s) -- copies string s to out, and returns void
# Include
# Include
Using namespace std; int main () {string line, word; while (getline (cin, line) {stringstream stream (line); cout <
<
> Word) {cout <
<
Input: shanghai no1 school 19
Output: shanghai no1 school 19
Shanghai
No1
School
19
# Include
# Include
Using namespace std; int main () {int val1 = 512, val2 = 1024; stringstream ss; ss <"val1:" <
<<<"val2: "<<
> Dump> a> dump> B; cout <
Note that when parsing an object from stringstream, The Delimiter is space and enter key.
Output: val1: 512
Val2: 1024
512 1024
Part 1 in bold: reads int type into ss and changes it to string type
The second part of the black text: extract and save as int type. Of course, if a and B are declared to be of the string type, the two literal constants are saved as strings.
Stringstream does not actively release memory (perhaps to improve efficiency), but if you want to use the same stream in your program and read and write a large amount of data repeatedly, it will cause a large amount of memory consumption, at this time, the buffer needs to be cleared in due time (using stream. str (""))
<<<"val2:>
<
<