- 浏览: 139845 次
-
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = "makes", word2 = "coding", return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
[分析]
在构造函数将各单词出现的所有位置保存在HashMap中,调用shortest()方法时,从HashMap中获取两单词的位置列表,两个列表是已排序的,因此可使用类似MergeSort的思路求解最短距离。
[ref]
https://leetcode.com/discuss/50190/java-solution-using-hashmap
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = "makes", word2 = "coding", return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
[分析]
在构造函数将各单词出现的所有位置保存在HashMap中,调用shortest()方法时,从HashMap中获取两单词的位置列表,两个列表是已排序的,因此可使用类似MergeSort的思路求解最短距离。
[ref]
https://leetcode.com/discuss/50190/java-solution-using-hashmap
public class WordDistance { private HashMap<String, List<Integer>> indexer = new HashMap<String, List<Integer>>(); public WordDistance(String[] words) { if (words == null) return; for (int i = 0; i < words.length; i++) { if (indexer.containsKey(words[i])) { indexer.get(words[i]).add(i); } else { List<Integer> positions = new ArrayList<Integer>(); positions.add(i); indexer.put(words[i], positions); } } } public int shortest(String word1, String word2) { List<Integer> posList1 = indexer.get(word1); List<Integer> posList2 = indexer.get(word2); int i = 0, j = 0; int diff = Integer.MAX_VALUE; while (i < posList1.size() && j < posList2.size()) { int pos1 = posList1.get(i), pos2 = posList2.get(j); if (pos1 < pos2) { diff = Math.min(diff, pos2 - pos1); i++; } else { diff = Math.min(diff, pos1 - pos2); j++; } } return diff; } } // Your WordDistance object will be instantiated and called as such: // WordDistance wordDistance = new WordDistance(words); // wordDistance.shortest("word1", "word2"); // wordDistance.shortest("anotherWord1", "anotherWord2");
发表评论
-
Leetcode - H-Index
2015-09-06 09:08 1138Given an array of citations (ea ... -
Leetcode - LRU Cache
2015-09-03 18:31 551[分析] 自己使用HashMap + LinkedList/A ... -
Leetcode - Max Points on a Line
2015-08-23 15:30 742[分析] 两条直线若包含一个公共点且斜率相同,则为同一条直线。 ... -
Leetcode - Fraction to Recurring Decimal
2015-08-23 10:05 482[分析] 处理int型整数运算时,为避免溢出,省事的做法就是内 ... -
Leetcode - Isomorphic Strings
2015-08-23 09:51 561[分析] 思路1:维护两个哈希表,char[] map, bo ... -
Leetcode - Palindrome Permutation
2015-08-22 16:24 1205[分析] 思路2让我大开眼界,顺便学习下BitSet~ [re ... -
Leetcode - Group Shifted String
2015-08-22 16:20 1745Given a string, we can "sh ... -
Leetcode - Two Sum III - Data Structure Design
2015-08-21 10:30 709[分析] find的version1 版本注意num2Occu ... -
Leetcode - Add Binary
2015-08-21 09:28 484[分析] 从低位往高位逐位相加,就是这么一个简单的题却花了我一 ... -
Leetcode - Longest Consecutive Sequence
2015-08-20 21:20 499[分析] base version说几句: 数组题一定要考虑重 ... -
Leetcode - First Missing Positive
2015-08-20 07:45 668[分析] 将各个正数放入相应下标处,使之满足nums[nums ... -
Leetcode - Set Matrix Zeros
2015-08-19 20:55 568Given a m x n matrix, if an ele ... -
Leetcode - Rotate Image
2015-08-19 19:51 508[分析] 自己的思路:从外到内一圈圈顺时针旋转90度,坐标映射 ... -
Missing Ranges
2015-08-19 09:48 526[分析] 此题若不考虑极大值极小值相关的corner case ... -
Leetcode - 3Sum Smaller
2015-08-18 22:12 1503Given an array of n integers nu ... -
Leetcode - Spiral Matrix II
2015-08-18 19:49 509Given an integer n, generate a ... -
Leetcode - Spiral Matrix
2015-08-18 09:50 442Given a matrix of m x n element ... -
Leetcode - Contains Duplicate II
2015-08-18 07:57 571Given an array of integers and ... -
Leetcode - Shortest Word Distance III
2015-08-17 22:05 1685This is a follow up of Shortest ... -
Leetcode - Shortest Word Distance
2015-08-17 22:01 937Given a list of words and two w ...
相关推荐
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...
137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [C++](./C++/single-number-ii.cpp) [Python](./Python/single-number-ii.py) | _O(n)_ | _O(1)_ | Medium ||| 190 | [Reverse Bits]...
5. **二叉树与图**:二叉树题目如"Binary Tree Inorder Traversal"(二叉树的中序遍历)和"Lowest Common Ancestor of a Binary Tree"(二叉树的最近公共祖先),图题目如"Shortest Path in Bidirectional Graph"...
例如,"最短单词距离"(Shortest Word Distance)可能需要使用队列来辅助计算。 5. **哈希表**:哈希表提供快速的查找和插入操作,常用于计数、去重、查找最近元素等。例如,"两数之和"的优化版本可以用哈希表在 O...
在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!
前端分析-2023071100789
基于kinect的3D人体建模C++完整代码.cpp
搞机工具箱10.1.0.7z
GRU+informer时间序列预测(Python完整源码和数据),python代码,pytorch架构,适合各种时间序列直接预测。 适合小白,注释清楚,都能看懂。功能如下: 代码基于数据集划分为训练集测试集。 1.多变量输入,单变量输出/可改多输出 2.多时间步预测,单时间步预测 3.评价指标:R方 RMSE MAE MAPE,对比图 4.数据从excel/csv文件中读取,直接替换即可。 5.结果保存到文本中,可以后续处理。 代码带数据,注释清晰,直接一键运行即可,适合新手小白。
在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!
基于ANSYS LSDyna的DEM-SPH-FEM耦合模拟滑坡入水动态行为研究,基于ANSYS LSDyna的DEM-SPH-FEM耦合的滑坡入水模拟分析研究,基于ansys lsdyna的滑坡入水模拟dem-sph-fem耦合 ,基于ANSYS LSDyna; 滑坡入水模拟; DEM-SPH-FEM 耦合,基于DEM-SPH-FEM耦合的ANSYS LSDyna滑坡入水模拟
auto_gptq-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
复件 复件 建设工程可行性研究合同[示范文本].doc
13考试真题最近的t64.txt
好用我已经解决报错问题
# 踏入C语言的奇妙编程世界 在编程的广阔宇宙中,C语言宛如一颗璀璨恒星,以其独特魅力与强大功能,始终占据着不可替代的地位。无论你是编程小白,还是有一定基础想进一步提升的开发者,C语言都值得深入探索。 C语言的高效性与可移植性令人瞩目。它能直接操控硬件,执行速度快,是系统软件、嵌入式开发的首选。同时,代码可在不同操作系统和硬件平台间轻松移植,极大节省开发成本。 学习C语言,能让你深入理解计算机底层原理,培养逻辑思维和问题解决能力。掌握C语言后,再学习其他编程语言也会事半功倍。 现在,让我们一起开启C语言学习之旅。这里有丰富教程、实用案例、详细代码解析,助你逐步掌握C语言核心知识和编程技巧。别再犹豫,加入我们,在C语言的海洋中尽情遨游,挖掘无限可能,为未来的编程之路打下坚实基础!
auto_gptq-0.4.2-cp38-cp38-win_amd64.whl
自动立体库设计方案.pptx
# 踏入C语言的奇妙编程世界 在编程的广阔宇宙中,C语言宛如一颗璀璨恒星,以其独特魅力与强大功能,始终占据着不可替代的地位。无论你是编程小白,还是有一定基础想进一步提升的开发者,C语言都值得深入探索。 C语言的高效性与可移植性令人瞩目。它能直接操控硬件,执行速度快,是系统软件、嵌入式开发的首选。同时,代码可在不同操作系统和硬件平台间轻松移植,极大节省开发成本。 学习C语言,能让你深入理解计算机底层原理,培养逻辑思维和问题解决能力。掌握C语言后,再学习其他编程语言也会事半功倍。 现在,让我们一起开启C语言学习之旅。这里有丰富教程、实用案例、详细代码解析,助你逐步掌握C语言核心知识和编程技巧。别再犹豫,加入我们,在C语言的海洋中尽情遨游,挖掘无限可能,为未来的编程之路打下坚实基础!
在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!