Time Complexity and space complexity

Source: Internet
Author: User

Time Complexity a simple understanding of time complexity is the number of statements executed. If loops and Recursion exist, ignore simple statements and calculate the number of cycles and Recursion statement executions. For example: [java] int x = 1; // the time complexity is O (1) for (int I = 0; I <n; I ++) {System. out. println (I);} // the time complexity is O (n). For example: 1. O (1) [java] int x = 1; 2. O (n) [java] for (int I = 0; I <n; I ++) {System. out. println (I) ;}3, O () [html] int n = 8, count = 0; for (int I = 1; I <= n; I * = 2) {count ++;} 4. [html] int n = 8, count = 0; for (int I = 1; I <= n; I ++) {for (int j = 1; j <= n; j ++) {count ++;} 5. [java] view plaincopyint n = 8, count = 0; for (int I = 1; I <= n; I * = 2) {for (int j = 1; j <= n; j ++) the {count ++;} example is relatively simple. Space complexity space complexity is also easily understood as the storage space occupied by temporary variables. A simple example: [java] // exchange two variables x and y int x = 1, y = 2; int temp = x; x = y; y = temp; A temporary variable temp, so the space complexity is O (1 ).

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.