[Data Structure and Algorithm] Learning Record Collection (01) Arrays and Strings

  • This collection is the study record of the "C++ Data Structure and Algorithm" course that the author self-study on the b station. It aims to record the important learning points, thinking content and part of the code, so that it can be read by yourself later, and it can also bring some information to other readers. refer to
  • The content is based on the author’s own understanding or perception, and may contain inappropriate or incorrect contents.
  • System environment: Win10, Visual Studio 2019

Table of contents

Algorithmic complexity

time complexity

space complexity

string matching

Array

Linear storage containers in STL

Operation 


Algorithmic complexity

Algorithm complexity is evaluated from two dimensions: time and space . Time complexity evaluates the time required to execute the algorithm and is easier to understand. Space complexity evaluates how much memory space is required to execute the algorithm. If the requirements are equally met, the smaller the better for both.

time complexity

Use Big O notation to describe

Divided into several common orders, constant, linear, square, logarithmic, linear logarithmic, etc.

Comparison of different order algorithms - Quote from watching animation to easily understand time complexity (1)_Senior Brother Wu learns algorithms

 

Best time complexity/worst time complexity

Some algorithms have a certain degree of randomness and can be calculated in one go if you are lucky. For example, if you find "X" in a large series of data, and the first one happens to be "X" during the search, the time complexity is O(1). Of course, Correspondingly, there will also be worst-case scenarios, for example, some sorting algorithms will spend more time than usual when processing data in a specific arrangement, etc.

average time complexity

Because there are best and worst cases as above, an average time complexity is generated that is weighted according to the probability of occurrence, and the time complexity of the algorithm can be described more objectively.

space complexity

The description of the memory space occupied by the algorithm is divided into fixed and variable parts. The fixed part includes the code itself and its defined variables, etc., which is basically determined after the code is compiled. There is also a variable part, which is generated when the code is running. Dynamically allocated space, as well as the space required for the recursive stack, etc., are spaces derived from the algorithm and will change as the data changes.

In reality, time complexity and space complexity often have a confrontational relationship. That is, wanting good time complexity may mean paying corresponding efforts in space complexity, and vice versa. Therefore, for a project, based on various factors, Optimizing time and space complexity is a very deep knowledge


string matching

Simple Brute-Force algorithm and Rabin-Karp algorithm can match strings

Use if(!*str) to determine whether the string is empty

KMP algorithm and its preprocessing of pattern strings, offset, time complexity is O(n), only related to the main string

Array

Stack & Heap, Stack's first-in-last-out, and Heap require programmers to manage (delete) themselves to prevent memory leaks. The heap is used to store dynamic data, such as elem = new a[3]; the stack is used to store static data, such as a1[3] = {3, 5, 7}; etc.

Two-dimensional & three-dimensional array, RowMajorOrder (C/C++/Java), ColumnMajorOrder (fortran)

HashTable (a very important data structure). When encountering problems such as unique or counting the number of times an element appears in a set of elements, think about whether you can use HashTable.

Linear storage containers in STL

vector,deque,string等,

(1) Dynamic storage space, sequential logical relationship

(2) Random access available

(3) The efficiency of insertion and deletion is low, and deque is slightly better because it can insert and delete in both directions.

(4) When there are few elements, the space utilization rate is low

Operation 

Given a string, it is required to replace the spaces in the string with "%20" and output

class Solution2 {
public:
	string replaceSpace(string s) {
		int count = 0, len = s.size();
		// 统计空格数量
		// 此处用到了C++11语法,利用char c来遍历字符串s
		for (char c : s) {
			if (c == ' ') count++;
		}
		// 修改 s 长度
		s.resize(len + 2 * count);
		// 倒序遍历修改
		for (int i = len - 1, j = s.size() - 1; i < j; i--, j--) {
			if (s[i] != ' ')
				s[j] = s[i];
			else {
				s[j - 2] = '%';
				s[j - 1] = '2';
				s[j] = '0';
				j -= 2;
			}
		}
		return s;
	}
};

Code reference LeetCode user: LeetCode

for (char c : s) is a syntax in C++11, which can be very convenient for traversing strings. In addition, you can also use double number[5] = {0.12, 9.12, 3.12, 4.64, 9.99 similarly. }; for(double x: number), etc. In particular, you can also use for(char &c: s) to directly modify the string.

for (int i = len - 1, j = s.size() - 1; i < j; i--, j--) In the judgment, when i<j, it means that there is no space in front of it at this time. Since this method directly expands (resizes) the original string and uses double pointers to complete filtering and replacement, when i<j, the string s can be directly output.


The End

Guess you like

Origin blog.csdn.net/Norths_/article/details/125433927