The first question: to find a two-dimensional array
Title Description
(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.
Problem solution
:
Due to the number of two-dimensional array is incremented from left to right, top to bottom increments, then we can consider the look from the bottom left corner, if val is greater than the array [i] [j], then, j ++, if less than a [i] [j] then, i--, otherwise the output true representation found val;
If in the end are not found, indicating that the element does not exist, returns false: that there is no the element.
Reference Code:
. 1 class Solution { 2 public : . 3 BOOL the Find ( int target, Vector <Vector < int >> Array) { . 4 // Array is a two-dimensional array, where the empty judgment do not operate . 5 int rows = array.size (); . 6 int cols = array [ 0 ] .size (); . 7 int I = rows- . 1 , J = 0 ; // bottom left coordinates of elements . 8 the while (I> = 0 && J <cols) { // so as not to exceed the array range . 9 IF (target < Array [I] [J]) 10 i--; // fewer elements to find, looking up . 11 the else IF (target> Array [I] [J]) 12 is J ++; // Find the larger elements, to find the right 13 is the else 14 return to true ; / / found 15 } 16 return to false ; . 17 } 18 is };
The second question: replace spaces
Title Description
Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.
Solution: Let's count up the number of spaces to calculate the length of the string after the replacement, and can be replaced from the back.
Reference Code:
View Code
1 public class Solution { 2 public String replaceSpace(StringBuffer str) { 3 return str.toString().replaceAll("\\s", "%20"); 4 } 5 }
Third question: print characters from the tail to the head
Title Description
Enter a list, by returning a list sequentially from the tail to the head of ArrayList.
Solution: each head portion is inserted into the vector until the head == NULL;
Reference Code:
1 /** 2 * struct ListNode { 3 * int val; 4 * struct ListNode *next; 5 * ListNode(int x) : 6 * val(x), next(NULL) { 7 * } 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> printListFromTailToHead(ListNode* head) 13 { 14 vector<int> ans; 15 while(head!=NULL) 16 { 17 ans.insert(ans.begin(),head->val); 18 head=head->next; 19 } 20 return ans; 21 22 } 23 };
Solution: Let's count up the number of spaces to calculate the length of the string after the replacement, and can be replaced from the back.
Reference Code:
View Code
1 public class Solution { 2 public String replaceSpace(StringBuffer str) { 3 return str.toString().replaceAll("\\s", "%20"); 4 } 5 }
Third question: print characters from the tail to the head
Title Description
Enter a list, by returning a list sequentially from the tail to the head of ArrayList.
Solution: each head portion is inserted into the vector until the head == NULL;
Reference Code:
1 /** 2 * struct ListNode { 3 * int val; 4 * struct ListNode *next; 5 * ListNode(int x) : 6 * val(x), next(NULL) { 7 * } 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> printListFromTailToHead(ListNode* head) 13 { 14 vector<int> ans; 15 while(head!=NULL) 16 { 17 ans.insert(ans.begin(),head->val); 18 head=head->next; 19 } 20 return ans; 21 22 } 23 };