Leetcode:happynumber,house Robbe
1.Happy number
2 points are harvested in this question:
1. After getting the question, consider the special situation, the code considers 1 and 4, or say <6 situation, hands-on calculation. (may be found in the code step-by-step test, it is not known how to know these special cases)
2. Each digit of the number, N%10,N/10.
Topic:
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process:starting with any positive integer and replace the number by the Sum of the squares of its digits, and repeat the process until the number equals 1 (where it would stay), or it loops Endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 is happy numbers.
Example: is a happy number
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
Ideas:
My idea: (as you end of the program, I hope you don't laugh)
Calculate the number with POW (x,2), but don't know how to write it, okay, just like no idea.
The idea in Leetcode/discuss:
(n%10) * (n%10), N/10.
Tip: In Leetcode each problem has a discuss, from the inside can see everyone's ideas and discussion, personally feel very good
Code:
There are two more concise and understandable codes that I think are written.
Code Listing 1:
1 BOOLIshappy (intN) {2 intnum=0;3 while(n!=1&&n!=4)//1 is the initial value, and how the square is 1. 4 into the dead loop .4 {5 while(n)6 {7num + = (n%Ten) * (n%Ten);//the loop will square each bit8N/=Ten;9 }Tenn=num; Onenum=0; A } - return 1==N; -}
Code Listing 2:
1 classSolution {2 Public:3 BOOLIshappy (intN) {4 while(n>6)5 {6 intNext =0;7 while(n)8 {9next+= (n%Ten) * (n%Ten);TenN/=Ten; One } An =Next; - } - returnn==1; the } -};
Code 1 is to take the numbers "1" and "4" separately out of view, code 2 is directly from n greater than 6 look. In fact, the two are the same, can use their own calculation, "2" and "4" is the same, "3", "5", "6" will eventually enter the "2", "4" cycle.
22 =4
42=8
82=16
12+62=37
32+72=58
52+82=89
82+92=145
12+42+52=42
42+22=20
22=4
32=9
92=81
82+12=65
62+52=61
62+12=37 (return to the 22 loop)
52=25
22+52=29
22+92=85
82+52=89 (return to the 22 loop)
62=36
32+62=45
42+52=41
42+12=17
12+72=50
52=25 (return to the 52 loop)
2.House robber
Topic Harvest:
- How to use iterations
- About the C + + code Editor flash-back:
1). Add the statement cin.get () at the above code location;
2). Add a location with the above code, and add the statement system ("pause");
Topic:
You is a professional robber planning to rob houses along a street. Each house have a certain amount of money stashed, and the only constraint stopping are from robbing each of the them are that Adjacent houses have security system connected and it would automatically contact the police if both adjacent houses Wer E brokeninto on the same night.
Given a list of non-negative integers representing the amount of money in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.
Ideas:
My train of thought:
For the given array, the maximum and the number of nonadjacent digits in the array. But I don't know how to write the code.
The idea in Leetcode/discuss:
Same as mine, to find the maximum and the number of nonadjacent numbers in the array.
Code:
Put out 3 pieces of code, the first two are simple and easy to understand, the latter is a problematic code.
Code 1:c++ Code
classSolution { Public: intRob (vector<int>&nums) { intn = nums.size (), pre =0, cur =0;//cur is the sum of the i-1, the pre is the sum of the I-2 for(inti =0; I < n; i++) { intTEMP = max (pre + nums[i], cur);//if Pre+nums[i] (that is, i-2) is greater than cur (the total value of i-1), then the sum is pre+nums[i], otherwise cur.Pre =cur; Cur=temp; } returncur; }};
Code 2:C Code, but the idea is very clear
1 intMaxintAintb) {2 returna > B?a:b;3 }4 5 intRobint* Nums,intnumssize) {6 int*D;7 8D = (int*)malloc(Numssize *sizeof(nums[0]));9 Tend[0] = nums[0]; Oned[1] = max (nums[0], nums[1]); A - //D[i] means the most value of can be robbed before the ith store. For each store,
We have both Choice:rob or not Rob: (1) If robbing, d[i] = D[i-2] + nums[i], for stores robbed cannot is connected. (2) If not robbing, d[i] = d[i-1] - the for(inti =2; i < numssize; i++) { -D[i] = max (d[i-2]+nums[i], d[i-1]); - } - + returnd[numssize-1]; -}
Code Listing 3: the wrong idea
#defineMax (A, B) ((a) > (b)? ( A):(B))intRobintNum[],intN) {intA =0; intb =0; for(intI=0; i<n; i++) { if(i%2==0)//with odd and even, odd and even numbers must not be adjacent, but not necessarily the largest{a= Max (A +Num[i], b); } Else{b= Max (A, B +Num[i]); } } returnMax (A, b);}
The error cause of code 3 is not necessarily the largest, such as a = {1, 4, 2, 2, 4, 5, 9, 5} This array has a maximum of 17 (9,4,4 only), an odd number of 16, and even 16. It is wrong to look at odd-even ideas.
With the main function: Do not write the main function, but still own Chi Chi out a main function of the code, do not know what the boundary condition of the test.
#include"stdafx.h" //Console Header File#include"iostream" //cin,cout header file for input and output#include"Vector" //Vector's header file#include"algorithm" //The head file of the max () function in the Code, Min () is also thisusing namespaceStd//C + + must write and don't know whyclasssolution{ Public: intRob (vector<int>&nums) { intCur =0; intPre =0; inttemp =0; for(size_t i =0; I < nums.size (); i++)//in Visul Studio must write wrong size_t otherwise will error, but leetcode on the test not, directly written int can{Temp= max (pre +nums[i], cur); Pre=cur; Cur=temp; } returncur; }};int_tmain (intARGC, _tchar*argv[]) {Vector<int> A = {1,4,2,2,4,5,9,5};//The test, originally wanted to write an array that is not fixed, the array from the input, and temporarily not intm; Solution solution; //It seems like it must be written, not directly quotedm =Solution.rob (a); cout<<" The money is"<< m <<Endl; Cin.Get();//prevent the editor from flashing return 0;}
2016.5.15--leetcode-happynumber,house robber