目录
LeetCode 1- 100
1. 两数之和
: https://blog.csdn.net/love905661433/article/details/84779586
int* twoSum(int* nums, int numsSize, int target) {
int * b =(int*)malloc(2 * sizeof(int));
for(int i =0 ;i<numsSize ; i++){
for(int j = 1 ;j<numsSize ;j++){
if((i!=j) && (nums[i]+nums[j]==target)){
b[0]=i;b[1]=j;
return b;
}
}
}
return b;
}
测试用例:
1、
3,2,3 6 ,易错:应该先判断是否有,再添加元素
3,2,4 6 易错:应该判断这两个数的下标是否一样
class Solution {
public int[] twoSum(int[] nums, int target) {
int [] res = {0,1};
HashMap<Integer, Integer> map = new HashMap<Integer , Integer>();
int len = nums.length;
int t = 0;
for(int i = 0;i<len ;i++){
t = target-nums[i];
if(map.containsKey(t) && i != (map.get(t))){
res[0] = map.get(t);
res[1] = i;
return res;
}
map.put(nums[i],i);
}
return res;
}
}
分析:一定要考虑测试用例
注意:hashmap的用法。
数组的长度是:nums.length
判断hashmap中是否存在该元素:map.containsKey(key); map.containsValue(v);
想获得某个key对应的value:map.get(k);
hashmap的添加是用put,不是add
2. 两数相加
: https://blog.csdn.net/love905661433/article/details/84842140
分析思路:链表最容易错的地方就是:两个链表合并的时候,不能让t1=t2,而是让t1的前一个next = t2.
两数相加,注意进位,最后也有可能会进位
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode t1 = l1;
ListNode t2 = l2;
ListNode qian1 = l1;
int f = 0;
while(t1 != null && t2!= null){
t1.val =t1.val + t2.val + f ;
f = t1.val/10;
t1.val = t1.val % 10;
qian1 = t1;
t1 = t1.next;
t2 = t2.next;
}
if(t2 != null){
qian1.next = t2;
t1 = t2;
}
while(t1 != null){
t1.val = t1.val + f;
f = t1.val/10;
t1.val = t1.val % 10;
qian1 = t1;
t1 = t1.next;
}
if(f == 1){
qian1.next = new ListNode(1);
qian1.next.next = null;
}
return l1;
}
}
3. 无重复字符的最长子串
: https://blog.csdn.net/love905661433/article/details/84640527
自己的博客:https://blog.csdn.net/qq_39474604/article/details/90674749
双指针做法
class Solution {
public static int lengthOfLongestSubstring(String s) {
if(s.length() == 0) return 0;
if(s.length() == 1) return 1;
int len = 0;
int qian = 0;
int hou = 0;
int [] map = new int[257];
while(hou<s.length()){
map[s.charAt(hou)]++;
if(map[s.charAt(hou)] > 1){
//一定是前面有重复的了,需要让qian往后移动
while(map[s.charAt(qian)]==1){
map[s.charAt(qian)]--;
qian++;
}
map[s.charAt(qian)]--;
qian++;
hou++;
}else {
//没有重复,就让hou往后移动,然后len改变一下
hou++;
len = Math.max(len , hou-qian);
}
}
return len ;
}
}
思路 1:
把每个字母当成回文串的中心
这里要考虑两种情况,回文串的长度为奇数或者偶数情况。
思路 2: 把每个字母当成回文串的结束
思路 3: 动态规划
dp[j][i] 表示字符串从 j 到 i 是否是为回文串,即当 s[j] == s[i] 如果 dp[j+1][i-1] 也是回文串,那么字符串从 j 到 i 也是回文串,即 dp[j][i] 为真。
作者:powcai
链接:https://leetcode-cn.com/problems/two-sum/solution/duo-chong-si-lu-qiu-jie-by-powcai/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*******4.寻找两个有序数组的中位数(官方解答) :
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1 = nums1.length;
int len2 = nums2.length;
int [] all = new int[len1+len2];
//边界
if(len1 == 0 && len2 == 0) return 0.0;
int i1 = 0;
int i2 = 0;
int c = 0;
double t = 0;
while(i1 < len1 && i2<len2){
if(nums1[i1]<nums2[i2]){
all[c] = nums1[i1];
i1++;
c++;
}else if(nums1[i1]>nums2[i2]){
all[c] = nums2[i2];
i2++;
c++;
}else{
all[c] = nums1[i1];
i1++;
c++;
all[c] = nums2[i2];
i2++;
c++;
}
}
while(i1<len1){
all[c++] = nums1[i1++];
}
while(i2<len2){
all[c++] = nums2[i2++];
}
if((len1+len2)%2 == 0){
return (all[(len1+len2)/2-1]+all[(len1+len2)/2])/2.0;
}
return (double)all[(len1+len2)/2];
}
}
5.最长回文子串(官方解答)
: https://leetcode-cn.com/problems/longest-palindromic-substring/solution/
思路1:每个元素都遍历看是否:以该元素为中心的回文,O(n*2*n),而且还是以两种方式的:aba,bb
暴力求解,列举所有的子串,判断是否为回文串,保存最长的回文串。
class Solution {
public String longestPalindrome(String s) {
if(s.length() == 0) return s;
int res = 0;
int tou_max = 0;
int wei_max = 0;
int sum = 1;
int left = 0 ;
int right = 0;
for(int i = 0 ;i<s.length() ;i++){
//这个适合:aba,所以sum = 1
sum = 1;
left = i -1 ;
right = i+1;
while(left >= 0 && right <s.length()){
if(s.charAt(left) == s.charAt(right)){
sum +=2 ;//注意每次都是加2
left--;
right ++ ;
}
else break;
}
if(sum > res){
tou_max = left +1;
wei_max = right-1;
res = sum;
}
//这里适合:bb,所以sum=0
sum = 0;
left = i ;
right = i+1;
while(left >= 0 && right <s.length()){
if(s.charAt(left) == s.charAt(right)){
sum += 2;
left--;
right ++ ;
}
else break;
}
if(sum > res){
tou_max = left +1;
wei_max = right-1;
res = sum;
}
}
return s.substring(tou_max, wei_max+1);
//这个substring(2,4);取s的子字符串,从下标为2的元素开始取,到4-1 = 下标为3的元素为止
}
}
改进版本:
6.Z 字形变换(官方解答) : https://leetcode-cn.com/problems/zigzag-conversion/solution/
7.整数反转(官方解答) : https://leetcode-cn.com/problems/reverse-integer/solution/
8.字符串转换整数 (atoi) :
9.回文数(官方解答) : https://leetcode-cn.com/problems/palindrome-number/solution/
10.正则表达式匹配 :
11. 盛最多水的容器 : https://blog.csdn.net/love905661433/article/details/84137187
class Solution {
public int maxArea(int[] height) {
int max_height = 0;
//找到最高的柱子,也就是线最高的位置,线从下往上移动
for(int i = 0;i<height.length;i++){
if(height[i]>max_height) max_height = height[i];
}
//线从下往上移动
int output = 0;
for(int xian = 1; xian<=max_height ;xian++){
int output_i = 0;
//从前往后 找到第一个>=i的柱子,并标记位置
int qian_i = 0;
for(int i = 0 ;i<height.length;i++){
if(height[i] >= xian){ qian_i = i; break; }
}
//从后往前,找到第一个>=i的柱子,并标记位置
int hou_i = 0;
for(int i = height.length-1;i>=0;i--){
if(height[i]>= xian){ hou_i = i;break; }
}
//计算该水平线的情况下的水容量
output_i = (hou_i - qian_i)*xian;
if(output_i>output) output = output_i;
}
return output;
}
}
12.整数转罗马数字 :
13.罗马数字转整数 :
14.最长公共前缀 :
15. 三数之和
: https://blog.csdn.net/love905661433/article/details/84779700
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
//先排个序
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<List<Integer>>();
for(int i = 0 ;i<nums.length;i++){
int start = i+1;
int end = nums.length-1;
while(start < end){
//if( end == i ) continue;//首先三个数不能重复
int sum = nums[i]+nums[start]+nums[end];
if(sum == 0){
List<Integer> temp = new ArrayList();
temp.add(nums[i]);
temp.add(nums[start]);
temp.add(nums[end]);
list.add(temp);
int t = nums[start];
while(start < end && nums[start]==t) start++;
t= nums[end];
while(start < end && nums[end]==t) end--;
}else if(sum <0) start++;
else end--;
}
int t = nums[i];
while(i<nums.length && nums[i]==t) i++;
i--;
}
return list;
}
//[-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]
}
16. 最接近的三数之和
: https://blog.csdn.net/love905661433/article/details/84779915
- 先排序, 后计算
- 双指针, 滑动窗口
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int res = nums[0]+nums[1]+nums[2];
int sum = 0;
for(int i = 0;i<nums.length-2;i++){
int start = i+1;
int end = nums.length-1;
while(start<end){
sum = nums[i]+nums[start]+nums[end];
if(((sum-target)*(sum-target)) < ((res-target)*(res-target))){
res = sum;
}
if(sum < target) start++;
else if(sum > target) end--;
else return target;
}
}
return res;
}
}
17.电话号码的字母组合
: https://blog.csdn.net/love905661433/article/details/85063007
题目:
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。
解题
- 字典 + 递归 + 回溯
自己做的:
思路:从前往后遍历digits, 每一次的字符串的数组都是在上一个的改进下做的
class Solution {
private static char[][] num_char = new char[][]{
null,
null,
{'a','b','c'},
{'d','e','f'},
{'g','h','i'},
{'j','k','l'},
{'m','n','o'},
{'p','q','r','s'},
{'t','u','v'},
{'w','x','y','z'}
};
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<String>();
if(digits.length() == 0) return res;
int [] num = new int[digits.length()];
for(int i = 0;i<digits.length();i++){
num[i] = digits.charAt(i)-'0';
}
int hang = num[0];
for(int j = 0 ;j<num_char[hang].length;j++){
String str = ""+num_char[hang][j];
res.add(str);
}
res = func(num , 1, res );
return res;
}
public List<String> func(int[] num, int i , List<String> yuan){
if(i>num.length-1) return yuan;
List<String> res = new ArrayList<String>();
int hang = num[i];
Iterator<String> iter = yuan.iterator();
while (iter.hasNext()) {
String s = (String) iter.next();
for(int j = 0;j<num_char[hang].length;j++){
String st = s+num_char[hang][j];
res.add(st);
}
}
res = func(num , i+1 , res);
return res;
}
}
结果:
执行用时 :2 ms, 在所有 Java 提交中击败了74.12%的用户
内存消耗 :35.9 MB, 在所有 Java 提交中击败了73.90%的用户
18.四数之和
: https://blog.csdn.net/love905661433/article/details/84779785
超时:里面,为了防止重复发生,加了continue;
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int i = 0 ;i<nums.length;i++){
if(i>0 && nums[i]==nums[i-1]) continue;
for(int j = i+1;j<nums.length;j++){
if(j>i+1 && nums[j]==nums[j-1]) continue;
for(int k = j+1; k<nums.length ; k++){
if(k>j+1 && nums[k]==nums[k-1]) continue;
for(int r = k+1;r<nums.length ; r++){
if(r>k+1 && nums[r]==nums[r-1]) continue;
int sum = nums[i]+nums[j]+nums[k]+nums[r];
if(sum == target){
List<Integer> t = new ArrayList<Integer>();
t.add(nums[i]);
t.add(nums[j]);
t.add(nums[k]);
t.add(nums[r]);
res.add(t);
}
}
}
}
}
return res;
}
}
改进:
一开始自己这样写,一直出错,参考:https://blog.csdn.net/weixin_43111966/article/details/82495887
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int i = 0 ;i<=nums.length-3;i++){
if(i>0 && nums[i]==nums[i-1]) continue;
for(int j = i+1;j<=nums.length-2;j++){
if(j>i+1 && nums[j]==nums[j-1]) continue;
//固定两个数,然后双指针做法
//if((target - nums[i]-nums[j]) < nums[j+1]) break;//剪枝:这样的情况不存在解
//这里剪枝填上就出错。。。。。。。。。无语,咋会错呢
//另外:让target = target-nums[i]-nums[j],就出错。。。。。。
int qian = j+1 ;
int hou = nums.length - 1;
while(qian < hou){
int sum =nums[i]+nums[j]+ nums[qian]+nums[hou];
if(sum >target ) hou--;
else if(sum < target) qian++;
else{
//加入结果中
List<Integer> t = new ArrayList<Integer>();
t.add(nums[i]);
t.add(nums[j]);
t.add(nums[qian]);
t.add(nums[hou]);
res.add(t);
hou--;
qian++;
//去重
while(qian<hou && nums[hou]==nums[hou+1]) hou--;
while(qian<hou && nums[qian]==nums[qian-1]) qian++;
}
}
}
}
return res;
}
}
19.删除链表的倒数第N个节点(官方解答)
: https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/solution/
自己做的:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//找到链表的倒数第n+1个节点
int new_n = n+1;
//添加一个头结点,好计算
ListNode tou = new ListNode(0);
tou.next = head;
head = tou;
//寻找节点
ListNode fast = head;
ListNode slow = head;
for(int i = 0;i<new_n;i++) fast = fast.next;
while(fast != null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
head = head.next;
return head;
}
}
20.有效的括号(官方解答)
: https://leetcode-cn.com/problems/valid-parentheses/solution/
class Solution {
public boolean isValid(String s) {
char [] kuoHaoMen = s.toCharArray();
Stack<Character> st = new Stack<Character>();
int i = 0;
while(i<kuoHaoMen.length){
if(st.isEmpty()){
st.push(kuoHaoMen[i]);
}else{
char a = st.peek();
char b = kuoHaoMen[i];
if((a=='(' && b==')')||(a=='[' && b==']')||(a=='{'&&b=='}')){
st.pop();
}else{
st.push(kuoHaoMen[i]);
}
}
i++;
}
if(st.isEmpty()){
return true;
}
return false;
}
}
21. 合并两个有序链表
: https://blog.csdn.net/love905661433/article/details/84842342
22.括号生成(官方解答) : https://leetcode-cn.com/problems/generate-parentheses/solution/
自己做的:
执行用时 :3 ms, 在所有 Java 提交中击败了84.36%的用户
内存消耗 :38 MB, 在所有 Java 提交中击败了60.30%的用户
class Solution {
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<String>();
func(n-1,n,"(",res);
return res;
}
public void func(int qian , int hou , String str,List<String> res){
if(qian == 0){
//只有一种结果了,可以输出了
for(int i = 0;i<hou;i++) str+=")";
res.add(str);
return ;
}
if(qian == hou){
//只能是qian括号
func(qian-1,hou,str +"(",res);
}else{
//可以是qian括号
func(qian-1,hou,str +"(",res);
//可以是hou括号
func(qian,hou-1,str +")",res);
}
}
}
23.合并K个排序链表 :
24. 两两交换链表中的节点
: https://blog.csdn.net/love905661433/article/details/84842520
自己做的:
执行用时 :1 ms, 在所有 Java 提交中击败了82.92%的用户
内存消耗 :35.1 MB, 在所有 Java 提交中击败了66.82%的用户
题目描述:
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例:
给定 1->2->3->4, 你应该返回 2->1->4->3.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
//边界问题
if(head == null){
return head;
}
//加一个头结点,便于处理
ListNode tou = new ListNode(0);
tou.next = head;
head = tou;
ListNode qian = head.next;
ListNode hou = qian.next;
ListNode p = head;
while(qian != null && hou != null){
qian.next = hou.next;
hou.next = qian;
p.next = hou;
p = qian;
qian = p.next ;
if(qian != null) hou = qian.next;
}
head = head.next;
return head;
}
}
25. k个一组翻转链表
: https://blog.csdn.net/love905661433/article/details/84930637
下面这个做法:最后不足k,也会翻转
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null || head.next == null) return head;
int c = 1;
ListNode p_qian = head;
ListNode p = head.next;
ListNode next_p = p.next;
while(c<k){
p_qian.next = next_p;
p.next = head;
head = p;
c++;
p = next_p;
if(p == null) return head;
next_p = p.next;
}
if(c==k && p!= null) p_qian.next = reverseKGroup(p , k);
ListNode t = head;
return head;
}
}
正确的:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null || head.next == null) return head;
int c = 0;
ListNode p_qian = head;
ListNode p = head.next;
ListNode next_p = p.next;
//判断是否有k个数:开始
ListNode tt = head;
while(tt!= null && c<k){
c++;
tt = tt.next;
}
if(c<k) return head;
//判断是否有k个数:结束
c = 1;
while(c<k){
p_qian.next = next_p;
p.next = head;
head = p;
c++;
p = next_p;
if(p == null) return head;
next_p = p.next;
}
if(c==k && p!= null) p_qian.next = reverseKGroup(p , k);
ListNode t = head;
return head;
}
}
26. 删除排序数组中的重复项
: https://blog.csdn.net/love905661433/article/details/84640011
结果:时间faster than 98.17%,空间less than 99.97%
class Solution {
public int removeDuplicates(int[] nums) {
//注意本题不只是返回长度,而是真的要改变数组中的内容
int len = nums.length;
if(len == 0) return 0;
if(len == 1) return 1;
int last_i = 0;
int count = 1;
for(int i = 1; i<len ;i++){
if(nums[last_i] != nums[i]){
last_i = i;
nums[count] = nums[i];//这里真的改变数组的内容
count ++;
}
}
return count;
}
}
27. 移除元素 : https://blog.csdn.net/love905661433/article/details/83619231
- 对整个数组进行一次快速排序中的partition操作即可, 将数组分为等于val和不等于val的两部分
28.实现strStr() :
29.两数相除 :
30.与所有单词相关联的字串 :
31.下一个排列(官方解答) : https://leetcode-cn.com/problems/next-permutation/solution/
32.最长有效括号 :
33.搜索旋转排序数组 :
34.在排序数组中查找元素的第一个和最后一个位置 :
35. 搜索插入位置 : https://blog.csdn.net/love905661433/article/details/84061132
二分法
36.有效的数独 :
37.解数独 :
38.报数 :
39.组合总和 : https://blog.csdn.net/love905661433/article/details/85250243
1、递归 + 回溯;2、注意重复解问题
40.组合总和 II : https://blog.csdn.net/love905661433/article/details/85250391
1、先排序;2、递归 + 回溯;3、注意重复解问题
41.缺失的第一个正数 :
42.接雨水 :
43.字符串相乘 :
44.通配符匹配 :
45.跳跃游戏 II :
46.全排列 : https://blog.csdn.net/love905661433/article/details/85156115
47.全排列 II : https://blog.csdn.net/love905661433/article/details/85156116
48.旋转图像 :
49. 字母异位词分组
: https://blog.csdn.net/love905661433/article/details/84797348
自己的博客:https://blog.csdn.net/qq_39474604/article/details/90673556
public class Main {
public static void main(String[] args) {
String[] strs= {"eat", "tea", "tan", "ate", "nat", "bat"};
List<List<String>> res = groupAnagrams(strs);
for(List<String> ress: res){
for(String string : ress){
System.out.print(string+" ");
}
System.out.println(" ");
}
}
public static List<List<String>> groupAnagrams(String[] strs){
Map<String, List<String>> map = new HashMap<String, List<String>>();
for(String str :strs){
char [] chars = str.toCharArray();
Arrays.sort(chars);
String key = new String(chars);
if(map.containsKey(key)){
List<String> group = map.get(key);
group.add(str);
}else {
List<String> newGroup = new ArrayList<String>();
newGroup.add(str);
map.put(key,newGroup);
}
}
return new ArrayList<List<String>>(map.values());
}
}
50.Pow(x, n) :
51.N皇后 :
52.N皇后 II :
53. 最大子序和 : https://blog.csdn.net/love905661433/article/details/84797550
双指针做法
54.螺旋矩阵 :
55.跳跃游戏 :
56.合并区间 :
57.插入区间 :
58.最后一个单词的长度 :
59.螺旋矩阵 II :
60.第k个排列 :
61. 旋转链表 : https://blog.csdn.net/love905661433/article/details/84931256
62.不同路径
: https://blog.csdn.net/love905661433/article/details/86258220
public class Main {
public static void main(String[] args) {
int m = 7;
int n = 3;
int [][] res = new int[n][m];
res[0][0] = 1;
for(int i = 0 ; i<n;i++){
for(int j = 0;j<m;j++){
if(i==0 || j==0) res[i][j] = 1;
else {
res[i][j] = res[i-1][j]+res[i][j-1];
}
}
}
System.out.println(res[n-1][m-1]);
}
}
63.不同路径 II : https://blog.csdn.net/love905661433/article/details/86258419
64.最小路径和 : https://blog.csdn.net/love905661433/article/details/85857221
65.有效数字 :
66.加一 :
67.二进制求和 :
68.文本左右对齐 :
69.x 的平方根 :
70.爬楼梯
: https://blog.csdn.net/love905661433/article/details/85725640
class Solution {
public int climbStairs(int n) {
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 2;
int [] a = new int[n+1];
a[0] = 0;
a[1] = 1;
a[2] = 2;
for(int i = 3;i<=n;i++){
a[i] = a[i-1]+a[i-2];
}
return a[n];
}
}
71.简化路径 :
72.编辑距离 :
73.矩阵置零 :
74.搜索二维矩阵 :
75. 颜色分类
: https://blog.csdn.net/love905661433/article/details/84640274
分析:(1)三路快排;(2)用两次快排也行,第一次快排结果(<1)(=1 和>1),再对第二部分进行快排,分为(=1)(>1)
????????????(还没做出来)76. 最小覆盖子串 : https://blog.csdn.net/love905661433/article/details/84640651
方法一:错误的,对于T中不含有相同的字母是可以的
先放一个错误的,这种对于下面这样的
输入: S = "ADOBECODEBANC", T = "ABC"
输出: "BANC"
class Solution {
public String minWindow(String s, String t) {
//根据t做个hash<A,A出现的坐标>
HashMap<Character , Integer> hashmap = new HashMap<Character,Integer>();
for(int i = 0; i<t.length();i++){
hashmap.put(t.charAt(i),-1);//一直出错,是因为这里出错了,这里写错字母了
}
int count = 0;
int i = 0;
while(i<s.length() && count < t.length()){
if(hashmap.containsKey(s.charAt(i))){
if(hashmap.get(s.charAt(i))==-1) count ++;
hashmap.put(s.charAt(i),i);
}
i++;
}
if(count<t.length()) return "";
int [] res = func(hashmap);
while(i<s.length()){
if(hashmap.containsKey(s.charAt(i))){
hashmap.put(s.charAt(i),i);
int [] x = func(hashmap);
if(x[0]<res[0]){
res[0] = x[0];
res[1] = x[1];
res[2] = x[2];
}
}
i++;
}
return s.substring(res[1],res[2]+1);
}
public int[] func(HashMap<Character , Integer> map){
int [] res = new int[3];
res[1] = Integer.MAX_VALUE;
res[2] = Integer.MIN_VALUE;
Iterator mapt = map.entrySet().iterator();
while(mapt.hasNext()){
Map.Entry<Character , Integer> entry = (Map.Entry<Character , Integer>) mapt.next();
res[1] = Math.min(res[1],entry.getValue());
res[2] = Math.max(res[2],entry.getValue());
}
res[0] = res[2]-res[1];
return res;
}
}
但是通不过t中含有重复的字符
输入:"aa" "aa"
输出:“aa”
方法二:正确的
77.组合 : https://blog.csdn.net/love905661433/article/details/85250115
78.子集 : https://blog.csdn.net/love905661433/article/details/85298479
自己做的:递归+回溯
执行用时 :2 ms, 在所有 Java 提交中击败了89.48% 的用户
内存消耗 :37.1 MB, 在所有 Java 提交中击败了41.50%的用户
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> lists = new ArrayList<>();
//首先有一个空的
List<Integer> tlist = new ArrayList<Integer>();
lists.add(tlist);
func(lists,nums,tlist,0);
return lists;
}
public void func(List<List<Integer>> lists,int[] nums,List<Integer> tlist , int start){
if(start >= nums.length) return;
for(int i = start ; i<nums.length;i++){
tlist.add(nums[i]);
lists.add(new ArrayList<>(tlist)); //把子集复制一份加入结果集(必不可少的,不然出错)
//lists.add(tlist);
func(lists,nums,tlist,i+1);
tlist.remove(tlist.size()-1);
}
}
}
改进方法:
解法四 位操作
前方高能!!!!这个方法真的是太太太牛了。参考这里。
数组的每个元素,可以有两个状态,在子数组中和不在子数组中,所有状态的组合就是所有子数组了。
例如,nums = [ 1, 2 , 3 ]。1 代表在,0 代表不在。
1 2 3
0 0 0 -> [ ]
0 0 1 -> [ 3]
0 1 0 -> [ 2 ]
0 1 1 -> [ 2 3]
1 0 0 -> [1 ]
1 0 1 -> [1 3]
1 1 0 -> [1 2 ]
1 1 1 -> [1 2 3]
所以我们只需要遍历 0 0 0 到 1 1 1,也就是 0 到 7,然后判断每个比特位是否是 1,是 1 的话将对应数字加入即可。如果数组长度是 n,那么每个比特位是 2 个状态,所有总共就是 2 的 n 次方个子数组。遍历 00 ... 0 到 11 ... 1 即可。
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
int bit_nums = nums.length;
int ans_nums = 1 << bit_nums; //执行 2 的 n 次方
for (int i = 0; i < ans_nums; i++) {
List<Integer> tmp = new ArrayList<>();
int count = 0; //记录当前对应数组的哪一位
int i_copy = i; //用来移位
while (i_copy != 0) {
if ((i_copy & 1) == 1) { //判断当前位是否是 1
tmp.add(nums[count]);
}
count++;
i_copy = i_copy >> 1;//右移一位
}
ans.add(tmp);
}
return ans;
}
作者:windliang
链接:https://leetcode-cn.com/problems/subsets/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--10/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
79.单词搜索 : https://blog.csdn.net/love905661433/article/details/85298734
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.
执行用时 :10 ms, 在所有 Java 提交中击败了88.72% 的用户
内存消耗 :45 MB, 在所有 Java 提交中击败了75.00%的用户
方法:递归+回溯
class Solution {
public boolean exist(char[][] board, String word) {
//先找到起始点是word的第一个字母的,找到后,去递归
char[] chars = word.toCharArray();
int first_ch = chars[0];
int [][] flag = new int[board.length][board[0].length];
for(int i = 0;i<board.length;i++){
for(int j = 0;j<board[0].length;j++){
if(board[i][j] == chars[0]){
boolean isExist = func(board,chars,flag , i , j, 0);
if(isExist == true) return true;
}
}
}
return false;
}
public boolean func(char[][] board , char[] chars , int[][] flag , int start , int end, int chars_start){
if(chars_start == chars.length ){
return true;
}
//越界
if(start >= board.length || start < 0 || end < 0 || end >= board[0].length || chars_start >= chars.length) return false;
//该点一定是没有被访问过的
if(flag[start][end] == 1) return false;
if(chars[chars_start] != board[start][end]) return false;
//如果当前相等,就去下一个
flag[start][end] = 1;
//上下左右
boolean res = func(board , chars , flag , start-1 , end , chars_start+1) || func(board , chars , flag , start+1 , end , chars_start+1)||func(board , chars , flag , start , end-1 , chars_start+1)||func(board , chars , flag , start , end+1 , chars_start+1);
flag[start][end] = 0;//易错点:这一步必须要有,有可能第一个数不满足,就去找下一个满足的第一个数字了
return res;
}
}
80. 删除排序数组中的重复项 ii : https://blog.csdn.net/love905661433/article/details/84640177
81.搜索旋转排序数组 II :
82. 删除排序链表中的重复元素 ii : https://blog.csdn.net/love905661433/article/details/84842471
83.删除排序链表中的重复元素(官方解答) : https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/solution/
84.柱状图中最大的矩形 :
85.最大矩形 :
86. 分隔链表 : https://blog.csdn.net/love905661433/article/details/84842234
87.扰乱字符串 :
88. 合并两个有序数组 : https://blog.csdn.net/love905661433/article/details/84640342
89.格雷编码 :
90.子集 II : https://blog.csdn.net/love905661433/article/details/85298905
91.解码方法 : https://blog.csdn.net/love905661433/article/details/86257919
92. 反转链表 ii : https://blog.csdn.net/love905661433/article/details/84842047
93.复原IP地址 : https://blog.csdn.net/love905661433/article/details/85067983
94. 二叉树的中序遍历 : https://blog.csdn.net/love905661433/article/details/84952877
95.不同的二叉搜索树 II :
96.不同的二叉搜索树 :自己的博客:https://blog.csdn.net/qq_39474604/article/details/90671323
97.交错字符串 :
98.验证二叉搜索树 : https://blog.csdn.net/love905661433/article/details/85041087
99.恢复二叉搜索树 :
100. 相同的树 : https://blog.csdn.net/love905661433/article/details/84978845
LeetCode 101- 200
101.对称二叉树(官方解答) : https://leetcode-cn.com/problems/symmetric-tree/solution/
102. 二叉树的层次遍历 : https://blog.csdn.net/love905661433/article/details/84977934
103. 二叉树的锯齿形层次遍历 : https://blog.csdn.net/love905661433/article/details/84978019
104.二叉树的最大深度(官方解答) : https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/solution/
105.从前序与中序遍历序列构造二叉树 :
106.从中序与后序遍历序列构造二叉树 :
107. 二叉树的层次遍历 ii : https://blog.csdn.net/love905661433/article/details/84977974
108.将有序数组转换为二叉搜索树 : https://blog.csdn.net/love905661433/article/details/85055171
109.有序链表转换二叉搜索树 :
110.平衡二叉树 : https://blog.csdn.net/love905661433/article/details/85008795
111. 二叉树的最小深度 : https://blog.csdn.net/love905661433/article/details/84978491
112.路径总和 : https://blog.csdn.net/love905661433/article/details/85008820
113.路径总和 II : https://blog.csdn.net/love905661433/article/details/85018760
114.二叉树展开为链表 :
115.不同的子序列 :
116.填充同一层的兄弟节点 :
117.填充同一层的兄弟节点 II :
118. 杨辉三角 : https://blog.csdn.net/love905661433/article/details/83303637
119. 杨辉三角 ii : https://blog.csdn.net/love905661433/article/details/83810630
120.三角形最小路径和 : https://blog.csdn.net/love905661433/article/details/85726383
121. 买卖股票的最佳时机 : https://blog.csdn.net/love905661433/article/details/83621681
122. 买卖股票的最佳时机 ii : https://blog.csdn.net/love905661433/article/details/83620102
123.买卖股票的最佳时机 III :
124.二叉树中的最大路径和 :
125. 验证回文串 : https://blog.csdn.net/love905661433/article/details/84137215
126.单词接龙 II :
127.单词接龙 :
128.最长连续序列 :
129.求根到叶子节点数字之和 : https://blog.csdn.net/love905661433/article/details/85018783
130.被围绕的区域 :
131.分割回文串 : https://blog.csdn.net/love905661433/article/details/85107942
132.分割回文串 II :
133.克隆图 :
134.加油站 :
135.分发糖果 :
136.只出现一次的数字 :
137.只出现一次的数字 II :
138.复制带随机指针的链表 :
139.单词拆分 :
140.单词拆分 II :
141. 环形链表 : https://blog.csdn.net/love905661433/article/details/84797850
142.环形链表 II (自己的博客):https://blog.csdn.net/qq_39474604/article/details/90702954
143. 重排链表 : https://blog.csdn.net/love905661433/article/details/84960596
144. 二叉树的前序遍历 : https://blog.csdn.net/love905661433/article/details/84952877
145. 二叉树的后序遍历 : https://blog.csdn.net/love905661433/article/details/84952877
146.LRU缓存机制 :
147. 对链表进行插入排序 : https://blog.csdn.net/love905661433/article/details/84842607
148. 排序链表 : https://blog.csdn.net/love905661433/article/details/84930685
https://blog.csdn.net/qq_39474604/article/details/90698698(自己的博客)
149.直线上最多的点数 :
150. 逆波兰表达式求值 : https://blog.csdn.net/love905661433/article/details/84931345
151.翻转字符串里的单词 :
152.乘积最大子序列 :
153.寻找旋转排序数组中的最小值 :
154.寻找旋转排序数组中的最小值 II :
155.最小栈 :
160. 相交链表 : https://blog.csdn.net/love905661433/article/details/84797915
160.相交链表 :
162.寻找峰值 :
164.最大间距 :
165.比较版本号 :
166.分数到小数 :
167. 两数之和 ii - 输入有序数组 : https://blog.csdn.net/love905661433/article/details/83657039
168.Excel表列名称 :
169. 求众数 : https://blog.csdn.net/love905661433/article/details/83545138
171.Excel表列序号 :
172.阶乘后的零 :
173.二叉搜索树迭代器 :
174.地下城游戏 :
175.组合两个表(官方解答) : https://leetcode-cn.com/problems/combine-two-tables/solution/
176.第二高的薪水 :
177.第N高的薪水 :
178.分数排名 :
179.最大数 :
180.连续出现的数字 :
181.超过经理收入的员工 :
182.查找重复的电子邮箱(官方解答) : https://leetcode-cn.com/problems/duplicate-emails/solution/
183.从不订购的客户(官方解答) : https://leetcode-cn.com/problems/customers-who-never-order/solution/
184.部门工资最高的员工 :
185.部门工资前三高的员工 :
187.重复的DNA序列 :
188.买卖股票的最佳时机 IV :
189.旋转数组 :
190.颠倒二进制位 :
191.位1的个数 :
192.统计词频 :
193.有效电话号码 :
194.转置文件 :
195.第十行 :
196.删除重复的电子邮箱(官方解答) : https://leetcode-cn.com/problems/delete-duplicate-emails/solution/
197.上升的温度(官方解答) : https://leetcode-cn.com/problems/rising-temperature/solution/
198.打家劫舍
: https://blog.csdn.net/love905661433/article/details/86568972
class Solution {
public int rob(int[] nums) {
int length = nums.length;
if(length == 0) return 0;
if(length == 1) return nums[0];
if(length == 2) return Math.max(nums[0],nums[1]);
if(length == 3) return Math.max(nums[0]+nums[2],nums[1]);
nums[2] = nums[0]+nums[2];
for(int i = 3;i<length ;i++){
nums[i] = Math.max(Math.max(nums[i]+nums[i-3] , nums[i]+nums[i-2]) , nums[i-1]);
}
return Math.max(nums[length-2],nums[length-1]);
}
}
199. 二叉树的右视图 : https://blog.csdn.net/love905661433/article/details/84978063
200.岛屿的个数 : https://blog.csdn.net/love905661433/article/details/85299506
LeetCode 201- 300
201.数字范围按位与 :
202. 快乐数 : https://blog.csdn.net/love905661433/article/details/84779178
203. 移除链表元素 : https://blog.csdn.net/love905661433/article/details/84842176
204.计数质数 :
205. 同构字符串 : https://blog.csdn.net/love905661433/article/details/84779425
206. 反转链表 : https://blog.csdn.net/love905661433/article/details/84797989
207.课程表 :
208.实现 Trie (前缀树) :
209. 长度最小的子数组 : https://blog.csdn.net/love905661433/article/details/84640454
210.课程表 II :
211.添加与搜索单词 - 数据结构设计 :
212.单词搜索 II :
213.打家劫舍 II : https://blog.csdn.net/love905661433/article/details/86569046
214.最短回文串 :
215. 数组中的第k个最大元素 : https://blog.csdn.net/love905661433/article/details/84930799
216.组合总和 III : https://blog.csdn.net/love905661433/article/details/85250446
217. 存在重复元素 : https://blog.csdn.net/love905661433/article/details/84797502
218.天际线问题 :
219. 存在重复元素 ii : https://blog.csdn.net/love905661433/article/details/84797476
220.存在重复元素 III :
221.最大正方形 :
222.完全二叉树的节点个数 : https://blog.csdn.net/love905661433/article/details/85008777
223.矩形面积 :
224.基本计算器 :
225.用队列实现栈 :
226. 翻转二叉树 : https://blog.csdn.net/love905661433/article/details/84978615
227.基本计算器 II :
228.汇总区间 :
229.求众数 II :
230.二叉搜索树中第K小的元素 : https://blog.csdn.net/love905661433/article/details/85055226
231.2的幂 :
232.用栈实现队列 :
233.数字1的个数 :
234. 回文链表 : https://blog.csdn.net/love905661433/article/details/84797776
自己的博客:https://blog.csdn.net/qq_39474604/article/details/90696708
235.二叉搜索树的最近公共祖先 :
236.二叉树的最近公共祖先 : https://blog.csdn.net/love905661433/article/details/85062991
237. 删除链表中的节点 : https://blog.csdn.net/love905661433/article/details/84931057
238.除自身以外数组的乘积 :
239.滑动窗口最大值 :
240.搜索二维矩阵 II :
241.为运算表达式设计优先级 :
242. 有效的字母异位词 : https://blog.csdn.net/love905661433/article/details/84779104
257.二叉树的所有路径 : https://blog.csdn.net/love905661433/article/details/85008906
258.各位相加 :
260.只出现一次的数字 III :
262.行程和用户 :
263.丑数 :
264.丑数 II :
268. 缺失数字 : https://blog.csdn.net/love905661433/article/details/83620692
273.整数转换英文表示 :
274.H指数 :
275.H指数 II :
278.第一个错误的版本 :
279.完全平方数 : https://blog.csdn.net/qq_17550379/article/details/80875782
282.给表达式添加运算符 :
283. 移动零 : https://blog.csdn.net/love905661433/article/details/83578058
284.顶端迭代器 :
287.寻找重复数 :
289.生命游戏 :
290. 单词模式 : https://blog.csdn.net/love905661433/article/details/84779322
292.Nim游戏 :
295.数据流的中位数 :
297.二叉树的序列化与反序列化 :
299.猜数字游戏 :
300.最长上升子序列 :
LeetCode 301- 400
301.删除无效的括号 :
303.区域和检索 - 数组不可变 :
304.二维区域和检索 - 矩阵不可变 :
306.累加数 :
307.区域和检索 - 数组可修改 :
309.最佳买卖股票时机含冷冻期 :
310.最小高度树 :
312.戳气球 :
313.超级丑数 :
315.计算右侧小于当前元素的个数 : https://blog.csdn.net/jmspan/article/details/51219203
316.去除重复字母 :
318.最大单词长度乘积 :
319.灯泡开关 :
321.拼接最大数 :
322.零钱兑换 :
324.摆动排序 II :
326.3的幂 :
327.区间和的个数 :
328. 奇偶链表 : https://blog.csdn.net/love905661433/article/details/84842310
329.矩阵中的最长递增路径 :
330.按要求补齐数组 :
331.验证二叉树的前序序列化 :
332.重新安排行程 :
334.递增的三元子序列 :
335.路径交叉 :
336.回文对 :
337.打家劫舍 III : https://blog.csdn.net/love905661433/article/details/86569116
题目:节点不能挨着,找最大的和
思路:进入一个点,有两种情况,可以抢,不可以抢
如果可以抢,可以有两种选择:可以选择抢,可以选择不抢
如果这个节点不可以抢,那就只能选择不枪
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public static int res = 0;
public int rob(TreeNode root) {
res = Math.max(dfs(root , true),dfs(root , false));
return res;
}
public int dfs(TreeNode root , boolean qiang){
if(root == null) return 0;
if(qiang == true){
//如果要抢,可以抢,可以不抢
//抢
int qiang_res = root.val + dfs(root.left , false) + dfs(root.right , false);
int buqiang_res = dfs(root.left , true) + dfs(root.right , true);
res = Math.max(qiang_res , buqiang_res);
}else{
//如果不能抢,那就一定不能抢
res = dfs(root.left , true) + dfs(root.right , true);
}
return res;
}
}
分析:用了递归,所以很慢,
改进:尝试改成非递归,改成循环的
,想着用两个栈,一个栈存节点,一个栈存是否抢的信息,然后失败了,没能写下去
看别人的方法:
高效之处在于:少做了很多重复的操作
class Solution {
public int rob(TreeNode root) {
return dfs(root)[1];
}
private int[] dfs(TreeNode root) {
int[] rob ={0, 0};
if(root != null) {
int[] robLeft = dfs(root.left);
int[] robRight = dfs(root.right);
rob[0] = robLeft[1] + robRight[1];
rob[1] = Math.max(robLeft[0] + robRight[0] + root.val, rob[0]);
}
return rob;
}
}
338.比特位计数 :
341. 扁平化嵌套列表迭代器 : https://blog.csdn.net/love905661433/article/details/84960740
342.4的幂 :
343.整数拆分 : https://blog.csdn.net/liyuanbhu/article/details/51198124
344. 反转字符串 : https://blog.csdn.net/love905661433/article/details/84137251
345. 反转字符串中的元音字母 : https://blog.csdn.net/love905661433/article/details/84137305
347.前K个高频元素 : https://blog.csdn.net/love905661433/article/details/85008932
自己的博客:https://blog.csdn.net/qq_39474604/article/details/90672266
以前做的:用TreeMap<数字,数字出现的次数>,因为这种数据结构是有序的,但是默认是按照key来排序的,所以就重写,使其按照出现的次数排序
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
List<Integer> list = new ArrayList<Integer>();
Map<Integer,Integer> map = new TreeMap<Integer, Integer>();
for(int i = 0 ;i<nums.length;i++){
if(map.containsKey(nums[i])){
//如果map里面已经有了这个值,那么就加一
int count = map.get(nums[i]);
count ++;
map.put(nums[i],count);
}else {
//如果不存在就添加,并且把count 设置为1
map.put(nums[i],1);
}
}
//把treemap的元素放到一个list里面
List<Map.Entry<Integer,Integer>> t_list = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
//list里面的元素按照map的value排序
Collections.sort(t_list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
if(o1.getValue()>o2.getValue()) return -1;
if(o1.getValue()==o2.getValue()) return 0;
return 1;
}
});
int i = 0;
for(Map.Entry<Integer,Integer> e:t_list){
if(i<k) list.add(e.getKey());
else break;
i++;
//System.out.println(e.getKey()+":"+e.getValue());
}
return list;
}
}
这个题也可以用堆:
还是用hashmap把元素先遍历一遍,再插入到堆中,维护一个大小为k的堆
349. 两个数组的交集 : https://blog.csdn.net/love905661433/article/details/84640712
350. 两个数组的交集 ii : https://blog.csdn.net/love905661433/article/details/84640775
352.将数据流变为多个不相交间隔 :
354.俄罗斯套娃信封问题 :
355.设计推特 :
357.计算各个位数不同的数字个数 :
363.矩形区域不超过 K 的最大数值和 :
365.水壶问题 :
367.有效的完全平方数 :
368.最大整除子集 :
371.两整数之和 :
372.超级次方 :
373.查找和最小的K对数字 :
374.猜数字大小 :
375.猜数字大小 II :
376.摆动序列 :
377.组合总和 Ⅳ :
378.有序矩阵中第K小的元素 :
380.常数时间插入、删除和获取随机元素 :
381.O(1) 时间插入、删除和获取随机元素 - 允许重复 :
382.链表随机节点 :
383.赎金信 :
384.打乱数组 :
385.迷你语法分析器 :
386.字典序排数 :
387.字符串中的第一个唯一字符 :
388.文件的最长绝对路径 :
389.找不同 :
390.消除游戏 :
391.完美矩形 :
392.判断子序列 :
393.UTF-8 编码验证 :
394.字符串解码 :
395.至少有K个重复字符的最长子串 :
396.旋转函数 :
397.整数替换 :
398.随机数索引 :
399.除法求值 :
400.第N个数字 :
LeetCode 401- 500
401.二进制手表 : https://blog.csdn.net/love905661433/article/details/85299879
402.移掉K位数字 :
403.青蛙过河 :
404.左叶子之和 : https://blog.csdn.net/love905661433/article/details/85008846
405.数字转换为十六进制数 :
406.根据身高重建队列 :https://leetcode.com/problems/queue-reconstruction-by-height/
分析思路:
按照第一个数字,从小到大排列,如果第一个数字相同,第二个数字按照从小到大排列(自己写了排序算法)
如上:7最大,第一个数字是7的不变:
[4 4] [5 0 ] [5 2] [6 1] [7 0] [7 1]
[6 1]往后插入,即:
[4 4] [5 0 ] [5 2] [7 0] [6 1] [7 1]
这时候,操作[5 2],因为与5相同的前面还有一个,所以后面不必挪动两位,只需要挪动2-1 = 1位就可以了
有点插入排序的意思
class Solution {
public int[][] reconstructQueue(int[][] people) {
int renshu = people.length;//6
if(renshu == 0 || renshu == 1) return people;
for(int i=0;i<renshu-1;i++){
for(int j = 0;j<renshu-i-1;j++){
if(com(people,j,j+1)) swap(people , j ,j+1);
}
}
//for(int i = 0;i<renshu ;i++){
// System.out.print("["+people[i][0]+","+people[i][1]+"]");
//}
//System.out.println(" ");
//最大的数字,先不变
int i = renshu-1-1;
while(i>=0 && people[i][0]==people[renshu-1][0]) i--;
//System.out.println("1i:"+i);
while(i>=0){
//把当前i放到前面的地方
int sum = 0;//与当前i相等的有几个
for(int k = i;k>=0 && people[k][0]==people[i][0];k--) sum++;
//System.out.println("sum:"+sum);
int hou_high_dangqian = people[i][1]-(sum-1);
//System.out.println("hou_high_dangqian:"+hou_high_dangqian);
int t_0 = people[i][0];
int t_1 = people[i][1];
int j = 0;
for(j = 0;j<hou_high_dangqian ; j++){
people[i+j][0] = people[i+j+1][0];
people[i+j][1] = people[i+j+1][1];
//swap(people , i+j , i+j+1);
}
people[i+j][0] = t_0;
people[i+j][1] = t_1;
//for(int ii = 0;ii<renshu ;ii++){
// System.out.print("["+people[ii][0]+","+people[ii][1]+"]");
//}
i--;
}
return people;
}
public boolean com(int [][] people , int i , int j){
//i>j 返回true
if(people[i][0]>people[j][0]) return true;
if(people[i][0]<people[j][0]) return false;
if(people[i][1]>people[j][1]) return true;
return false;
}
public void swap(int[][] people , int i , int j){
int t = people[i][0];
people[i][0] = people[j][0];
people[j][0] = t;
t = people[i][1];
people[i][1] = people[j][1];
people[j][1] = t;
}
}
407.接雨水 II :
409.最长回文串 :
410.分割数组的最大值 :
412.Fizz Buzz :
413.等差数列划分 :
414.第三大的数 :
415.字符串相加 :
416.分割等和子集 :
417.太平洋大西洋水流问题 :
419.甲板上的战舰 :
420.强密码检验器 :
421.数组中两个数的最大异或值 :
423.从英文中重建数字 :
424.替换后的最长重复字符 :
427.建立四叉树 :
429.N叉树的层序遍历 :
430.扁平化多级双向链表 :
432.全 O(1) 的数据结构 :
433.最小基因变化 :
434.字符串中的单词数 :
435.无重叠区间 :
436.寻找右区间 :
437.路径总和 III : https://blog.csdn.net/love905661433/article/details/85018809
438. 找到字符串中所有字母异位词 : https://blog.csdn.net/love905661433/article/details/84640594
440.字典序的第K小数字 :
441.排列硬币 :
442.数组中重复的数据 :
443.压缩字符串 :
445. 两数相加 ii : https://blog.csdn.net/love905661433/article/details/84842369
446.等差数列划分 II - 子序列 :
447.回旋镖的数量 :
448. 找到所有数组中消失的数字 : https://blog.csdn.net/love905661433/article/details/83814993
449.序列化和反序列化二叉搜索树 :
450.删除二叉搜索树中的节点 : https://blog.csdn.net/love905661433/article/details/85055102
451. 根据字符出现频率排序 : https://blog.csdn.net/love905661433/article/details/84779547
452.用最少数量的箭引爆气球 :
453.最小移动次数使数组元素相等 :
454. 四数相加 ii : https://blog.csdn.net/love905661433/article/details/84779844
455.分发饼干 :
456.132模式 :
457.环形数组循环 :
458.可怜的小猪 :
459.重复的子字符串 :
460.LFU缓存 :
461.汉明距离 :
462.最少移动次数使数组元素相等 II :
463.岛屿的周长 :
464.我能赢吗 :
466.统计重复个数 :
467.环绕字符串中唯一的子字符串 :
468.验证IP地址 :
470.用 Rand7() 实现 Rand10() :
472.连接词 :
473.火柴拼正方形 :
474.一和零 :
475.供暖器 :
476.数字的补数 :
477.汉明距离总和 :
478.在圆内随机生成点 :
479.最大回文数乘积 :
480.滑动窗口中位数 :
481.神奇字符串 :
482.密钥格式化 :
483.最小好进制 :
485. 最大连续1的个数 : https://blog.csdn.net/love905661433/article/details/83619476
486.预测赢家 :
488.祖玛游戏 :
491.递增子序列 :
492.构造矩形 :
493.翻转对 :
494.目标和 :
495.提莫攻击 :
496.下一个更大元素 I :
497.非重叠矩形中的随机点 :
498.对角线遍历 :
500.键盘行 :
LeetCode 501- 600
501.二叉搜索树中的众数 :
502.IPO :
503.下一个更大元素 II :
504.七进制数 :
506.相对名次 :
507.完美数 :
508.出现次数最多的子树元素和 :
513.找树左下角的值 :
514.自由之路 :
515.在每个树行中找最大值 :
516.最长回文子序列 :
517.超级洗衣机 :
518.零钱兑换 II :
519.随机翻转矩阵 :
520.检测大写字母 :
521.最长特殊序列 Ⅰ :
522.最长特殊序列 II :
523.连续的子数组和 :
524.通过删除字母匹配到字典里最长单词 :
525.连续数组 :
526.优美的排列 :
528.按权重随机选择 :
529.扫雷游戏 :
530.二叉搜索树的最小绝对差 :
532.数组中的K-diff数对 :
535.TinyURL 的加密与解密 :
537.复数乘法(官方解答) : https://leetcode-cn.com/problems/complex-number-multiplication/solution/
538.把二叉搜索树转换为累加树 :
539.最小时间差 :
540.有序数组中的单一元素 :
541.反转字符串 II :
542.01 矩阵 :
543.二叉树的直径 :
546.移除盒子 :
547.朋友圈 :
551.学生出勤记录 I :
552.学生出勤记录 II :
553.最优除法 :
554.砖墙 :
556.下一个更大元素 III :
557.反转字符串中的单词 III :
558.四叉树交集 :
559.N叉树的最大深度 :
560.和为K的子数组 :
561. 数组拆分 i : https://blog.csdn.net/love905661433/article/details/83276956
563.二叉树的坡度 :
564.寻找最近的回文数 :
565.数组嵌套 :
566. 重塑矩阵 : https://blog.csdn.net/love905661433/article/details/83414915
567.字符串的排列 :
572.另一个树的子树 :
575.分糖果 :
576.出界的路径数 :
581.最短无序连续子数组 :
583.两个字符串的删除操作 :
587.安装栅栏 :
589.N叉树的前序遍历 :
590.N叉树的后序遍历 :
591.标签验证器 :
592.分数加减运算 :
593.有效的正方形 :
594.最长和谐子序列 :
595.大的国家 :
596.超过5名学生的课 :
598.范围求和 II :
599.两个列表的最小索引总和 :
600.不含连续1的非负整数 :
LeetCode 601- 700
601.体育馆的人流量 :
605.种花问题 :
606.根据二叉树创建字符串 :
609.在系统中查找重复文件 :
611.有效三角形的个数 :
617.合并二叉树 :https://leetcode.com/problems/merge-two-binary-trees/
思路:把第二个树的值加到第一个树上
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1 == null && t2 != null) return t2;
if(t1 != null && t2 == null) return t1;
if(t1 != null && t2 != null){
t1.val += t2.val;
if(t1.left == null){
t1.left = t2.left;
}else mergeTrees(t1.left , t2.left);
if(t1.right == null){
t1.right = t2.right;
}else mergeTrees(t1.right , t2.right);
}
return t1;
}
}
620.有趣的电影(官方解答) : https://leetcode-cn.com/problems/not-boring-movies/solution/
621.任务调度器 :
622.设计循环队列 :
623.在二叉树中增加一行 :
626.换座位 :
627.交换工资(官方解答) : https://leetcode-cn.com/problems/swap-salary/solution/
628.三个数的最大乘积 :
629.K个逆序对数组 :
630.课程表 III :
632.最小区间 :
633.平方数之和 :
636.函数的独占时间 :
637.二叉树的层平均值 :
638.大礼包 :
639.解码方法 2 :
640.求解方程 :
641.设计循环双端队列 :
643.子数组最大平均数 I :
645.错误的集合 :
646.最长数对链 :
647.回文子串 :https://leetcode.com/problems/palindromic-substrings/
注意:写成两个for循环不会出错,写成一个for循环就会出错
class Solution {
public int countSubstrings(String s) {
int len = s.length();
int res = len;
//分析:如果i到j是一个回文串,则f[i][j] = 1;
//如果f[i][j] = 1 && s[i-1]==s[j+1];
//右上三角,i<=j
for(int x = 0;x<len ;x++){
int i = x;
int j = x+1;
while(i>=0 && j<len){
if(s.charAt(i)==s.charAt(j)){
i--;
j++;
res++;
}else break;
}
i = x-1;
j = x+1;
while(i>=0 && j<len){
if(s.charAt(i)==s.charAt(j)){
i--;
j++;
res++;
}else break;
}
}
return res;
}
}
648.单词替换 :
649.Dota2 参议院 :
650.只有两个键的键盘 :
652.寻找重复的子树 :
653.两数之和 IV - 输入 BST :
654.最大二叉树 :
655.输出二叉树 :
657.机器人能否返回原点 :
658.找到 K 个最接近的元素 :
659.分割数组为连续子序列 :
661. 图片平滑器 : https://blog.csdn.net/love905661433/article/details/83655054
662.二叉树最大宽度 :
664.奇怪的打印机 :
665.非递减数列 :
667.优美的排列 II :
668.乘法表中第k小的数 :
669.修剪二叉搜索树 :
670.最大交换 :
671.二叉树中第二小的节点 :
672.灯泡开关 Ⅱ :
673.最长递增子序列的个数 :
674.最长连续递增序列 :
675.为高尔夫比赛砍树 :
676.实现一个魔法字典 :
677.键值映射 :
678.有效的括号字符串 :
679.24点游戏 :
680.验证回文字符串 Ⅱ :
682.棒球比赛(官方解答) : https://leetcode-cn.com/problems/baseball-game/solution/
684.冗余连接 :
685.冗余连接 II :
686.重复叠加字符串匹配 :
687.最长同值路径 :
688.“马”在棋盘上的概率 :
689.三个无重叠子数组的最大和 :
690.员工的重要性 :
691.贴纸拼词 :
692.前K个高频单词 :
693.交替位二进制数 :
695. 岛屿的最大面积 : https://blog.csdn.net/love905661433/article/details/83794258
696.计数二进制子串 :
697.数组的度 : https://blog.csdn.net/love905661433/article/details/83992603
698.划分为k个相等的子集 :
699.掉落的方块 :
700.二叉搜索树中的搜索 :
LeetCode 701- 800
701.二叉搜索树中的插入操作 :
703.数据流中的第K大元素 :
704.二分查找 :
705.设计哈希集合 :
706.设计哈希映射 :
707.设计链表 :
709.转换成小写字母 :
710.黑名单中的随机数 :
712.两个字符串的最小ASCII删除和 :
713.乘积小于K的子数组 :
714.买卖股票的最佳时机含手续费 :
715.Range 模块 :
717. 1比特与2比特字符 : https://blog.csdn.net/love905661433/article/details/83415650
718.最长重复子数组 :
719.找出第 k 小的距离对 :
720.词典中最长的单词 :
721.账户合并 :
722.删除注释 :
724.寻找数组的中心索引 :
725.分隔链表 :
726.原子的数量 :
728.自除数 :
729.我的日程安排表 I :
730.统计不同回文子字符串 :
731.我的日程安排表 II :
732.我的日程安排表 III :
733.图像渲染 :
735.行星碰撞 :
736.Lisp 语法解析 :
738.单调递增的数字 :
739.每日温度(自己的博客) :https://blog.csdn.net/qq_39474604/article/details/90602475
740.删除与获得点数 :
741.摘樱桃 :
743.网络延迟时间 :
744.寻找比目标字母大的最小字母 :
745.前缀和后缀搜索 :
746.使用最小花费爬楼梯 :
747.至少是其他数字两倍的最大数 :
748.最短完整词 :
749.隔离病毒 :
752.打开转盘锁 :
753.破解保险箱 :
754.到达终点数字 :
756.金字塔转换矩阵 :
757.设置交集大小至少为2 :
761.特殊的二进制序列 :
762.二进制表示中质数个计算置位 :
763.划分字母区间 :
764.最大加号标志 :
765.情侣牵手 :
766. 托普利茨矩阵 : https://blog.csdn.net/love905661433/article/details/83544633
767.重构字符串 :
768.最多能完成排序的块 II :
769.最多能完成排序的块 :
770.基本计算器 IV :
771.宝石与石头 :
773.滑动谜题 :
775.全局倒置与局部倒置 :
777.在LR字符串中交换相邻字符 :
778.水位上升的泳池中游泳 :
779.第K个语法符号 :
780.到达终点 :
781.森林中的兔子 :
782.变为棋盘 :
783.二叉搜索树结点最小距离 :
784.字母大小写全排列 :
785.判断二分图 :
786.第 K 个最小的素数分数 :
787.K 站中转内最便宜的航班 :
788.旋转数字 :
789.逃脱阻碍者 :
790.多米诺和托米诺平铺 :
791.自定义字符串排序 :
792.匹配子序列的单词数 :
793.阶乘函数后K个零 :
794.有效的井字游戏 :
795.区间子数组个数 :
796.旋转字符串 :
797.所有可能的路径 :
798.得分最高的最小轮调 :
799.香槟塔 :
LeetCode 801- 900
801.使序列递增的最小交换次数 :
802.找到最终的安全状态 :
803.打砖块 :
804.唯一摩尔斯密码词 :
805.数组的均值分割 :
806.写字符串需要的行数 :
807.保持城市天际线 :
808.分汤 :
809.情感丰富的文字 :
810.黑板异或游戏 :
811.子域名访问计数 :
812.最大三角形面积 :
813.最大平均值和的分组 :
814.二叉树剪枝 :
815.公交路线 :
816.模糊坐标 :
817.链表组件 :
818.赛车 :
819.最常见的单词 :
820.单词的压缩编码 :
821.字符的最短距离 :
822.翻转卡片游戏 :
823.带因子的二叉树 :
824.山羊拉丁文 :
825.适龄的朋友 :
826.安排工作以达到最大收益 :
827.最大人工岛 :
828.独特字符串 :
829.连续整数求和 :
830. 较大分组的位置 : https://blog.csdn.net/love905661433/article/details/84797613
831.隐藏个人信息 :
832.翻转图像 :
833.字符串中的查找与替换 :
834.树中距离之和 :
835.图像重叠 :
836.矩形重叠 :
837.新21点 :
838.推多米诺 :
839.相似字符串组 :
840.矩阵中的幻方 :
841.钥匙和房间 :
842.将数组拆分成斐波那契序列 :
843.猜猜这个单词 :
844.比较含退格的字符串 :
845.数组中的最长山脉 :
846.一手顺子 :
847.访问所有节点的最短路径 :
848.字母移位 :
849.到最近的人的最大距离 :
850.矩形面积 II :
851.喧闹和富有(官方解答) : https://leetcode-cn.com/problems/loud-and-rich/solution/
852.山脉数组的峰顶索引 :
853.车队 :
854.相似度为 K 的字符串 :
855.考场就座 :
856.括号的分数 :
857.雇佣 K 名工人的最低成本 :
858.镜面反射 :
859.亲密字符串(官方解答) : https://leetcode-cn.com/problems/buddy-strings/solution/
860.柠檬水找零(官方解答) : https://leetcode-cn.com/problems/lemonade-change/solution/
861.翻转矩阵后的得分 :
862.和至少为 K 的最短子数组 :
863.二叉树中所有距离为 K 的结点 :
864.获取所有钥匙的最短路径 :
865.具有所有最深结点的最小子树 :
866.回文素数 :
867. 转置矩阵 : https://blog.csdn.net/love905661433/article/details/83302961
868.二进制间距(官方解答) : https://leetcode-cn.com/problems/binary-gap/solution/
869.重新排序得到 2 的幂 :
870.优势洗牌(官方解答) : https://leetcode-cn.com/problems/advantage-shuffle/solution/
871.最低加油次数 :
872.叶子相似的树(官方解答) : https://leetcode-cn.com/problems/leaf-similar-trees/solution/
873.最长的斐波那契子序列的长度(官方解答) : https://leetcode-cn.com/problems/length-of-longest-fibonacci-subsequence/solution/
874.模拟行走机器人(官方解答) : https://leetcode-cn.com/problems/walking-robot-simulation/solution/
875.爱吃香蕉的珂珂(官方解答) : https://leetcode-cn.com/problems/koko-eating-bananas/solution/
876.链表的中间结点(官方解答) : https://leetcode-cn.com/problems/middle-of-the-linked-list/solution/
877.石子游戏(官方解答) : https://leetcode-cn.com/problems/stone-game/solution/
878.第 N 个神奇数字 :
879.盈利计划(官方解答) : https://leetcode-cn.com/problems/profitable-schemes/solution/
880.索引处的解码字符串(官方解答) : https://leetcode-cn.com/problems/decoded-string-at-index/solution/
881.救生艇(官方解答) : https://leetcode-cn.com/problems/boats-to-save-people/solution/
882.细分图中的可到达结点(官方解答) : https://leetcode-cn.com/problems/reachable-nodes-in-subdivided-graph/solution/
883.三维形体投影面积(官方解答) : https://leetcode-cn.com/problems/projection-area-of-3d-shapes/solution/
884.两句话中的不常见单词(官方解答) : https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/
885.螺旋矩阵 III(官方解答) : https://leetcode-cn.com/problems/spiral-matrix-iii/solution/
886.可能的二分法(官方解答) : https://leetcode-cn.com/problems/possible-bipartition/solution/
888.公平的糖果交换(官方解答) : https://leetcode-cn.com/problems/fair-candy-swap/solution/
889.根据前序和后序遍历构造二叉树(官方解答) : https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-
891.子序列宽度之和(官方解答) : https://leetcode-cn.com/problems/sum-of-subsequence-widths/solution/
892.三维形体的表面积(官方解答) : https://leetcode-cn.com/problems/surface-area-of-3d-shapes/solution/
893.特殊等价字符串组(官方解答) : https://leetcode-cn.com/problems/groups-of-special-equivalent-strings/solution/
894.所有可能的满二叉树(官方解答) : https://leetcode-cn.com/problems/all-possible-full-binary-trees/solution/
895.最大频率栈(官方解答) : https://leetcode-cn.com/problems/maximum-frequency-stack/solution/
896. 单调数列 : https://blog.csdn.net/love905661433/article/details/83624974
905. 按奇偶排序数组 : https://blog.csdn.net/love905661433/article/details/83271962