Stay button --- 2020.3.24

Interview questions 17.16. Masseur

class Solution {
    public int massage(int[] nums) {
        if(nums.length==0) return 0;
        if(nums.length==1) return nums[0];
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0],nums[1]);
        for(int i=2;i<nums.length;i++){
            dp[i] = Math.max(dp[i-1],dp[i-2]+nums[i]);
        }
        return dp[nums.length-1];
    }
}
class Solution {
    public int massage(int[] nums) {
        int a = 0,b = 0;
        for(int i = 0;i < nums.length;i++){
            int c = Math.max(b,a+nums[i]);
            a = b;
            b = c;
        }
        return b;
    }
}

57. The two figures and to face questions of s

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for(int i=0;i<nums.length;i++){
            for(int j=nums.length-1;j>=0;j--){
                if(nums[i]+nums[j]==target){
                    return new int[]{nums[i],nums[j]};
                }
                if(nums[i]+nums[j]<target){
                    break;
                }
            }
        }
        return new int[]{};
    }
}
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int i = 0, j = nums.length - 1;
        while(i < j) {
            int s = nums[i] + nums[j];
            if(s < target) i++;
            else if(s > target) j--;
            else return new int[] { nums[i], nums[j] };
        }
        return new int[0];
    }
}
class Solution {
    public int[] twoSum(int[] nums, int target) {
        Set<Integer> set = new HashSet<>();
        for(int num : nums){
            if(!set.contains(target - num))
                set.add(num);
            else
                return new int[]{num,target - num};
        }
        return new int[]{};
    }
}

21. The order of the array face questions adjusted so that in front of the even-odd

class Solution {
    public int[] exchange(int[] nums) {
        int i = 0,j=nums.length-1;
        while(i<j){
            if(nums[i]%2==0&&nums[j]%2==1){
                int temp = nums[i];
                nums[i++] = nums[j];
                nums[j--] = temp;
            }else if(nums[i]%2==1&&nums[j]%2==0){
                i++;
                j--;
            }else if(nums[i]%2==1&&nums[j]%2==1){
                i++;
            }else if(nums[i]%2==0&&nums[j]%2==0){
                j--;
            }
        }
        return nums;
    }
}
// nums[i] & 1  <==>  num[i] % 2 
class Solution {
    public int[] exchange(int[] nums) {
        int i = 0, j = nums.length - 1, tmp;
        while(i < j) {
            while(i < j && (nums[i] & 1) == 1) i++;
            while(i < j && (nums[j] & 1) == 0) j--;
            tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
        return nums;
    }
}
class Solution {
    public int[] exchange(int[] nums) {
        int slow = 0,fast = 0;
        while(fast<nums.length){
            if((nums[fast]&1)==1) swap(nums,slow++,fast);
            fast++;
        }
        return nums;
    }

    public void swap(int[] nums,int a,int b){
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] =temp;
        return;
    }
}

The more you know, the more you do not know.
Proper way without surgery, patients can still seek, there is no way to surgery, ending surgery.
If you have other questions, welcome message, we can discuss, learn together and progress together

Published 205 original articles · won praise 139 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_40722827/article/details/105081826