数组(链表)双指针(左右指针/快慢指针)

力扣26 快慢指针 去掉重复元素 数组

 public int removeDuplicates(int[] nums) {
        int slow = 0;
        int fast = 1;
        int count = 0;
        if (nums.length == 1) {
            return 1;
        } else {
            while (fast < nums.length) {
                if (nums[slow] == nums[fast]) {
                    // 若相等 则 fast向前走 slow不动
                    fast++;
                }
                else{
                    //  若不相等 则 slow后一位设置为fast位置的元素 fast向后走
                    slow++;
                    nums[slow] = nums[fast];
                    fast++;
                }
            }
        }
        return slow+1;
    }

力扣83 快慢指针向后遍历 去掉重复元素(链表)

 public ListNode deleteDuplicates(ListNode head) {
        if(head==null || head.next==null){
            return head;
        }
        else{
            // head 不为空 同时 head.next 不为空
            ListNode slow = head;
            ListNode fast = head.next;

            while(fast != null){
                if(slow.val == fast.val){
                    slow.next = fast.next;
                    fast = fast.next;
                }
                else{
                    slow = fast;
                    fast = fast.next;
                }
            }
            return head;
        }
    }

力扣 27 快慢指针向后遍历 去掉val元素

public int removeElement(int[] nums, int val) {
        int fast = 0, slow = 0;
        //如果 fast 遇到值为 val 的元素,则直接跳过,
        //否则就赋值给 slow 指针,并让 slow 前进一步。
        while (fast < nums.length) {
            if (nums[fast] != val) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        // 前面slow++ 所以返回slow
        return slow;
    }

力扣283 快慢指针向后遍历 除零

public void moveZeroes(int[] nums) {
        int length = nums.length;

        int slow = 0, fast = 0;
        while(fast < length){
            if(nums[fast] == 0){
                // fast == 0 则跳过 fast向前进
                fast++;
            }
            else{
                // fast不为0 则 将fast当前的值送到左侧,slow和fast都向前走一步
                nums[slow] = nums[fast];
                slow++;
                fast++;
            }
        }
        for(int i=slow;i<length;i++){
            // 剩余部分赋零
            nums[i] = 0;
        }
    }

力扣 344 左右向中间简单交换

 public void reverseString(char[] s) {

        for (int i = 0; i < s.length / 2; i++) {
            char temp = s[i];
            s[i] = s[s.length - 1 - i];
            s[s.length - 1 - i] = temp;
        }

    }

力扣1 HashMap

public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i=0;i<nums.length;i++){
            int need = target - nums[i];
            if(map.containsKey(need)){
                return new int[]{i,map.get(need)};
            }
            else{
                map.put(nums[i],i);
            }
        }
        return new int[]{};
    }

力扣5 最长回文子串 从中间向左右步进 找回文子串

public String longestPalindrome(String s) {
        String res = "";
        // res记录回文串中的最长子串
        for(int i=0;i<s.length();i++){
            // 找奇数个个数的回文子串
            String returnString1 = Palindrome(s,i,i);
            // 找偶数个个数的回文子串
            String returnString2 = Palindrome(s,i,i+1);

            res = res.length()>returnString1.length()?res:returnString1;
            res = res.length()>returnString2.length()?res:returnString2;
        }
        return res;
    }

    public String Palindrome(String s, int i, int j){
        // 以 i j 为中心点的最长回文子串
        // 奇数长度 i=j=length/2
        // 偶数长度 i=length/2-1  j=length/2

        while(i>=0 && j<s.length() && s.charAt(i)==s.charAt(j)){
            i--;j++;
        }
        // 退出循环时 最大回文子串 为 i+1 --- j-1
        // String.substring(beginIndex, endIndex)
        // 取beginIndex-(endIndex-1)的全部元素
        String returnString = s.substring(i+1,j);
        return returnString;
    }
posted @ 2024-04-20 23:21  在天边偷看小天使  阅读(10)  评论(0)    收藏  举报  来源