면접문제 수집(12)(문자열 반전)
package StingSub;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
public class ReverseStr {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(reverseStack("wokessd.cm .cok .ddj dd*&4 dlk"));
System.out.println(reverseDeque("wokessd.cm .cok .ddj dd*&4 dlk"));
System.out.println(reverse("wokessd.cm .cok .ddj dd*&4 dlk", "cok"));
System.out.println(reverseByInfo("wokessd.cm .cok .ddj dd*&4 dlk"));
}
public static String reverseStack(String stringInfo) {
if (stringInfo == null) {
throw new NullPointerException("info is null.");
}
StringBuffer buffer = new StringBuffer("");
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < stringInfo.length(); i++) {
stack.push(stringInfo.charAt(i));
}
while (!stack.empty()) {
buffer.append(stack.pop());
}
return buffer.toString();
}
public static String reverseDeque(String stringInfo) {
if (stringInfo == null) {
throw new NullPointerException("info is null.");
}
StringBuffer buffer = new StringBuffer("");
Deque<Character> deque = new ArrayDeque<Character>();
for (int i = 0; i < stringInfo.length(); i++) {
deque.addFirst(stringInfo.charAt(i));
}
while (!deque.isEmpty()) {
buffer.append(deque.removeFirst());
}
return buffer.toString();
}
public static String reverse(String stringInfo, String noReverse) {
if (stringInfo == null || noReverse == null) {
throw new NullPointerException("info is null.");
}
StringBuffer buffer = new StringBuffer("");
Stack<Character> stack = new Stack<Character>();
boolean iscontain = false;
for (int i = 0; i < stringInfo.length(); i++) {
iscontain = true;
for (int j = 0; j < noReverse.length(); j++) {
if ((i + j) > stringInfo.length()
|| (stringInfo.charAt(i + j) != noReverse.charAt(j))) {
iscontain = false;
break;
}
}
if (iscontain) {
for (int j = noReverse.length() - 1; j >= 0; j--) {
stack.push(noReverse.charAt(j));
}
i += noReverse.length() - 1;
} else {
stack.push(stringInfo.charAt(i));
}
}
while (!stack.empty()) {
buffer.append(stack.pop());
}
return buffer.toString();
}
public static String reverseByInfo(String stringInfo) {
if (stringInfo == null) {
throw new NullPointerException("info is null.");
}
StringBuffer buffer = new StringBuffer("");
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < stringInfo.length(); i++) {
stack.push(stringInfo.charAt(i));
}
while (!stack.empty()) {
buffer.append(stack.pop());
}
String reverseall = buffer.toString();
buffer.delete(0, buffer.length());
for (int i = 0; i < reverseall.length(); i++) {
if (reverseall.charAt(i) == ' ' || reverseall.charAt(i) == '.') {
while (!stack.empty()) {
buffer.append(stack.pop());
}
buffer.append(reverseall.charAt(i));
} else {
stack.push(reverseall.charAt(i));
}
}
while (!stack.empty()) {
buffer.append(stack.pop());
}
return buffer.toString();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
LintCode - 순차적으로 숫자를 인쇄합니다.1에서 최대 N까지의 정수를 반복하는 방법으로 찾습니다. 예제 제시N = 1, 반환[1,2,3,4,5,6,7,8,9]. 제시N = 2, 반환[1,2,3,4,5,6,7,8,9,10,11,...,99]. 주의 다음과 같...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.