今天去面试时笔试,有两道字符串操作的拿出来给大家看下(1)abcd1234efgh 怎样转变成 efgh5678dcba 代码最短
(2)为字符串的两个变量引用 a,b 怎样使其值交换。不能出现第三个中间变量。第一题我算是搞出来了,不知道是不是最短的。想看看大家怎么弄。
第二题当时没想出来,后来在网上查到了找工作的或许可以参考下 ^_^
(2)为字符串的两个变量引用 a,b 怎样使其值交换。不能出现第三个中间变量。第一题我算是搞出来了,不知道是不是最短的。想看看大家怎么弄。
第二题当时没想出来,后来在网上查到了找工作的或许可以参考下 ^_^
public class Test24
{
public static void main(String... args) {
String str1 = "abcd"; // abcdefg
String str2 = "efg";
str1 = (str2 + ( str2 = str1)).replace(str2,"") ;
System.out.println("str1: " + str1 + "," + "str2: " + str2);
}
}output: str1: efg,str2: abcd第一题我看直接赋值就最短,,哈哈。。
后来不得不一个个取出来加上4再放进去的
public class Test26
{
public static void main(String... args) {
//(1)abcd1234efgh 怎样转变成 efgh5678dcba 代码最短
// efgh
// 1357 String str = "abcd1234efgh";
String result = str;
int j = 3;
for(int i = (str.length() - 1); i >=0 ; i--) {
if( i >= 8) {
result = result.replace(result.charAt(i),
(char)(result.charAt(i) - ( 2*j + 1)));
j--;
}
if( i < 8) {
result = result.replaceFirst(String.valueOf(result.charAt(i)),String.valueOf((char)(result.charAt(i) + 4)));
}
}
System.out.println(result);
}
}
希望有更好的答案出现。
public class Test26
{
public static void main(String... args) {
//(1)abcd1234efgh 怎样转变成 efgh5678dcba 代码最短
// efgh
// 1357 String str = "abcd1234efgh";
String result = str;
int j = 3;
for(int i = (str.length() - 1); i >=0 ; i--) {
if( i >= 8) {
result = result.replace(result.charAt(i),
(char)(result.charAt(i) - ( 2*j + 1)));
j--;
}
if( i < 8) {
result = result.replaceFirst(String.valueOf(result.charAt(i)),
String.valueOf((char)(result.charAt(i) + 4)));
}
}
System.out.println(result);
}
}
String str1 = "abcd"; // abcdefg
String str2 = "efg";1、str2 = str1 --> str2 = "abcd"
2、"efg" + "abcd" --> "efgabcd"
3、"efgabcd".replace("abcd",""); --> efg
有什么问题?输出不是:
efgh5678dcba 吗?
我是回答(切尔斯基) 论坛常发生的“脏读”哈哈关于第一题,我用的StringBuffer。转来转去的,怎么样,晕了吧,感觉代码其实很长,我只是把他那样缩着,哈哈。用MyEclipse写发现当时用笔答的有好几处错误。感觉我的和你的结合下可能会短点。public class Test2 {
/**
* abcd1234efgh 怎样转变成 efgh5678dcba 代码最短
* @param sb
*/
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcd1234efgh");
for(int i = 4; i < 8; i++) {
sb = sb.replace(i, i+1, String.valueOf(Integer.parseInt(new Character(sb.charAt(i)).toString()) + 4 ));
}
sb = sb.replace(0, 4, new StringBuffer(sb.reverse().substring(0, 4)).reverse().toString());
sb = sb.replace(4, 8, new StringBuffer(sb.substring(4, 8)).reverse().toString());
System.out.println(sb);
}
}
/**
* abcd1234efgh 怎样转变成 efgh5678dcba 代码最短
* @param sb
*/
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcd1234efgh");
for(int i = 4; i < 8; i++) {
sb = sb.replace(i, i+1, String.valueOf(Integer.parseInt(new Character(sb.charAt(i)).toString()) + 4 ));
}
sb = sb.replace(0, 4, new StringBuffer(sb.reverse().substring(0, 4)).reverse().toString());
sb = sb.replace(4, 8, new StringBuffer(sb.substring(4, 8)).reverse().toString());
System.out.println(sb);
}
}
试试下面个组数据:
String str1 = "abc";
String str2 = "abcd";输出:str1: d,str2: abc
谢谢!!那就下面这个:public class Test24
{
public static void main(String... args) {
//abcdeabcd
String str1 = "abcd"; // abcdefg
String str2 = "abcdeeeabcd";
str1 = ( str2 + str1 ).substring(0,(str2 + ( str2 = str1)).lastIndexOf(str1)) ;
System.out.println("str1: " + str1 + "," + "str2: " + str2);
}
}
public class Test24
{
public static void main(String... args) {
String str1 = "abcd";
String str2 = "efg";
str1 += str2;
str2 = str1.substring(0, str1.length()-str2.length());
str1 = str1.substring(str2.length());
System.out.println("str1: " + str1 + " " + "str2: " + str2);
}
}
public static void main(String args[]) {
String str="abcd1234efgh";
char[] temp=str.toCharArray();
for(int i=0;i<temp.length;i++){
temp[i]+=4-i/8*(2*(temp[i]-'a')-3);
}
System.out.println(new String(temp));
}
}要不要把temp变以t以使代码变短?
---第二题
a=a+b
b=a-b
a=a-b
---是这样吗?
// String str="abcd1234efgh";
String str1 = "ba342abe";
char[] temp=str1.toCharArray();
for(int i=0;i<temp.length;i++){
temp[i]+=4-i/8*(2*(temp[i]-'a')-3);
}
System.out.println(new String(temp));
}输出结果为: fe786efi
对于楼上的做法,再怎么样做都只是新生成了一个字符串常量,然后通过str重新指向新生成的字符串.
既然这样,题目告诉你了要变成什么样的字符串,那还不如直接str="efgh5678dcba".
所以我觉得我的答案是这样的,呵呵:
public class Test
{
public static void main(String... args) {
String str1 = "abcd1234efgh";
System.out.println(str1="efgh5678dcba")
}
}
这里引用上面某位仁兄的代码,大家打印下就知道我说的什么意思了.
public class Test { public static void main(String[] args) {
String str = "abcd1234efgh";
String str2 = "abcd1234efgh";
String result = str;
int j = 3;
for(int i = (str.length() - 1); i >=0 ; i--) {
if( i >= 8) {
result = result.replace(result.charAt(i),
(char)(result.charAt(i) - ( 2*j + 1)));
j--;
}
if( i < 8) {
result = result.replaceFirst(String.valueOf(result.charAt(i)),
String.valueOf((char)(result.charAt(i) + 4)));
}
}
System.out.println(result);
System.out.println(str);
str = result;
System.out.println(str);
System.out.println(str2);
}}既然终究要new一个新字符串,莫不如直接new一个啊!
然后取符号位判断是哪边的字符,用一个类似触发器的算法实现,左边就是0,1 右边就是1,0
d左边的字符加4,d右边的字符以d为分界线对称到左边字符
public class Test24{
public static void main(String args[]) {
String str="abcd1234efgh";
char[] temp=str.toCharArray();
for(int i=0;i<temp.length;i++){
char t = temp[i];
temp[i]+=(2*(100-t)+1)*((100-t)>>>31) + 4*(1-((100-t)>>>31));
}
System.out.print(temp);
}
}
String a = "abc";
String b = "efg";
a = a ^ b;
b = a ^ b;
a = a ^ b;
不行哎,字符串用不了
The operator ^ is undefined for the argument type(s) java.lang.String, java.lang.String
这个应该可以了吧
String s2 = "efgh5678dcba";
s1 = s2;
System.out.println(s1);
第二题 String s1 = "123";
String s2 = "456";
s1 = s2 + (s2 = s1).replaceAll(".", "");
System.out.println(s1);
System.out.println(s2);
//1.
String str1 = "abcd";
String str2 = "efg"; str1 = str2 + (str2 = str1) == null ? "" : "";
System.out.println(str1);
System.out.println(str2);//2.
String str = "abcd1234efgh";
char[] c = str.toCharArray();
for (int i = 0; i < c.length; i++) {
c[i] += i < 8 ? 4 : 2 * ('d' - c[i]) + 1;
}
System.out.println(new String(c));
String regexString = "[a-z]+";
String regexNumber = "\\d+";
Pattern pString = Pattern.compile(regexString);
Pattern pNumber = Pattern.compile(regexNumber);
Matcher mString = pString.matcher(test);
Matcher mNumber = pNumber.matcher(test);
List<String> result = new LinkedList<String>();
boolean flag1 = true;
boolean flag2 = true;
while(flag1 | flag2){
if(mString.find()){
result.add(mString.group());
}else {
flag1=false;
}
if(mNumber.find()){
result.add(mNumber.group());
}else {
flag2=false;
}
}
System.out.println("before");
for (String string : result) {
System.out.print(string+" ");
}
System.out.println("after");
for(int i=result.size()-1;i>=0;i--){
System.out.print(result.get(i)+" ");
}
强大 先学习了。
这儿掉了个括号:
str1 = str2 + ((str2 = str1) == null ? "" : "");
这就交换了a,b的值了呀!
str1 = str2 + ((str2 = str1) == null ? "" : "");
我想了半天为什么要加括号,才注意到"+" 的优先级高于“==”。
package test;import java.io.UnsupportedEncodingException;//abcd1234efgh
public class Test {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = new String("abcd1234efgh");
System.out.print(Test.replace(str));
} public static String replace(String str)
throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
char c;
for (byte b : str.getBytes("utf-8")) {
c = (char) (b + 4);
sb.append(c);
}
return sb.toString();
}
}
上面错了,适用于整数
public static void main(String[] args) {
String str = "abcd1234efgh";
System.out.println(reverse(str));
}
public static String reverse(String str) {
if(str == null || str.length() < 2) {
return str;
}
char[] chs = str.toCharArray();
for(int i = 0, k = chs.length / 2; i < k; i++) {
char c = chs[i];
chs[i] = chs[chs.length - i - 1];
chs[chs.length - i - 1] = c;
}
return new String(chs);
}
}
public class test{
public static void main(String ... args){
String st = "abcd1234efgh";
Stack<Character> s = new Stack<Character>();
for(int i=0;i<st.length();i++)
s.push(st.charAt(i));
for(int i=0;i<st.length();i++)
System.out.print(s.pop());
}
}