Huawei computer questions

Source: Internet
Author: User

I. Question description:
Enter a string of lowercase letters (~ Z. Compile a character string filter program. If multiple identical characters appear in the string, filter out non-first-time characters.
For example, the "abacacde" character string is "ABCDE ".

    public String stringFilter(String str) {    Map<String, Integer> map = new LinkedHashMap<String, Integer>();        String s = "";    for(int i=0; i<str.length(); i++) {    s = String.valueOf(str.charAt(i));    if(map.keySet().contains(s))         map.put(s, map.get(s)+1);    else     map.put(s, 1);    }    String _str = "";    for(Map.Entry<String, Integer> entry : map.entrySet()) {    _str += entry.getKey();         }    return _str;    }

-- Note that the traversal order of elements in hashmap is not in the input order, but in the "hash order conducive to random search ".

-- Linkedhashmap is traversed in the order of addition, similar to linkedhashset.

 

Ii. Question description:
Enter a string of lowercase letters (~ Z. Compile a character string compression program to compress duplicate letters that are present continuously in the string and output the compressed character string.
Compression rules:
1. Only compress the repeated characters. For example, the compressed string "abcbc" is still "abcbc" because there are no consecutive duplicate characters ".
2. The format of the compressed field is "repeated characters + characters ". For example, after the string "xxxyyyyz" is compressed, it becomes "3x6yz ".

Public String stringzip (string Str) {string _ STR = ""; int I = 0; // cursor position char C; // cursor character while (I <Str. length () {int n = 1; // Number of adjacent identical characters C = Str. charat (I); For (Int J = I + 1; j <Str. length (); j ++) {char _ c = Str. charat (j); If (C = _ C) {n ++;} else {break;} If (n = 1) {_ STR + = C ;} else if (n> 1) {_ STR + = string. valueof (n) + C;} I + = N; // cursor position} return _ STR ;}

  

 

Iii. Question description:
Enter the addition, subtraction, multiplication, and Division operations of positive integers less than 100 on the keyboard. Compile a program to output the computation result string.
The format of the input string is "operand 1 operator operand 2", and "operand" and "operator" are separated by a space.

Note:
1. the operand is a positive integer and computing result overflow is not required.
2. If the input formula format is incorrect, the output result is "0 ".

Public int Arithmetic (string Str) {string [] strarr = Str. Split (""); If (strarr. length! = 3 |! Isnumeric (strarr [0]) |! Isnumeric (strarr [2]) {return 0;} string operator = strarr [1]; // operator int leftint = integer. valueof (strarr [0]); // left operand int rightint = integer. valueof (strarr [2]); // right operand int I = 0; If ("+ ". equals (operator) {I = leftint + rightint;} else if ("-". equals (operator) {I = leftint-rightint;} else if ("*". equals (operator) {I = leftint * rightint;} else if ("/". equals (operator) {I = leftint/right Int;} return I;} // determines whether the string can be converted to the public static Boolean isnumeric (string Str) {for (INT I = 0; I <Str. length (); I ++) {If (! Character. isdigit (Str. charat (I) {return false ;}} return true ;}

  

 

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.