1.1

 1 import java.util.Scanner;
 2 public class Main 
 3 {
 4     //此方法时间复杂度为O(n) 空间复杂度为O(1)
 5     public static boolean isUniqueChar2(String str)
 6     {
 7         //若字符串长度大于字母表中字符个数,则必有重复
 8         if(str.length()>256)    return false;
 9         boolean[] char_set = new boolean[256];    //默认都是false
10         for(int i=0; i<str.length(); i++)
11         {
12             int val = str.charAt(i);
13             //若这个字符已出现过,则返回false
14             if(char_set[val])    return false;
15             char_set[val] = true;
16         }
17         return true;
18     }
19     public static void main(String[] args) {
20         String str;
21         Scanner sca = new Scanner(System.in);
22         //多组输入,输入#结束
23         while(true)
24         {
25             str = sca.nextLine();
26             if(str.equals("#"))    return;
27             boolean res = isUniqueChar2(str);
28             System.out.println(res);
29         }
30     }
31 }

1.2

#include <iostream>
#include <cstring>
using namespace std;
void reverse(char* str)
{
  char* end = str;
  char tmp;
  //找出字符串末尾
  while(*end)
  {
    end++;
  }
  end--;//会退到null前的一个字符
  while(str < end)
  {
    tmp = *str;
    *str = *end;
    *end = tmp;
    str++;
    end--;
  }
}
int main()
{
    char s[100];
    cin>>s;
    reverse(s);
    cout<<s;
    return 0;
}

1.3

import java.util.Arrays;
import java.util.Scanner;

public class SortC {
    public static String sort(String s)
    {
        char[] con = s.toCharArray();
        Arrays.sort(con);
        return new String(con);
    }
    public static boolean permutation(String s, String t)
    {
        if(s.length() != t.length())    return false;
        return sort(s).equals(sort(t));
    }
    public static void main(String[] args) {
        String s, t;
        Scanner sca = new Scanner(System.in);
        while(true)
        {
            s = sca.nextLine();
            if(s.equals("#"))
                return;
            t = sca.nextLine();
            System.out.println(permutation(s, t));
        }
    }

}

1.4

这题主要是一开始开的数组要足够大,不然会有数组越界的错误。

 1 public class Main {
 2     public static void main(String[] args) {
 3         String string = "Mr John Smith "; 
 4         char[] s = new char[100];
 5         int length = string.length();
 6         for(int i=0; i<length; i++){
 7             s[i] = string.charAt(i);
 8         }
 9         for (int i=length-1; i>=0; i--) {
10             if(s[i] == ' ')    length--;
11             else     break;
12         }
13         repSpace(s, length);
14         System.out.println(s);
15     }
16     public static void repSpace(char[] s, int length) {
17         int spaceNum=0, newLength;
18         for(int i=0; i<length; i++){
19             if(s[i] == ' ') spaceNum++;
20         }
21         newLength = length + spaceNum*2;
22         s[newLength] = '\0';
23         int j = newLength - 1;
24         for(int i=length-1; i>=0; i--){
25             if(s[i]!=' ')
26                 s[j--] = s[i];    
27             else {
28                 s[j--]='0'; s[j--]='2'; s[j--]='%';
29             }
30         }
31     }
32 }

 1.5 这题主要是考察对StringBuffer的理解,在字符串拼接上比String效率高

 1 public class Main {
 2     public static void main(String[] args) {
 3         System.out.println(compressBad("aabcccccaaa"));
 4         System.out.println(compressBuffer("aabcccccaaa"));
 5     }
 6     //时间复杂度为O(str.length()+k^2)
 7     public static String compressBad(String str) {
 8         String mystr = "";
 9         char last_char = str.charAt(0);
10         int count = 1;
11         for(int i=1; i<str.length(); i++){
12             if(str.charAt(i) == last_char)    count++;
13             //如果这个字符与之前不同
14             else {
15                 //字符串拼接的时间杂度为O(n^2)
16                 mystr+=(last_char+""+count);
17                 last_char = str.charAt(i);
18                 count=1;
19             }
20         }
21         mystr+=(last_char+""+count);
22         if(mystr.length()>=str.length())    return str;
23         else    return mystr;
24     }
25     //利用StringBuffer来减少时间复杂度
26     public static String compressBuffer(String str) {
27         StringBuffer mystr = new StringBuffer();
28         char last_char = str.charAt(0);
29         int count = 1;
30         for(int i=1; i<str.length(); i++){
31             if(str.charAt(i) == last_char)    count++;
32             //如果这个字符与之前不同
33             else {
34                 //字符串拼接会直接构建一个新串,因此时间杂度为O(n^2)
35                 mystr.append(last_char);
36                 mystr.append(count);
37                 last_char = str.charAt(i);
38                 count = 1;
39             }
40         }
41         mystr.append(last_char);
42         mystr.append(count);
43         if(mystr.length()>=str.length())    return str;
44         else    return mystr.toString();
45     }
46 }