leetcode网的典型练习题目
题目
字符串:3,49,30
线性表:86,16,27,732
队列:641,406,899
栈:946,116,117,895
哈希表:61,729,25,554
dfs:105,112,98,494,547,1254
bfs:1091,1129,102,101,752
字符串
3. 无重复字符的最长子串
public class Letcode3 {
/**
* 输入: s = "abcabcbb"
* 输出: 3
* 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
* <p>
* 思路 : 双指针思路,考虑单个字段的特殊情况,只顾右边界,下标左减右,
* 移除目标里的字段,挨着插入轮训
*
* @param args
*/
public static void main(String[] args) {
String s = "abcabcbb";
System.out.println(countString(s));
}
private static int countString(String s) {
if (s.length() <= 1) {
return s.length();
}
HashSet<Character> hashSet = new HashSet<>();
int l = 0;
int r = 1;
int result = 0;
hashSet.add(s.charAt(l));
while (r < s.length()) {
while (r < s.length() && !hashSet.contains(s.charAt(r))) {
hashSet.add(s.charAt(r));
r++;
}
result = Math.max(result, r - l);
hashSet.remove(s.charAt(l));
l++;
}
return result;
}
}
49. 字母异位词分组
public class Letcode49 {
/**
* 输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
* 输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
* <p>
* 思路 :字符串转toCharArray,可字母排序,hashmap key分组List处理
*
* @param args
*/
public static void main(String[] args) {
String[] str = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
System.out.println(dealData(str));
}
private static List<List<String>> dealData(String[] next) {
HashMap<String, List<String>> map = new HashMap<>();
for (String s : next) {
// s转为char可排序
char[] charArray = s.toCharArray();
Arrays.sort(charArray);
String s1 = String.valueOf(charArray);
if (map.containsKey(s1)) {
map.get(s1).add(s);
} else {
List<String> list = new ArrayList<>();
list.add(s);
map.put(s1, list);
}
}
List<List<String>> objects = new ArrayList<>(map.values());
return objects;
}
}
线性表
16. 最接近的三数之和
public class Letcode16 {
/**
* 思路 : 数组排序,前三数临时求和作为参照,双指针,左下右上,看谁接近目标值,打了缩右,小了缩左
*
* @param args
*/
public static void main(String[] args) {
int[] ints = new int[]{-1, 2, 1, -4};
System.out.println(threeSumClosest2(ints, 1));
}
private static int threeSumClosest2(int[] ints, int target) {
// 数组排序 -4 -1 1 2
Arrays.sort(ints);
// 临时求和参照
int tempSum = ints[0] + ints[1] + ints[2];
for (int j = 0; j < ints.length - 2; j++) {
int left = j + 1;
int right = ints.length - 1;
while (left != right) {
int sum = ints[j] + ints[left] + ints[right];
if (Math.abs(sum - target) < Math.abs(tempSum - target)) {
tempSum = sum;
}
if (sum > target) {
right--;
} else {
left++;
}
}
}
return tempSum;
}
27. 移除元素
public class Letcode27 {
public static void main(String[] args) {
Integer[] nums = new Integer[]{0, 1, 2, 2, 3, 0, 4, 2};
int val = 2;
List<Integer> list1 = new ArrayList<>();
// List<String> list2 = new ArrayList<>();
for (int anInt : nums) {
if (anInt != val) {
list1.add(anInt);
}
// else {
// list2.add("_");
// }
}
int size = list1.size();
// TODO 不明太为啥输出的带_占位,这里却不要
for (int i = 0; i < size; i++) {
nums[i] = Integer.valueOf(list1.get(i));
}
System.out.println(size);
}
}
栈
public static void main(String[] args) {
int[] pushed = new int[]{1, 2, 3, 4, 5};
int[] popped = new int[]{4, 5, 3, 2, 1};
System.out.println(validateStackSequences(pushed, popped));
}
private static boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>();
int i = 0;
for (int num : pushed) {
stack.push(num);
while (!stack.isEmpty() && stack.peek().equals(popped[i])) {
stack.pop();
i++;
}
}
return stack.isEmpty();
}