这是一个面试问题。因此,您有一个输入字符数组,如下所示:
char[] input = "Hello world! ".toCharArray();
任务是开发一种方法,以将char数组作为输入并将多余的空格移到数组的末尾,而无需使用java提供的任何内置方法,也不会从数组中删除任何元素。预期输出:
"Hello world! ".toCharArray();
如您所见,输入中可能会有多余的空白,应将其移到数组的末尾。我的尝试:
public char[] moveWhiteSpaces(char[] inArr){
for(int i = 0; i<inArr.length; i++){
String currChar = String.valueOf(inArr[i]);
while(currChar.equals(" ")){
inArr[i] = inArr[i+1];
}
{
return inArr;
}
这不起作用。那么如何在不删除多余空白的情况下将其移到末尾呢?
输入:['h', 'e', 'l', 'l', 'o', ' ', ' ', ' ', 'w', 'o', 'r', 'l', 'd', '!', ' ', ' ',]
输出:['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '! ', ' ', ' ',' ', ' ']
答案 0 :(得分:2)
在一个for-loop
中:
static char[] moveWhiteSpaces(char[] input){
char[] result = new char[input.length];
int _char = 0, space = input.length-1;
boolean first_occur = true, first_char = false;
for(int i = 0; i < input.length; i++){
char c = input[i];
if(c != ' '){
result[_char++] = c;
first_char = true;
}else if (c == ' ' && first_occur && first_char){
result[_char++] = c;
first_occur = false;
}else{
result[space--] = c;
}
}
return result;
}
char[] input = "Hello world! Hello world! hahah hohohoh hehehe random! ! ! :)".toCharArray();
System.out.println(moveWhiteSpaces(input));
Hello world! Hello world! hahah hohohoh hehehe random! ! ! :)
答案 1 :(得分:1)
!是需要特殊待遇的。但是然后:
private static void moveSpaces(char[] input) {
int newPos = 0;
for (int oldPos = 0; oldPos < input.length; oldPos++) {
if ((input[oldPos] == '!') && (input[oldPos - 1] == ' ')) {
input[newPos - 1] = '!';
} else if (input[oldPos] != ' ') {
input[newPos] = input[oldPos];
newPos++;
} else if ((oldPos > 0) && (input[oldPos - 1] != ' ')) {
input[newPos] = input[oldPos];
newPos++;
}
}
for (int i = newPos; i < input.length; i++) {
input[i] = ' ';
}
}
答案 2 :(得分:1)
String t = "Hello World !";
String s = "";
int c=0;
for(int i=0;i<t.length();i++)
{
String s1 = String.valueOf(t.charAt(i));
if(s1.equals(" "))
{
if( String.valueOf(s.charAt(s.length()-1)).equals(" ")) {
c++;
}
else
{
s=s+s1;
}
}
else
{
s=s+s1;
}
}
for(int i=0;i<c;i++)
{
s=s+" ";
}
答案 3 :(得分:1)
您可以执行以下操作,而不是将char放入一个数组中:
public char[] moveWhiteSpaces(char[] inArr){
// Prepare an array for your result
char[] result = new char[inArr.length];
// Counter on the characters already added into the result
int filled = 0;
for(int i = 0; i < inArr.length; i++){
char c = inArr[i];
// Add the char to the result if it is not a space or if the last added char was a space
if(c != ' ' || (filled > 0 && result[filled-1] != ' ')){
result[filled++] = c;
}
}
// Complete the result with missing spaces
while(filled < inArr.length) {
result[filled++] = ' ';
}
return result;
}
这将返回:
"Hello world ! "
有时候创建中间对象/变量(在这种情况下为char[] result
)更加容易,也更具可读性。
答案 4 :(得分:0)
可以避免使用StringBuilder
。
public static char[] moveWhiteSpaces(char[] arr) {
char[] res = fill(new char[arr.length], ' ');
for (int i = 0, j = 0; i < arr.length; i++) {
if (isAlphabetic(arr[i]))
res[j++] = arr[i];
else if (isWhitespace(arr[i])) {
if (i > 0 && !isWhitespace(res[j - 1]))
res[j++] = arr[i];
} else
res[j == 0 ? j++ : j - 1] = arr[i];
}
return res;
}
private static char[] fill(char[] arr, char ch) {
for (int i = 0; i < arr.length; i++)
arr[i] = ch;
return arr;
}
private static boolean isAlphabetic(char ch) {
if (ch >= 'a' && ch <= 'z')
return true;
if (ch >= 'A' && ch <= 'Z')
return true;
if (ch >= '0' && ch <= '9')
return true;
return false;
}
private static boolean isWhitespace(char ch) {
return ch == ' ' || ch == '\t';
}
答案 5 :(得分:0)
希望这会有所帮助
public static void main(String[] args) {
char[] input = "Hello world! ".toCharArray();
char[] output = new char[input.length];
int count =0;
int k=0;
for(int i=0;i<=input.length-2;i++) {
if(input[i] == input[i+1] && input[i] == ' ') {
count++;
}else {
output[k] = input[i];
k++;
}
}
output[k] = input[input.length-1];//assigning the last variable
for(int j=0;j<count;j++) {
output[++k] = ' '; // adding the spaces to the end
}
ArrayList<Character> c = new ArrayList<>() ;//Arraylist is just to print your answer
//in a meaningful way
for(int i=0;i<output.length-1;i++) {
//System.out.println(output[i]);
c.add(output[i]);
}
System.out.println(c);
}
答案 6 :(得分:0)
我的工作方式类似于@Nirekin,直接与chars工作。
public char[] moveWhiteSpaces(char[] inArr){
char[] output = new char[inArr.length];
boolean lastCharWhiteSpace = false;
int lastIndex = 0;
for(int i = 0; i<inArr.length; i++){
char currChar = inArr[i];
if (' ' == currChar) {
if (!lastCharWhiteSpace) {
output[lastIndex] = currChar;
lastIndex++;
}
lastCharWhiteSpace = true;
} else {
output[lastIndex] = currChar;
lastIndex++;
lastCharWhiteSpace = false;
}
}
for(int i = lastIndex; i<output.length; i++) {
output[i] = ' ';
}
return output;
}