代码改变世界

随笔分类 -  ACM

Cracking the coding interview--Q1.4

2013-07-15 11:14 by Loull, 230 阅读, 收藏,
摘要: 题目原文:Write a method to decide if two strings are anagrams or not.译文:写一个函数判断两个字符串是否是变位词。解答变位词(anagrams)指的是组成两个单词的字符相同,但位置不同的单词。比如说, abbcd和abcdb就是一对变位词。该题目有两种做法:O(nlogn)的解法由于组成变位词的字符是一模一样的,所以按照字典序排序后,两个字符串也就相等了。 因此我们可以用O(nlogn)的时间去排序,然后用O(n)的时间比较它们是否相等即可。代码如下: public static boolean isAnagraml(Strin... 阅读全文

Cracking the coding interview--Q1.3

2013-07-15 10:48 by Loull, 203 阅读, 收藏,
摘要: 题目原文:Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.FOLLOW UPWrite the test cases for this method.译文:设计算法并写出代码移除字符串中重复的字符,不能使用额外的缓存空间。注意: 可以使用额外的一 阅读全文

Cracking the coding interview--Q1.2

2013-07-11 17:02 by Loull, 267 阅读, 收藏,
摘要: 题目原文:Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)译文:写代码翻转一个C风格的字符串。(C风格的意思是"abcd"需要用5个字符来表示,包含末尾的 结束字符)解答这道题如果就是要考察你有没有注意到C风格字符串最后的那个结束符,那我觉得还是像书 上写的那样,在代码中有所体现。代码如下:java:package cha1;public class A002 阅读全文

Cracking the coding interview--Q1.1

2013-07-09 13:58 by Loull, 261 阅读, 收藏,
摘要: 出处:http://hawstein.com/posts/1.1.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处。题目原文:Implement an algorithm to determine if a string has all unique characters.What if you can not use additional data structures?译文:实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构。(即只使用基本的数. 阅读全文

JOJ1202。重新操刀ACM,一天一练!做个简单的题目温习。

2013-07-08 22:50 by Loull, 438 阅读, 收藏,
摘要: http://ac.jobdu.com/problem.php?pid=1202题目描述: 对输入的n个数进行排序并输出。输入: 输入的第一行包括一个整数n(1#include #include using namespace std;int main(){ freopen("in.txt","r",stdin); int n; while(scanf("%d", &n)!=EOF && n) { int a[102]; for (int i=0; i list = new ArrayList(); for ( 阅读全文