Skip to content

Commit 28fd2f2

Browse files
committed
leetcode
1 parent 3901d1d commit 28fd2f2

File tree

7 files changed

+16
-21
lines changed

7 files changed

+16
-21
lines changed

src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
6767
public static void main(String[] args) {
6868
ListNode a=new ListNode(5);
6969
ListNode b=new ListNode(5);
70+
a.next=b;
7071

71-
ListNode result = addTwoNumbers(a, b);
72+
73+
ListNode result = addTwoNumbers(a, a);
7274
for (ListNode node=result;node!=null;node=node.next){
7375
System.out.println(node.val);
7476
}

src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution11.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,8 @@ public static int removeElement1(int[] nums, int val) {
3333
}
3434
return i;
3535
}
36-
public static int removeElement2(int[] nums, int val) {
37-
//双指针-覆盖
38-
int n=0;
39-
for (int i = 0; i < nums.length; i++) {
40-
if (nums[n]!=val){
41-
nums[n]=nums[i];
42-
n++;
43-
}
44-
}
45-
return n;
46-
}
4736
public static void main(String[] args) {
4837
int[] nums=new int[]{3,2,2,1};
49-
System.out.println(removeElement2(nums,2));
38+
System.out.println(removeElement1(nums,3));
5039
}
5140
}

src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static int lengthOfLongestSubstring(String s) {
4141
for(int end=0;end<s.length();end++){
4242
Character currChar=s.charAt(end);
4343
if(map.containsKey(currChar)){
44-
//移动start
44+
//移动start+1
4545
start=Math.max(start,map.get(currChar)+1);
4646
}
4747
//计算出start和end之间长度

src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution3.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public static int[] topKFrequent(int[] nums, int k) {
5858
}
5959

6060
public static void main(String[] args) {
61-
topKFrequent(new int[]{1,1,3,3,3,3,5},2);
61+
int[] ints = topKFrequent(new int[]{1, 1, 3, 3, 3, 3, 5}, 2);
62+
for (int anInt : ints) {
63+
System.out.println(anInt);
64+
}
6265
}
6366
}

src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution4.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
public class Solution4 {
1414

15+
1516
public static String function(int a, int b){
1617
//存储商和余数
1718
List<Map<Integer,Integer>> temp=new ArrayList<>();
@@ -23,6 +24,7 @@ public static String function(int a, int b){
2324
remainder= a%b;
2425
for (int i=0;i<temp.size();i++){
2526
Map<Integer, Integer> integerIntegerMap = temp.get(i);
27+
//如果相除得到的整数答案和余数在之前出现过,那么就会开始循环。也就是循环节点
2628
if (integerIntegerMap.containsKey(value) && integerIntegerMap.containsValue(remainder)){
2729
flag=true;
2830
break;
@@ -45,7 +47,7 @@ public static String function(int a, int b){
4547
}
4648

4749
public static void main(String[] args) {
48-
System.out.println(function(1,7));
50+
System.out.println(function(3,7));
4951
}
5052

5153
}

src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution8.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static ListNode removeNthFromEnd(ListNode head, int n) {
3131
if (head.next==null){
3232
return null;
3333
}
34-
//增加一个头部节点,方便删除第一个节点
34+
//增加一个头部节点,方便删除倒数的节点刚好是第一个节点
3535
ListNode temp=new ListNode(-1);
3636
temp.next=head;
3737
ListNode p1=temp;

src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution9.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ public static String longestPalindrome(String s) {
4343
}
4444
String res="";
4545
for (int i=0;i<s.length()-1;i++){
46-
//判断aba汇文字符串
46+
//判断aba回文字符串 奇数回文
4747
String p1 = palindrome(s, i, i);
48-
//判断aa回文字符串
48+
//判断aa回文字符串 偶数回文
4949
String p2 = palindrome(s, i, i+1);
50-
System.out.println(p1);
5150
res=res.length()>=p1.length()?res:p1;
5251
res=res.length()>=p2.length()?res:p2;
5352
}
5453
return res;
5554
}
5655
public static void main(String[] args) {
57-
System.out.println(longestPalindrome("abc"));
56+
System.out.println(longestPalindrome("babad"));
5857
}
5958
}

0 commit comments

Comments
 (0)