Where to go for the writing test?

Source: Internet
Author: User

1, two points to find the topic description:

For an ordered array, we usually use a binary lookup to locate an element, write a binary lookup algorithm, and find the specified element in the array.
Given an array of integers a and its size n, and given the Val of the element to look for, return its position in the array (starting from 0), or 1 if no element exists. If the element appears multiple times, return to the first occurrence.
Test Sample:
[1,3,5,7,9],5,3
Return: 1 Code implementation:

Class BinarySearch {public
:
    int GetPos (vector<int> A, int n, int val) {
        //write code here
        int left = 0;
        int right = n-1;
        While [left <= right] {
            int mid = left + (right-left)/2;
            if (a[mid] = = val) {
                if (mid = 0 | | (Mid > 0 && a[mid-1]!= val)) {return
                    mid;
                }
                else if (a[mid-1] = = val) {Right
                    = mid-1;
                }
            }
            else if (A[mid] > val) Right
                = mid-1;
            else left
                = mid+1;
        }
        return-1;
    }
;

2, the first repeated occurrences of the character title description

For a string, design an efficient algorithm to find the first occurrence of the character.
Given a string (not necessarily all letters) A and its length n. Please return the first occurrence of the character. Guarantees that there are duplicate characters in the string and that the length of the string is less than or equal to 500.
Test Sample:
"QYWYER23TDD", 11
Back: Y topic Analysis:

The topic did not read the meaning of the topic at the beginning. I thought it was to go through all the characters and find the first recurring character, but instead of traversing to a character, as long as the character was repeated, the current character was returned, regardless of whether the front character had already appeared behind it. Implementation code:

Class Firstrepeat {public
:
    char findfirstrepeat (string A, int n) {
        //write code here
        if (A.size () < 2 || N < 2 return
            ' to ';
        int hash[256]={0};
        for (int i = 0; i < a.size (); ++i) {
            if (hash[a[i]] = = 0)
                hash[a[i] = 1;
           else return
                a[i];
        }
        Return ' a ';
    }
;

3, Find Coder topic description

Design an efficient algorithm and, in a given string array, find the string containing "coder" (case-insensitive) and return it as a new array. The order of the resulting strings is descending by the number of "coder" occurrences, and if the number of "coder" in the two strings is the same, keep their position in the original array.
Given an array of strings A and its size n, return the result array. Ensure that the original array size is less than or equal to 300, where the length of each string is less than or equal to 200. Also ensure that there must be a string containing coder.
Test Sample:
["I am a coder", "Coder coder", "Code"],3
Back: ["Coder coder", "I am a coder"] topic analysis:

The point is to find the number of substrings in the string, and then sort the stability by the number of substrings (you can use merge sorting here, or you can sort by using the library function Stable_sort () function). Code implementation:

BOOL Compare (const pair<string, int>& L, const pair<string, int>& R) {return L.second > R.seco
nd
        Class Coder {string change (string str) {string ans = str;  for (int i = 0;i<str.length (); i++) if (Str[i] >= ' A ' &&str[i] <= ' Z ') ans[i] =
        Str[i] + 32;
    return ans;
        int Count (const string& s) {string str = change (s);
        int count = 0;
        String substr = "Coder";
            for (int i = 0; i < str.size (); ++i) {int found = Str.find (substr, i);
                if (found!=-1) {count++;
            i = found + 1;
        else break;
    return count; } public:vector<string> Findcoder (vector<string> A, int n) {//write code here if (a.si
        Ze () < 1) return A;
        Vector<pair<string, Int>> v; for (int i = 0; I &Lt N
        ++i) {v.push_back (Make_pair (a[i), Count (a[i)));
        } stable_sort (V.begin (), V.end (), Compare);
        vector<string> ret;
            for (int i = 0; i < v.size (); ++i) {if (V[i].second > 0) ret.push_back (v[i].first);
        if (V[i].second = = 0) break;
    return ret; }
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.