➤ micro-channel public number: Shan Wing Chi (shanqingyongzhi)
➤ blog Park Address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub Address: https://github.com/strengthen/LeetCode
➤ original address: https://www.cnblogs.com/strengthen/p/10978800.html
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
1, looking for odd numbers of repeats.
Given an array of integers, of which only a number appears odd, other figures appear even number of times. Find this number?
Consideration: an integer of two identical or different zero, or an integer and 0 exclusive for itself. Or you may use different commutative and associative.
Example: Array [1,1,2,2,2,3,3,3,3] seeking occurs odd number of times the digital.
1^1^2^2^2^3^3^3^3
=(1^1)^(2^2)^2^(3^3)^(3^3)
=0^0^2^0^0
=2
Talk is cheap.Show me your code:
1 // test 2 the let ARR = [ 1 , 1 , 2 , 2 , 2 , . 3 , . 3 , . 3 , . 3 ] . 3 // print . 4 Print (arr.reduce ( 0 , ^ )) . 5 // the Print 2
2, without the use of additional auxiliary variables, how to exchange the values of two integer variables x and y are variables?
Thoughts: XOR can use the commutative and associative.
1 x = x ^ y ( 1 ) 2 y = x ^ y ( 2 ) 3 x = x ^ y ( 3 )
Proof: Formula 1 are substituted into Formula 2, Formula 3
1 y = x ^ y = (x ^ y) ^ y = x ^ (y ^ y) = x ^ 0 = x 2 x = x ^ y = (x ^ y) ^ x = (x ^ x) ^ y = 0 ^ y = y
Talk is cheap.Show me your code:
1 func swap(_ x:inout Int,_ y:inout Int) 2 { 3 x = x ^ y 4 y = x ^ y 5 x = x ^ y 6 }
test:
1 var x:Int = 1 2 var y:Int = 9 3 swap(&x,&y) 4 print(x) 5 //Print 9 6 print(y) 7 //Print 1
3, without using pow library functions, the m-th power of n -?
Example: 7 m seeking (binary: 111) power?
It can be broken down as: m ^ 7 = (m ^ 100) * (m ^ 010) * (m ^ 001)
Trends read bit by bit [& 1] and [>> 1], is a 1-bit index represents the accumulated multiplied to the final result.
Talk is cheap.Show me your code:
1 func jump(_ n:Int) -> Int 2 { 3 if n <= 2 {return n} 4 var n1:Int = 1 5 var n2:Int = 2 6 var sum:Int = 0 7 for _ in 3...n 8 { 9 sum = n1 + n2 10 n1 = n2 11 n2 = sum 12 } 13 return sum 14 }
test:
1 print(newPow(3,4)) 2 //print 81
4 the difference, modulo arithmetic modulo operation and
The same procedure and modulo arithmetic is modulo operation.
For integer a, b. Calculating a% b:
(1), the quotient: c = a / b;
(2) calculating modulus or remainder: r = a - c * b.
Modulo and remainder operations in the first step different:
When taken modulo arithmetic value c, rounding towards zero;
Modulo arithmetic in calculating the value of c, the direction of rounding to -∞.
% In the Swift, C, C ++, Java called remainder operator, in Python be modulo operator.
Modulo arithmetic: Swift, C, C ++, Java
A modulo operation: Python
Swift is modulo operation. If a negative b, b ignore the sign. Namely: a% b = a% (-b)
[to sum up]
When the same symbols a and b:
Modulo arithmetic modulo operation and consistent with the results obtained.
When inconsistencies symbols a and b:
Remainder calculation result and a same symbol.
B modulo arithmetic symbols and consistent results.
test:
1 print(6%4) 2 print(-6%4) 3 print(6%(-4)) 4 //Print 2 5 //Print -2 6 //Print 2
5, the array index (counting sequencing): statistics, determining the number of occurrences of letters.
Given an array of integers in the range 0 to 100 array elements.
Please use (n) time complexity of O, from small to large print?
Ideas: the value corresponding to the index as an array, if the number appeared, then the corresponding array plus 1.
Talk is cheap.Show me your code:
1 func printNum(_ arr:[Int],_ maxNum:Int) 2 { 3 var temp:[Int] = [Int](repeating: 0, count: maxNum + 1) 4 for i in 0..<arr.count 5 { 6 temp[arr[i]] += 1 7 } 8 for i in 0...maxNum 9 { 10 for j in 0..<temp[i] 11 { 12 print("\(j):\(i)") 13 } 14 } 15 }
test:
1 let arr:[Int] = [100,5,71,89,66,77,89,91,71,95,5,100] 2 printNum(arr, 100) 3 /* 4 0:5 5 1:5 6 0:66 7 0:71 8 1:71 9 0:77 10 0:89 11 1:89 12 0:91 13 0:95 14 0:100 15 1:100 16 */
5, the recursive (n-> 1) and the recursive (1-> n)
A frog can skip steps 1 or 2 steps. The frog jumped seek n-level steps how many jumps?
Recursive (n-> 1) Method: Calculation is repeated many
1 func jump(_ n:Int) -> Int 2 { 3 if n <= 2 {return n} 4 else 5 { 6 return jump(n - 1) + jump(n - 2) 7 } 8 }
test:
1 print(jump(10)) 2 //print 89
Memorandum method: Using an array index, calculated deduplication
When arr [n] = 0, it means jump (n) is not calculated,
When arr [n]! = 0, indicates jump (n) has been calculated.
. 1 FUNC Jump (_ n-: Int) -> Int 2 { . 3 var ARR: [Int] = [Int] (repeating: 0 , COUNT: . 1 ) . 4 IF n-<= 2 { return n-} . 5 the else . 6 { . 7 / / automatic array expansion . 8 the while (n-> arr.count - . 1 ) . 9 { 10 ARR = + [Int] (repeating: 0 , COUNT: arr.count) . 11 } 12 is // not calculated 13 is IF ARR [n-] == 0 14 { 15 ARR [n-] = Jump (N- . 1 ) + Jump (N- 2 ) 16 return ARR [n-] . 17 } 18 is the else . 19 { 20 is // Calculated 21 is return ARR [n-] 22 is } 23 is } 24 }
test:
1 print(jump(10)) 2 //print 89 3 print(jump(11)) 4 //print 144
Recursive (1-> n) Method:
1 func jump(_ n:Int) -> Int 2 { 3 if n <= 2 {return n} 4 var n1:Int = 1 5 var n2:Int = 2 6 var sum:Int = 0 7 for _ in 3...n 8 { 9 sum = n1 + n2 10 n1 = n2 11 n2 = sum 12 } 13 return sum 14 }
test:
1 print(jump(10)) 2 //print 89 3 print(jump(11)) 4 //print 144 5 print(jump(12)) 6 //print 233
Recursive often consider the following questions:
(1) Is there repeated calculations, can be optimized using methods memo.
(2) whether the method can be taken recursive (1-> n), reducing the cost of recursion.