牛客網算法練習題目——(雙指針)

在這個特殊的假期裏,由於牛牛在家特別無聊,於是他發明了一個小遊戲,遊戲規則爲:將字符串數字中爲偶數位的數字進行翻轉,將翻轉後的結果進行輸出。

import java.util.*;


public class Solution {
    /**
     * 
     * @param number string字符串 
     * @return string字符串
     */
    public String change (String number) {
        if(number == null || number.length() == 0 || number.length() > Math.pow(10,7)){
            return number;
        }
        char[] temp = number.toCharArray();
        int left = 0;
        int right = temp.length - 1;

        while(left < right){
            if(temp[left]%2!=0){
                left++;
                continue;
            }
            if(temp[right]%2!=0){
                right--;
                continue;
            }
            
            char tempNum = temp[left];
            temp[left] = temp[right];
            temp[right] = tempNum;
            left++;
            right--;
        }
        return String.valueOf(temp);
    }
}

輸入一個遞增排序的數組和一個數字S,在數組中查找兩個數,使得他們的和正好是S,如果有多對數字的和等於S,輸出兩個數的乘積最小的。

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
        ArrayList<Integer> list = new ArrayList<>();
        //如果是最小的數據都是大於sum 那麼就不存在
        if (array.length==0||array[0] > sum) {
            return list;
        }
        int left = 0;
        int right = array.length - 1;
        
        while (left <= right) {
            //如果兩個數字的和等於的話
            if (array[left] + array[right] == sum) {
                if (list.isEmpty()) {
                    list.add(array[left]);
                    list.add(array[right]);
                } else {
                    if (list.get(0) * list.get(1) > array[left] * array[right]) {
                        list.clear();
                        list.add(array[left]);
                        list.add(array[right]);
                    }
                }
                left++;
                right--;
            } else if (array[left] + array[right] > sum) {
                right--;
            } else {
                left++;
            }
        }
        return list;
    }
}

題目描述

給定一個無序數組arr,找到數組中未出現的最小正整數

例如arr = [-1, 2, 3, 4]。返回1

       arr = [1, 2, 3, 4]。返回5

[要求]

時間複雜度爲O(n)O(n)O(n),空間複雜度爲O(1)O(1)O(1)

/**
 * Copyright (C), 2018-2020
 * FileName: Main001_double_pointer
 * Author:   xjl
 * Date:     2020/7/6 14:17
 * Description: 雙指針
 */
package Double_pointer;

import java.util.Arrays;
import java.util.Scanner;
//這個方法不符要求 只能通過20%的用例測試
public class Main001_double_pointer {

    public static void main(String[] args) {
        //數據的輸入
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int[] array = new int[m];
        for (int i = 0; i < m; i++) {
            array[i] = sc.nextInt();
        }
        //函數的調用
        int result = test(array);
        //結果的顯示
        System.out.println(result);
    }

    public static int test(int[] array) {
        int res=1;
        Arrays.stream(array);
        for (int i = 0; i < array.length; i++) {
            if (res!= array[i]) {
                return res;
            }
            res++;
        }
        return array[array.length - 1] + 1;
    }
}
import java.util.*;

public class Main{
   public static void main(String[] args){
        //數據的輸入
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int[] array = new int[m];
        for (int i = 0; i < m; i++) {
            array[i] = sc.nextInt();
        }
        //函數的調用
        int result = test(array);
        //結果的顯示
        System.out.println(result);
    }
    
     public static int test(int[] array) {
        ArrayList<Integer> list = new ArrayList();
        for (int i=0;i<array.length;i++){
            list.add(array[i]);
        }
        //檢查
        for (int i=1;i<=array.length;i++){
            if (!list.contains(i)){
                return i;
            }
        }
        return  list.get(list.size()) + 1;
    }
}

15. 三數之和

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList();
        int len = nums.length;
        if (nums == null || len < 3) return result;
        // 排序
        Arrays.sort(nums);
        //遍歷第一個數據
        for (int i = 0; i < len; i++) {
            // 如果當前數字大於0,則三數之和一定大於0,所以結束循環
            if (nums[i] > 0) break;
            // 去重
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            //制定兩個指針
            int L = i + 1;
            int R = len - 1;
            //如果是左邊的指針小於就表示可以進行
            while (L < R) {
                int sum = nums[i] + nums[L] + nums[R];
                if (sum == 0) {
                    //添加進入list中
                    result.add(Arrays.asList(nums[i], nums[L], nums[R]));
                    // 左邊去重
                    while (L < R && nums[L] == nums[L + 1]) L++;
                    // 右邊去重
                    while (L < R && nums[R] == nums[R - 1]) R--;
                    L++;
                    R--;
                } else if (sum < 0) L++;
                else if (sum > 0) R--;
            }
        }
        return result;
    }
}

18. 四數之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        //除重複
        Set<List<Integer>> set = new HashSet<>();
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(nums);//排序
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                int p = j + 1;
                int q = nums.length - 1;
                int currentTarget = target - nums[i] - nums[j];
                while (p < q) {
                    int sum = nums[p] + nums[q];
                    if (sum == currentTarget) {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[p]);
                        list.add(nums[q]);
                        set.add(list);
                        p++;
                        q--;
                    } else if (sum < currentTarget) {
                        p++;
                    } else {
                        q--;
                    }
                }
            }
        }
        for (List list : set) {
            result.add(list);
        }
        return result;
    }
}

923. 三數之和的多種可能

class Solution {
    public int threeSumMulti(int[] A, int target) {
         int MOD = 1_000_000_007;
        long ans = 0;
        Arrays.sort(A);
        for (int i = 0; i < A.length; ++i) {
            int T = target - A[i];
            int j = i + 1, k = A.length - 1;
            while (j < k) {
                if (A[j] + A[k] < T)
                    j++;
                else if (A[j] + A[k] > T)
                    k--;
                else if (A[j] != A[k]) {
                    int left = 1, right = 1;
                    while (j + 1 < k && A[j] == A[j + 1]) {
                        left++;
                        j++;
                    }
                    while (k - 1 > j && A[k] == A[k - 1]) {
                        right++;
                        k--;
                    }
                    ans += left * right;
                    ans %= MOD;
                    j++;
                    k--;
                } else {
                    ans += (k - j + 1) * (k - j) / 2;
                    ans %= MOD;
                    break;
                }
            }
        }
        return (int) ans;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章