#58 Length of last Word
Given A string s consists of upper/lower-case alphabets and empty space characters ‘ ‘
, return the length of LA St Word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given S = "Hello World"
,
Return 5
.
int Lengthoflastword (char* s) {int count = 0;int len = strlen (s); int i = 0,j = len-1; while (s[j]== ")//Ignore the space j--; while (j>=i) { if (s[j]! = ") count++; else break ; j--; } return count;}
#66 Plus One
Given a non-negative number represented as an array of digits, plus one to the number.
The digits is stored such, the most significant digit was at the head of the list.
Returns the result of +1, given a non-negative number stored in a string (in string form)
Of course, the character of string storage is convenient for large number calculation, so it is not possible to convert from string to number +1.
/** * Return An array of size *returnsize. * Note:the returned array must is malloced, assume caller calls free (). */int* PlusOne (int* digits, int digitssize, int* returnsize) {int i = Digitssize-1;int *result;//return string digits[i]++;//plus 1/* as Fruit >=10 */while (digits[i]>=10 && i>=1) {digits[i] = digits[i]-10;i--;d igits[i]++;} /* Determines whether the highest bit yields a carry-whether to increase the string length */if (digits[0]>=10) { *returnsize = Digitssize+1;result = (int *) malloc (sizeof (int) * ( digitssize+1));d igits[0] = digits[0]-10;for (i=0;i<digitssize;i++) result[i+1] = digits[i];result[0] = 1;} else{ *returnsize = digitssize; result = digits;} return result;}
#67 Add Binary
given binary strings, return their sum (also a binary string).
For example,
A ="11"
b ="1"
Return "100"
.
Adds a binary number stored in a string. The following code can also be optimized to add edges for carry judgments. When testing, A, B takes an array form, otherwise if you point to a string constant, you are not allowed to modify the value in the string, resulting in an error.
char* Addbinary (char* A, char* b) { int i,j=0;int len1 = strlen (a); int len2 = strlen (b); char *p,*q,*r;int Len_max = (l EN1>=LEN2)? Len1:len2;int len_min = (len1<=len2)? len1:len2;//pointer p points to a A/b with a long length of q pointing to the shorter if (len1 = = len2) {p = A;q = b;} else if (Len1 > Len2) {p = A;q = b;} else if (Len1 < len2) {p = B;q = A;} if (len1==0) return b;if (len2==0) return a;for (i=len_min-1; i>=0; i--) {P[len_max-1-j] + = (q[len_min-1-j]-' 0 '); j + +;} Determines whether the highest bit has a carry for (i=len_max-1;i>=1;i--) {if (p[i]>= ' 2 ') {P[i]-= 2;p[i-1]++;}} Determine the highest bit if (p[0]-' 0 ' <2) return p;//bit constant else{p[0]-= 2;//overflow R = (char *) malloc (sizeof (char) * (len_max+2)); r[0 ] = ' 1 ';//carry for (I=1; i<=len_max; i++) r[i] = p[i-1];r[len_max+1]= ' + '; return r;}}
#70 Climbing Stairs
You is climbing a stair case. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In what many distinct ways can you climb to the top?
There are a lot of problems like this, such as monkeys picking peach, walking steps, the essence is Fibonacci that cut sequence. F (n) = f (n-1) +f (n-2).
Use recursion directly, as follows, but the submission results show time Limit exceeded
int climbstairs (int n) { if (n==1) return 1; if (n==2) return 2; if (n>2) return Climbstairs (n-1) +climbstairs (n-2);}
Because recursion is used, the calculation is repeated. The DP algorithm saves intermediate results to avoid the calculation of duplicate sub-problems. Improvements are as follows:
int climbstairs (int n) { int i,*a; if (n==1) return 1; if (n==2) return 2; if (n>2) { a= (int *) malloc (sizeof (int) *n); A[0]=1; a[1]=2; for (i=2;i<n;i++) a[i]=a[i-1]+a[i-2]; return a[n-1];} }
Leetcode--easy Series 4