Complexity of Time
The simple understanding of time complexity is the number of lines that execute the statement. If there are loops and recursion, the simple statement is ignored, and the number of statements executed by the loop and recursion is calculated directly.
Like what:
[Java]View PlainCopy
- int x = 1; Time Complexity of O (1)
- for (int i=0; i<n; i++) {
- System.out.println (i);
- }//Time complexity of O (n)
Specific examples:
1, O (1)
[Java]View PlainCopy
- int x = 1;
2, O (n)
[Java]View PlainCopy
- for (int i=0; i<n; i++) {
- System.out.println (i);
- }
3. O ()
[HTML]View PlainCopy
- int n = 8, count = 0;;
- for (int i=1; I<=n; i *= 2) {
- count++;
- }
4.
[HTML]View PlainCopy
- int n = 8, count = 0;;
- for (int i=1; I<=n; i++) {
- for (int j=1; J<=n; J + +) {
- count++;
- }
- }
5.
[Java]View PlainCopy
- int n = 8, count = 0;;
- for (int i=1; i<=n; i *= 2) {
- For (int j=1; j<=n; J + +) {
- count++;
- }
- }
The examples are relatively simple.
Complexity of space
Spatial complexity is also a simple understanding of the storage space occupied by temporary variables. A simple example:
[Java]View PlainCopy
- Swap two variables x and y
- int x=1, y=2;
- int temp = x;
- x = y;
- y = temp;
A temporary variable temp, so the spatial complexity is O (1).
Transferred from: http://blog.csdn.net/qiantujava/article/details/12898461
Time complexity and spatial complexity (RPM)