class Solution {
public int[]twoSum(int[] nums,int target){
int[] res ={
-1,-1};
Map<Integer,Integer> map = new HashMap<>();for(int i =0;i < nums.length;i ++) map.put(nums[i],i);for(int i =0;i < nums.length;i ++){
if(map.containsKey(target - nums[i])&& map.get(target - nums[i])!= i){
res[0]= i;
res[1]= map.get(target - nums[i]);}}return res;}}
参考实现
class Solution {
public int[]twoSum(int[] nums,int target){
// define hashmap to store the value
HashMap<Integer,Integer> dict = new HashMap<>();for(int i =0;i < nums.length;i ++){
int compart = target - nums[i];if(dict.containsKey(compart)){
return new int[]{
i,dict.get(compart)};}else{
dict.put(nums[i],i);}}return new int[]{
-1,-1};}}
class Solution {
public List<List<String>>groupAnagrams(String[] strs){
List<List<String>> res = new ArrayList<>();
Map<String,Integer> map = new HashMap<>();for(String str :strs){
char[] temp = new char[str.length()];for(int i =0;i < str.length();i ++) temp[i]= str.charAt(i);
Arrays.sort(temp);
String tempStr = new String(temp);if(map.containsKey(tempStr)){
res.get(map.get(tempStr)).add(str);}else{
map.put(tempStr,res.size());
List<String> list = new ArrayList<>();
list.add(str);
res.add(list);}}return res;}}
到站了,第一个站台就刷了两道题,差不多十五分钟一道做过的题目。
参考实现
参考代码更加简洁,思路基本上一致,来人了,没有充电线,所以要等等了
很幸运,无锡站没人上来,一直到下一站还是继续使用充电线!
classSolution{
publicList<List<String>>groupAnagrams(String[] strs){
// define the res List<List<String>> res =newArrayList<>();Map<String,ArrayList<String>> dict =newHashMap<>();// traverse all the elementsfor(String x : strs){
// sort the chars in xchar[] temp = x.toCharArray();Arrays.sort(temp);String strTemp =String.valueOf(temp);if(dict.containsKey(strTemp)){
dict.get(strTemp).add(x);}else{
dict.put(strTemp,newArrayList<String>(Arrays.asList(x)));}}// traverse the map for(String x : dict.keySet()){
res.add(dict.get(x));}return res;}}
class Solution {
public intlongestConsecutive(int[] nums){
int res =0;
Set<Integer> set = new HashSet<>();for(int x: nums) set.add(x);for(int x :nums){
if(set.contains(x -1))continue;else{
int curNum =0;while(set.contains(x)){
x++;
curNum