博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode之Anagrams
阅读量:7225 次
发布时间:2019-06-29

本文共 1320 字,大约阅读时间需要 4 分钟。

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

这道题的意思是返回含有相同字母的字符串

First, I don't know the real meaning of this problem. Next ,After searching the answer through Internet,I also feel puzzled.

Last, I have no experience about the function of HashMap or Hashtable.

I feel exhausted about the current of my attitude. Anyway What I should do is try my best.

Fighting!

慢慢积累

char[] aChar = a.toCharArray();

add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素,而addAll是传入一个List,将此List中的所有元素加入到当前List中,也就是当前List会增加的元素个数为传入的List的大小

不多说了,今天要再刷3道题,然后写论文!

下面附上代码:

public List
anagrams(String[] strs) { ArrayList
result = new ArrayList
(); HashMap
> ht = new HashMap
>(); for(int i =0;i
val = ht.get(sor); if(val!=null){ val.add(strs[i]); } else{ val = new ArrayList
(); val.add(strs[i]); ht.put(sor,val); } } Set
set = ht.keySet(); for(String s : set){ ArrayList
val = ht.get(s); if(val.size()>1){ result.addAll(val); } } return result; } public String sorted(String a){ char[] aChar = a.toCharArray(); Arrays.sort(aChar); return new String(aChar); }

  

 

转载于:https://www.cnblogs.com/gracyandjohn/p/4503447.html

你可能感兴趣的文章
python学习笔记(9)-python编程风格
查看>>
Apache HTTP Server搭建虚拟主机
查看>>
(译).NET4.X 并行任务中Task.Start()的FAQ
查看>>
git log显示
查看>>
java中相同名字不同返回类型的方法
查看>>
Rails NameError uninitialized constant class solution
查看>>
Android 获取SDCard中某个目录下图片
查看>>
设置cookies第二天0点过期
查看>>
【转载】NIO客户端序列图
查看>>
poj_2709 贪心算法
查看>>
【程序员眼中的统计学(11)】卡方分布的应用
查看>>
文件夹工具类 - FolderUtils
查看>>
http://blog.csdn.net/huang_xw/article/details/7090173
查看>>
lua学习例子
查看>>
研究:印度气候变暖速度加剧 2040年或面临重灾
查看>>
python爬虫——爬取豆瓣TOP250电影
查看>>
C++与Rust操作裸指针的比较
查看>>
了解webpack-4.0版本(一)
查看>>
如何培养良好的编程风格
查看>>
Netty Channel源码分析
查看>>