Common algorithm questions in iOS interviews and common iOS interview Algorithms
I. Preface
Here is an algorithm question you have encountered in your iOS job search. I hope it will help you. Occasionally updated. If you want to run code debugging online, you can copy the code here. Then perform debugging. The following are common algorithm questions.
Ii. Text
1. The factorial of n.
Idea: Recursive Implementation
#include <stdio.h>int getNJ(int n) { if (n==1 || n==0) { return 1; } return n*getNJ(n-1);}int main() { printf("%d",getNJ(10)); return 0;}
The running result is as follows:
3628800
2. Determine whether a string is an ip address.
Train of Thought: divide the string into two arrays (one digit array and one character array), and then determine whether the content of the number array meets the ip address conditions and whether the content of the character array is ".". The sscanf function is used here. The Code is as follows:
# Include <stdio. h> int checkIP (const char * p) {int n [4]; char c [4]; if (sscanf (p, "% d % c", & n [0], & c [0], & n [1], & c [1], & n [2], & c [2], & n [3], & c [3]) = 7) {int I; for (I = 0; I <3; I ++) {if (c [I]! = '. ') {Return 0 ;}}for (I = 0; I <4; I ++) {if (n [I]> 255 | n [I] <0) {return 0 ;}} return 1 ;}else {return 0 ;}} int main () {const char * x [] = {"132.161.1", "10.0.0.1. "," 127.256.0.1 "," iudfdsfdasfdaf "," 172.16, 2.1 "}; const char * m [] = {" not a valid IP Address "," a valid IP Address "}; int I = 0; while (x [I]! = 0) {printf ("% s \ n", x [I], m [checkIP (x [I]); I ++;} return 0; return 0 ;}
The running result is as follows:
132.161.1 is a legal IP address 10.0.0.1. The IP address 127.256.0.1 is not a legal IP address. iudfdsfdasfdaf is not a legal IP address 172.16, and 2.1 is not a legal IP Address