Programmer's Interview Guide (6th Edition)

Question one:

Interview question 01.01. Determine whether characters are unique

Easy difficulty 260 Favorites and sharing switch to English to receive dynamic feedback

Implement an algorithm to determine  s whether all characters of a string are different.

Example 1:

Input: s = "leetcode"
 Output: false 

Example 2:

Input: s = "abc"
 Output: true

limit:

  • 0 <= len(s) <= 100
  • s[i]Contains lowercase letters only
  • Bonus points if you don't use additional data structures.
bool isUnique(char* astr){
    int i=0;
    int j=0;
    for(i=0;i<strlen(astr);i++){
        for(j=i+1;j<strlen(astr);j++){
            if(astr[i]==astr[j]){
                return false;
            }
        }
    }
    return true;

}

Question two: 

 

Interview question 01.03. URLization

Easy Difficulty 96 Favorites and Sharing Switch to English to receive dynamic feedback

URLization. Write a method that replaces all spaces in a string %20. It is assumed that there is enough space at the end of the string for the additional characters and that the "real" length of the string is known. (Note: If you want to Javaimplement it, please use a character array to implement it so that you can operate directly on the array.)

Example 1:

Input : "Mr John Smith", 13
 Output : "Mr%20John%20Smith"

Example 2:

Input : " ", 5
 Output : "%20%20%20%20%20"

hint:

  • The string length is in the range [0, 500000].
char* replaceSpaces(char* S, int length){
    char *ans = malloc(sizeof(char)*(length*3 + 1));
    int n = 0;
    while(length--){
        if(*S==' '){
            ans[n++] = '%';
            ans[n++] = '2';
            ans[n++] = '0';
        }else{
            ans[n++] = *S;
        }
        S++;
    }
    ans[n]  = 0;
    return ans;
}

Question three:

 

Interview question 01.02. Determine whether characters are mutually rearranged

Easy Difficulty 148 Favorites and Sharing Switch to English to receive dynamic feedback

Given two strings consisting of lowercase letters  s1 and  s2, write a program to determine whether the characters of one string can be rearranged to become the other string.

Example 1:

Input: s1 = "abc", s2= "bca"
 Output: true 

Example 2:

Input: s1 = "abc", s2= "bad"
 Output: false

illustrate:

  • 0 <= len(s1) <= 100
  • 0 <= len(s2) <= 100
bool CheckPermutation(char* s1, char* s2){
    int count=0;
    int s=strlen(s1);
    for(int i=0;i<s;i++){
        for(int j=0;j<s;j++){
            if(s1[i]==s2[j]){
                count++;
            }
        }
    }
    if(count==s){
        return true;
    }
    else{
        return false;
    }

}

 

Guess you like

Origin blog.csdn.net/qq_71356343/article/details/128749580