680. Valid Palindrome II
![](https://i-blog.csdnimg.cn/blog_migrate/de8c043c0425a8212fc8b63bd5fc8008.png)
class Solution {
public boolean validPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
while(i < j) {
if(s.charAt(i) != s.charAt(j)) {
return(check(s, i, j - 1) || check(s, i + 1, j));
}
i++;
j--;
}
return true;
}
public boolean check(String s, int i, int j) {
while(i < j) {
if(s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
27. Remove Element
![](https://i-blog.csdnimg.cn/blog_migrate/862ff4295b756b2f0e579bca4aef6aa0.png)
class Solution {
public int removeElement(int[] nums, int val) {
int index = 0;
for(int num : nums) {
if(num != val) {
nums[index] = num;
index++;
}
}
return index;
}
}
26. Remove Duplicates from Sorted Array
![](https://i-blog.csdnimg.cn/blog_migrate/77f0ddde2b52bd3677fc7fb1df5476be.png)
class Solution {
public int removeDuplicates(int[] nums) {
int index = 1;
for(int i = 1; i < nums.length; i++) {
if(nums[i] != nums[i - 1]) {
nums[index] = nums[i];
index++;
}
}
return index;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
int index = 0;
for(int i = 1; i < nums.length; i++) {
if(nums[index] != nums[i]) {
index++;
nums[index] = nums[i];
}
}
return index +1;
}
}
80. Remove Duplicates from Sorted Array II
![](https://i-blog.csdnimg.cn/blog_migrate/25bab4443ba3dd4e5a128ea2343e5071.png)
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length <= 1) {
return nums.length;
}
int i = 0, j = 1, sum = 1;
while(j < nums.length) {
if(nums[i] == nums[j] && sum < 2) {
nums[++i] = nums[j];
sum++;
} else if(nums[i] != nums[j]) {
sum = 1;
nums[++i] = nums[j];
}
j++;
}
return ++i;
}
}
83. Remove Duplicates from Sorted List
![](https://i-blog.csdnimg.cn/blog_migrate/0ee92b679cbae63f84d55827aefb7dc9.png)
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null) {
return head;
}
ListNode p = head;
ListNode nex = head.next;
while(nex != null) {
if(p.val != nex.val) {
p.next = nex;
p = nex;
}
nex = nex.next;
}
p.next = null;
return head;
}
}
82. Remove Duplicates from Sorted List II
![](https://i-blog.csdnimg.cn/blog_migrate/b1b2676d17bbfaaa4157a456aa5aaf34.png)
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode fake = new ListNode();
fake.next = head;
ListNode first = fake;
while(first.next != null && first.next.next != null) {
if(first.next.val == first.next.next.val) {
int duplic = first.next.val;
while(first.next != null && first.next.val == duplic) {
first.next = first.next.next;
}
} else {
first = first.next;
}
}
return fake.next;
}
}
643. Maximum Average Subarray I![](https://i-blog.csdnimg.cn/blog_migrate/323a47beb8440ec3a9471ba4af679e5b.png)
class Solution {
public double findMaxAverage(int[] nums, int k) {
int sum = 0;
if(nums.length <= 1) {
return nums[0];
}
for(int i = 0; i < k; i++) {
sum += nums[i];
}
int maxSum = sum;
for(int i = k; i < nums.length; i++) {
sum += nums[i];
sum -= nums[i-k];
maxSum = maxSum > sum ? maxSum : sum;
}
return (double)maxSum / (double)k;
}
}
674. Longest Continuous Increasing Subsequence
![](https://i-blog.csdnimg.cn/blog_migrate/b78f0a04241dced1bdadfe37707ae177.png)
class Solution {
public int findLengthOfLCIS(int[] nums) {
int count = 1;
int max = 1;
for(int i = 1; i < nums.length; i++) {
if(nums[i] > nums[i - 1]) {
count++;
max = Math.max(max, count);
} else {
count = 1;
}
}
return max;
}
}
160. Intersection of Two Linked Lists
![](https://i-blog.csdnimg.cn/blog_migrate/8f3fa185332fe7ae6f51f3f27d8e74d8.png)
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA, b = headB;
while(a != b) {
a = a == null ? headB : a.next;
b = b == null ? headA : b.next;
}
return b;
}
}
88. Merge Sorted Array
![](https://i-blog.csdnimg.cn/blog_migrate/c5c463cb9e8e49e5d0825f8ccda8fe88.png)
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int index1 = m - 1, index2 = n - 1, index3 = m + n - 1;
while(index1 >= 0 && index2 >= 0) {
if(nums1[index1] > nums2[index2]) {
nums1[index3--] = nums1[index1--];
} else {
nums1[index3--] = nums2[index2--];
}
}
while(index2 >= 0) {
nums1[index3--] = nums2[index2--];
}
}
}
141. Linked List Cycle
![](https://i-blog.csdnimg.cn/blog_migrate/8d9cc5b6f06c4073f8e6a678a103c5e6.png)
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head, slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(slow == fast) {
return true;
}
}
return false;
}
}
142. Linked List Cycle II
![](https://i-blog.csdnimg.cn/blog_migrate/7e9b251d9b2523d70880a4a5cedb4150.png)
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(slow == fast) {
break;
}
}
if(fast == null || fast.next == null) {
return null;
}
fast = head;
while(fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
287. Find the Duplicate Number
![](https://i-blog.csdnimg.cn/blog_migrate/fdbe1dfeea3d76dc3ea476b3c00d2300.png)
class Solution {
public int findDuplicate(int[] nums) {
int slow = nums[0];
int fast = nums[nums[0]];
while(slow != fast) {
slow = nums[slow];
fast = nums[nums[fast]];
}
int entry = 0;
while(entry != slow) {
entry = nums[entry];
slow = nums[slow];
}
return entry;
}
}
234. Palindrome Linked List
![](https://i-blog.csdnimg.cn/blog_migrate/62e5df03b0a00c39de50f1e7d68739ee.png)
class Solution {
public boolean isPalindrome(ListNode head) {
if(head.next == null){
return true;
}
ListNode middleNode = getMiddleNode(head);
ListNode head1 = head;
ListNode head2 = middleNode.next;
middleNode.next = null;
head2 = reverseLinkedList(head2);
while(head1!=null && head2!=null){
if(head1.val != head2.val){
return false;
}
head1 = head1.next;
head2 = head2.next;
}
return true;
}
public ListNode getMiddleNode(ListNode node){
ListNode slow = node;
ListNode fast = node.next;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public ListNode reverseLinkedList(ListNode node){
if(node==null || node.next == null){
return node;
}
ListNode newHead = reverseLinkedList(node.next);
node.next.next = node;
node.next = null;
return newHead;
}
}
2540. Minimum Common Value
![](https://i-blog.csdnimg.cn/blog_migrate/cf2c3a38399f66a902655c966db06c14.png)
class Solution {
public int getCommon(int[] nums1, int[] nums2) {
int n1 = 0, n2 = 0;
int len1 = nums1.length, len2 = nums2.length;
while(n1 < len1 && n2 < len2) {
if(nums1[n1] == nums2[n2]) {
return nums1[n1];
} else if(nums1[n1] > nums2[n2]){
n2++;
} else {
n1++;
}
}
return -1;
}
}
11. Container With Most Water
![](https://i-blog.csdnimg.cn/blog_migrate/0e531d86c5b9b9fed8527ba9ecc96a4f.png)
class Solution {
public int maxArea(int[] height) {
int begin = 0, end = height.length - 1;
int res = 0;
while(begin < end) {
res = Math.max(res, (end - begin) * Math.min(height[begin], height[end]));
if(height[begin] < height[end]) {
begin++;
} else {
end--;
}
}
return res;
}
}