class Solution {
public int removeDuplicates(int[] nums) {
int slow = 0;
int length = nums.length;
for(int i = 1; i < nums.length; i++){
if(nums[i] == nums[slow])
length--;
else
nums[++slow] = nums[i];
}
return length;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length == 0)
return 0;
int slow = 0;
for(int i = 1; i < nums.length; i++)
if(nums[i] != nums[slow])
nums[++slow] = nums[i];
return slow + 1;
//return ++slow
}
}
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length == 0)
return 0;
int fast = 0;
int slow = 0;
while(fast < nums.length){
if(nums[fast] != nums[slow])
nums[++slow] = nums[fast];
fast++;
}
return slow + 1;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return null;
ListNode slow = head;
for(ListNode i = slow; i != null; i = i.next)
if(slow.val != i.val){
slow.next = i;
slow = slow.next;
}
slow.next = null;
return head;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null;
ListNode slow = head, fast = head;
while (fast != null) {
if (fast.val != slow.val) {
// nums[slow] = nums[fast];
slow.next = fast;
// slow++;
slow = slow.next;
}
// fast++
fast = fast.next;
}
// 断开与后面重复元素的连接
slow.next = null;
return head;
}
}
这里可能有读者会问,链表中那些重复的元素并没有被删掉,就让这些节点在链表上挂着,合适吗?
class Solution {
public int removeElement(int[] nums, int val) {
if(nums.length == 0)
return 0;
int temp = 0;
boolean flag = false;
for(int i = 0; i < nums.length; i++){
if(nums[i] == val){
if(flag == false){
temp = i;
flag = true;
}
else continue;
}
else
nums[temp++] = nums[i];
}
return temp;
}
}
class Solution {
public int removeElement(int[] nums, int val) {
if(nums.length == 0)
return 0;
int temp = -1;
for(int i = 0; i < nums.length; i++){
if(nums[i] == val && temp == -1)
temp = i;
else if(nums[i] != val)
nums[temp++] = nums[i];
}
return temp;
}
}
class Solution {
public int removeElement(int[] nums, int val) {
if(nums.length == 0)
return 0;
int slow = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != val)
nums[slow++] = nums[i];
}
return slow;
}
}
class Solution {
public int removeElement(int[] nums, int val) {
if(nums.length == 0)
return 0;
int slow = 0;
int fast = 0;
while(fast < nums.length){
if(nums[fast] != val)
nums[slow++] = nums[fast];
fast++;
}
return slow;
}
}
class Solution {
public void moveZeroes(int[] nums) {
if(nums.length == 0)
System.out.print(0);
int slow = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0)
nums[slow++] = nums[i];
}
for(int i = slow; i < nums.length; i++)
nums[i] = 0;
for(int i = 0; i < nums.length; i++){
if(i == nums.length - 1)
System.out.print(nums[i]);
else
System.out.print(nums[i] + ",");
}
}
}
class Solution {
public static void moveZeroes(int[] nums) {
if(nums.length == 0)
System.out.print(0);
int count = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0){
count++;
System.out.print(nums[i]);
if(count != nums.length)
System.out.print(",");
}
}
for(int i = count; i < nums.length; i++){
System.out.print(0);
if(i != nums.length - 1)
System.out.print(",");
}
}
}
结果:
看来leetcode的输出,是遍历了这个数组,氧化钙。
class Solution {
public static int removeElement(int[] nums, int val) {
if(nums.length == 0)
return 0;
int slow = 0;
int fast = 0;
while(fast < nums.length){
if(nums[fast] != val)
nums[slow++] = nums[fast];
fast++;
}
return slow;
}
public static void moveZeroes(int[] nums) {
// 去除 nums 中的所有 0,返回不含 0 的数组长度
int p = removeElement(nums, 0);
// 将 nums[p..] 的元素赋值为 0
for (; p < nums.length; p++) {
nums[p] = 0;
}
}
}
int binarySearch(int[] nums, int target) {
// 一左一右两个指针相向而行
int left = 0, right = nums.length - 1;
while(left <= right) {
int mid = (right + left) / 2;
if(nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid - 1;
}
return -1;
}
class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
for(int i = 0; i < numbers.length; i++){
for(int j = i + 1; j < numbers.length; j++){
if(numbers[i] + numbers[j] == target){
res[0] = i + 1;
res[1] = j + 1;
return res;
}
}
}
return res;
}
}
class Solution {
public int[] twoSum(int[] numbers, int target) {
int left = 0;
int right = numbers.length - 1;
while(left < right){
if(numbers[left] + numbers[right] == target)
return new int[]{left + 1, right + 1};
else if(numbers[left] + numbers[right] > target)
right--;
else
left++;
}
return new int[]{-1, -1};
}
}
class Solution {
public void reverseString(char[] s) {
int left = 0;
int right = s.length - 1;
char temp;
while(left < right){
temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}
public static boolean isPalindrome(String s){
int left = 0;
int right = s.length() - 1;
while(left < right){
if(s.charAt(left) != s.charAt(right))
return false;
left++;
right--;
}
return true;
}
class Solution {
public static boolean isPalindrome(String s){
int left = 0;
int right = s.length() - 1;
while(left < right){
if(s.charAt(left) != s.charAt(right))
return false;
left++;
right--;
}
return true;
}
public String longestPalindrome(String s) {
int len = s.length();
String str;
while(len > 0){
for(int i = 0; i < s.length(); i++){
if(i + len <= s.length()){
str = s.substring(i, i + len);
if(isPalindrome(str) == true)
return str;
}
}
len--;
}
return "-1";
}
}
for 0 <= i < len(s):
找到以 s[i] 为中心的回文串
找到以 s[i] 和 s[i+1] 为中心的回文串
更新答案
class Solution {
public static String Palindrome(String s, int left, int right){
while(left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){
// 双指针,向两边展开
left--;
right++;
}
return s.substring(left + 1, right);
}
public String longestPalindrome(String s) {
String res = "";
for(int i = 0; i < s.length(); i++){
//以 s[i] 为中心的最长回文子串
String str1 = Palindrome(s, i, i);
// 以 s[i] s[i+1] 为中心的最长回文子串
String str2 = Palindrome(s, i, i + 1);
res = res.length() < str1.length() ? str1 : res;
res = res.length() < str2.length() ? str2 : res;
}
return res;
}
}
参考:https://labuladong.github.io/algo/di-yi-zhan-da78c/shou-ba-sh-48c1d/shuang-zhi-fa4bd/