lintcode
public class Solution {
/**
* @param s: The first string
* @param b: The second string
* @return true or false
*/
public boolean anagram(String s, String t) {
// write your code here
Map<String , Integer> sMap = new HashMap<String , Integer>();
Map<String , Integer> tMap = new HashMap<String , Integer>();
if(s.length() != t.length()){
return false;
}
int i=0;
int wCount=0;
while(i<s.length()){
if(sMap.containsKey(s.substring(i,i+1))){
wCount=sMap.get(s.substring(i,i+1)).intValue();
wCount++;
sMap.put(s.substring(i,i+1) , new Integer(wCount));
}
else{
sMap.put(s.substring(i,i+1) , new Integer(1));
}
i++;
}
i=0;
while(i<t.length()){
if(tMap.containsKey(t.substring(i,i+1))){
wCount=tMap.get(t.substring(i,i+1)).intValue();
wCount++;
tMap.put(t.substring(i,i+1) , new Integer(wCount));
}
else{
tMap.put(t.substring(i,i+1) , new Integer(1));
}
i++;
}
Set sEntrySet = sMap.entrySet();
Iterator sIterator = sEntrySet.iterator();
while(sIterator.hasNext()){
Map.Entry pair = (Map.Entry)sIterator.next();
if(! tMap.containsKey(pair.getKey()) ){
return false;
}else{
if(((Integer)tMap.get(pair.getKey())).intValue() != ((Integer)(pair.getValue())).intValue()){
return false;
}
}
}
return true;
}
}
public class Solution {
/**
* @param A : A string includes Upper Case letters
* @param B : A string includes Upper Case letter
* @return : if string A contains all of the characters in B return true else return false
*/
public boolean compareStrings(String A, String B) {
// write your code here
String strTemp;
int i=0;
while(i<B.length()){
if((( strTemp=A.replaceFirst(B.substring(i,i+1),"") ).compareTo(A)) ==0 ){
return false;
}
else{
A=strTemp;
}
i++;
}
return true;
}
}
class Solution {
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
public int strStr(String source, String target) {
// write your code here
int i=0;
if((source==null)||(target==null)) return -1;
while(i<(source.length()-target.length()+1)){
if(source.substring(i,i+target.length()).compareTo(target) == 0){
return i;
}
i++;
}
return -1;
}
}
lintcode的更多相关文章
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
- Lintcode 372. O(1)时间复杂度删除链表节点
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...
- Lintcode 469. 等价二叉树
----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...
- Lintcode 375.克隆二叉树
-------------------------- 水题 AC代码: /** * Definition of TreeNode: * public class TreeNode { * public ...
随机推荐
- java设置随机数教程
java作为程序猿开发人员都在使用的一款编程语言,许多入门的朋友都陷入了一个简单的问题就是,使用java开发时随机数要怎么设置?java怎么设置随机数?经常会有地方需要用到随机数,不用着急,一起来看看 ...
- PHP:__get()、__set()、__isset()、__unset()、__call()、__callStatic()六个魔术方法
哎呀呀,今天小仓鼠学到了魔术方法,简称魔法,哈哈哈哈,神经病啊~ 平时在面试的时候,也会遇到问魔术方法有哪些的问题哦!今天我们来了解一下下~ 1.__get() 形式: __get($objName) ...
- 数据结构(C#):图的最短路径问题、(Dijkstra算法)
今天曾洋老师教了有关于图的最短路径问题,现在对例子进行一个自己的理解和整理: 题目: 要求:变成计算出给出结点V1到结点V8的最短路径 答: 首先呢,我会先通过图先把从V1到V8的各种路径全部计算下来 ...
- 第三章 八位数字开关板&模拟输入板&火焰传感器
这节我将带大家了解亮宁机器人基础外接硬件. 八位数字板开关 接线方法:W1~W8接23~37号数字端口,Enter接39号数字端口,vcc和gnd分别接正负. #include <LNDZ.h& ...
- 进程、内存的理想与现实 VS 虚拟内存
理想情况下一个进程的运行,需要一块足够大的连续的内存进行装载. 现状: 1)内存不够大:分解进程内存空间. 2)内存不连续:内存映射.
- 【CCPC-Wannafly Winter Camp Day3 (Div1) F】小清新数论(莫比乌斯反演+杜教筛)
点此看题面 大致题意: 让你求出\(\sum_{i=1}^n\sum_{j=1}^n\mu(gcd(i,j))\). 莫比乌斯反演 这种题目,一看就是莫比乌斯反演啊!(连莫比乌斯函数都有) 关于莫比乌 ...
- IDEA的常用操作(快捷键)
IDEA的常用操作(快捷键) Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt ...
- 2月4号学习的一个SSM整合项目,第一课
本文引自:https://github.com/Sunybyjava/seckill 原作者:sunybyjava@gmail.com seckill 一个整合SSM框架的高并发和商品秒杀项目,学习 ...
- cityscape分割3类别数据处理
cpp: #include "cv.h" #include "highgui.h" #include <iostream> #include < ...
- C/C++语言代码规范
1.标识符名称: 标识符名称包括函数名.常量名.变量名等.这些名字应该能反映它所代表的实际东西,具有一定的意义,使其能 够见名知义,有助于对程序功能的理解.规则如下: 所有宏定义.枚举常数和const ...