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 ...
随机推荐
- 2017.10.28 QB模拟赛 —— 下午
题目链接 T1 按x值排序 遇到第二种牌插入 遇到第一种牌 查询<=y 的最小值 删除他 splay multiset cys大佬说 multiset就是不去重的set, #include &l ...
- Kalman filter, Laser/Lidar measurement
You can download this project from https://github.com/lionzheng10/LaserMeasurement The laser measure ...
- java 使用hashmap一个键对应多值的方法
背景:在你使用map对象时,你可能会有一个key,对应多个值的需求 实现: import java.util.ArrayList; import java.util.HashMap; import j ...
- ARM是CPU体系结构
https://zhidao.baidu.com/question/680620766286548532.html ARM是一种使用精简指令(RISC)的CPU,有别于英特尔的复杂指令(CISC) x ...
- firewalld 使用简介
学习apache安装的时候需要打开80端口,由于centos 7版本以后默认使用firewalld后,网上关于iptables的设置方法已经不管用了,想着反正iptable也不会用,索性直接搬官方文档 ...
- linux命令之grep命令
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...
- 利用python中的PIL进行矩阵与图像之间的转换
1.图像转换为矩阵 matrix = numpy.asarray(image) 2.矩阵转换为图像 image = Image.fromarray(matrix)
- caffe新版本的各种软件
系统重装了,于是,我想装就体验一下最新的各种东西吧. anaconda最新的 cuda最新的 cudnn最新的 本来安装好了没问题.caffe编译也通过了.但是不能用,缺少python opencv和 ...
- python的**和*
1.**两个乘号就是乘方,比如2**4,结果就是2的4次方,结果是16一个乘号*,如果操作数是两个数字,就是这两个数字相乘,如2*4,结果为8*如果是字符串.列表.元组与一个整数N相乘,返回一个其所有 ...
- 3.Netty的粘包、拆包(二)
Netty提供的TCP数据拆包.粘包解决方案 1.前言 关于TCP的数据拆包.粘包的介绍,我在上一篇文章里面已经有过介绍. 想要了解一下的,请点击这里 Chick Here! 今天我们要讲解的是Net ...