Strings and Arrays

Source: Internet
Author: User

Find the first non-repeating character in a string

For example, the first non-repeating character in Atioia is T. From a general idea, you can compare each character with the character behind him, and when the end is not the same, you will find the character, which is obviously On2.

One way to do that is to use a hash table and an array to iterate once or multiple times by counting the corresponding key values that correspond to the corresponding characters. The second traversal of the original string, that corresponds to the value of one is the request. The array subscript and hash key values are selected for the integer converted to the corresponding character.

First, build the string hash list:

For each character

If there is no current character, the store 1

Otherwise, the number of characters for the current statistic is +1

Second, scan the string:

For each character

The If hash count is 1 to return it

If there are no statistics unique characters, return null

Deletes the specified character

A case in which all characters containing another string are removed from one string. If the internet deletes ter, it becomes inn. We remember that the Internet is str,ter as remove.

It is easy to think of a kind of idea is to walk through the source string, and then encounter the decision whether to exist in the deleted string, some words will be deleted. It's obviously also On2.

In one of the non-repeating characters of the preceding one, by constructing a flag array with a character subscript, first set the value of all characters in Str to false, and the character value in the Traverse remove setting is true. Then the original array operation, when encountered false operation, the subsequent copy to the front, because if it is true we have no need, then return a smaller than before the array is good.

public static string Removechars (String str, string remove) {

Char[] s = Str.tochararray ();

Char[] r = Remove.tochararray ();

int src, dst = 0;

Boolean[] Flags = new Boolean [128];

for (src = 0;src < r.length; ++src) {

FLAGS[R[SRC]] = false;

}

for (src = 0; src < s.length; ++src) {

if (!flag[s[src]] S[DST + +] = s[src];

}

return new String (s, 0, DST);

}

Invert words

You're a girl reversal becomes gril A is you consider first the overall rollover for LIRG a era uoy and then in each word is reversed.

Conversion of integers and strings

String to integer: minus "0" for each character he is the corresponding integer, traversing the string from left to right and then into the corresponding integer. Each more traversal of a character, the previous integer *10 plus now this integer is good, relatively simple. Oh forget, and remember to handle the minus.

Start with number set to 0

If The first character is a "-"

Start with a second character and set a negative number marker

For each character

Number multiplied by 10 plus the character minus "0"

If there is a negative number mark

Take counter

Return the desired value

Integer to string: Set a buffer, and then give the integer% 10, take the last after the last plus "0" is the corresponding character, but also to the original integer/10 minus. This time also remember the handling of the minus sign, and the treatment of the special value 0.

If number is less than 0

Reverse the number and set a negative number marker

Do

Add "0" to number%10 and write the result to the buffer

Number/=10

While number is not equal to 0

If there is a negative number mark

Writes "-" to the buffer

String that writes out the buffer backwards

Strings and Arrays

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.