Cracking the coding interview--Q1.2
原文
Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
译文
用C或者C++实现一个void reverse(char* str)的函数来逆转一个已null终止的字符串
解答
在C++里,设置俩指针,一个指向字符串头,一个指向尾,然后每次交换一个字符和,指针分别像中间移就行了。这里借用一下Hawstein(http://hawstein.com/posts/1.2.html)的代码好了:
#include <iostream>
#include <cstring>
using namespace std; void swap(char &a, char &b){
a = a^b;
b = a^b;
a = a^b;
} void reverse2(char *s){
int n = strlen(s);
for(int i=; i<n/; ++i)
swap(s[i], s[n-i-]);
} void reverse1(char *s){
if(!s) return;
char *p = s, *q = s;
while(*q) ++q;
--q;
while(p < q)
swap(*p++, *q--);
} int main(){
char s[] = "";
reverse1(s);
cout<<s<<endl;
return ;
}
那么如果在Java里能不能也用同样的方法呢?如果简单的用String并且用到上面相同的方法的话,是不行的。因为String是一个不可变的对象,所以swap其中两个字符并不会真的交换。
在Java里要实现逆转字符串的话可以用以下的方法:
public class Main {
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
public static void main(String args[]) {
String s1 = "i am hawstein.";
String s2 = "abcdefghijklmnopqrstuvwxyzABCD1234567890";
System.out.println(reverse(s1));
System.out.println(reverse(s2));
}
}
Cracking the coding interview--Q1.2的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- CapsLock indicator on Ubuntu for Thinkpad
http://askubuntu.com/questions/292535/how-to-get-caps-num-scroll-lock-keys-osd-notification sudo add ...
- Exception in thread "main" com.sun.xml.internal.ws.streaming.XMLStreamReaderException: unexpected XML tag.
webservice 抛异常,原因: public class HeaderHandler implements SOAPHandler<SOAPMessageContext>{ @Ove ...
- MVC - 知识点
1. @Styles.Render("~/Content/css") 是怎么工作的? 在App_Start文件夹里面的BundleConfig.cs中定义了StyleBu ...
- iOS 通过个推 推送原理
目前使用过的第三方推送很多,有极光, 友盟,个推等,现在主要针对个推,谈谈我对推送流程的理解. 在项目中,如果想要实现评论 推送功能 需要进行以下步骤: 1. 在用户登录的时候 通过 [GeTui ...
- Android(java)学习笔记258:JNI之hello.c(c代码功能实现)指针语法解析
1. 接下来我们细讲分析一下前面一讲中,c功能实现的代码: (1)hello.c : #include <jni.h> char* getHello() { //////// return ...
- 【iOS之轮播视图、自定义UIPageControl】
基于UISrollView实现的无限循环轮播视图. 实现的思路:使用三个UIImageView不断循环利用,始终将最中间一个View显示在UIScrolView的contentSize上,每次滚动后, ...
- java07循环结构
public class WhileTest { // while循环结构 public static void main(String[] args) { System.out.println(&q ...
- hdu 1175
#include <iostream> #include <string> #include <stdio.h> using namespace std; int ...
- 《node.js开发指南》读书笔记(一)
在开发时如果修改了js内容,不能通过刷新浏览器直接看到效果,必须通过重启nodejs程序才能看到,这样显然不利于开发调试,supervisor可以实现这个功能,监视对代码的改动,并自动重启nodejs ...
- eclipse - An internal error occurred during: "Running Android Lint"
概述 也不晓得为什么,编译eclipse,设置打开,就自动报错: An internal error occurred during: "Running Android Lint" ...