[IT pen questions over the years] Which Online internship pen questions will I go to in 2015, it 2015
(1) Same as [LeetCode] 33. Search in Rotated Sorted Array in LeetCode.
/* ----------------------------------------------------------- * Date: * Author: SJF0115 * Subject: 33. search in Rotated Sorted Array * Source: output result: AC * Source: LeetCode * Summary: ----------------------------------------------------------- */# include <iostream> # include <stdio. h> using namespace std; class Solution {public: // Binary search int search (int A [], int n, int target) {int start = 0, end = n-1; int mid; while (start <= end) {mid = (start + end)/2; if (A [mid] = target) {return mid ;} // if the middle element is greater than the leftmost element, the left part is divided into an ordered array else if (A [mid]> = A [start]). {// the target is located in the left part. if (target> = A [start] & target <= A [mid]) {end = mid-1 ;} // The target is in the right else {start = mid + 1 ;}} // if the middle element is smaller than the rightmost element, the right part is divided into the ordered array else {// the target is located in the right part if (target <= A [end] & target> = A [mid]) {start = mid + 1;} // else {end = mid-1 ;}}} return-1 ;}}; int main () {int result; solution solution; int A [] = {3, 1}; result = solution. search (A, 2, 1); printf ("Result: % d \ n", result); return 0 ;}
For more questions about Binary Search: [classic Interview Questions] problem summary about Binary Search
(2)
# Include <string ># include <iostream> using namespace std; string Decode (string str) {string result; int size = str. size (); if (size <= 0) {return result;} // if // The repeating element interval int start = 0, end = 0; // number of repetitions int count = 0; for (int I = 0; I <size; ++ I) {// number if (str [I]> = '0' & str [I] <= '9 ') {count = count * 10 + str [I]-'0 '; if (I = size | (str [I + 1] <'0' | str [I + 1]> '9 ')) {for (int j = 0; j <count; ++ j) {result + = str. substr (start, end-start);} // for // reset start = I + 1; end = I + 1; count = 0 ;} // if} // if // character else {+ end ;}// for return result;} int main () {string str ("love2big2gae4fff1 "); cout <Decode (str) <endl; return 0 ;}