class Main {
	public static void main(String[] args) {
		System.out.println("Hello JSRUN!   \n\n         - from JAVA .");
	}

    public int majorityElement(int[] nums) {

        int count = 1;
        int res = nums[0];
        
        for(int i = 0; i < nums.length; i++){
            if(count == 0){
                res = nums[i];
            }else {
                if(res == nums[i]){
                    count ++;
                }else{
                    count --;
                }
            }
        }   

        return res;
    }

    public int majorityElement_1(int[] nums) {

        Stack<Integer> stack = new Stack<>();
        for(int i : nums){
            if(stack.isEmpty() || i == stack.peek()){
                stack.push(i);
            }else{
                stack.pop();
            }
        }
        
        return stack.peek();
    }
}
toryang (Toryang )- 2019-06-17 0 人
LeetCode 169. 求众数
class Main {
	public static void main(String[] args) {
		System.out.println("Hello JSRUN!   \n\n         - from JAVA .");
	}

    public int maxProduct(int[] nums) {
        int max = Integer.MIN_VALUE, imax = 0, imin = 0;

        for(int i = 0; i < nums.length; i++){
            if(nums[i] < 0){
                int temp = imax;
                imax = imin;
                imin = temp;
            }
            imax = Math.max(imax*nums[i],nums[i]);
            imin = Math.min(imin*nums[i],nums[i]);

            max = Math.max(max,imax);
        }
        return max;
    }

    public int maxProduct_1(int[] nums) {

        int[][] dp = new int[2][2];
        dp[0][0] = nums[0];
        dp[0][1] = nums[0];
        int max = nums[0];

        for(int i = 1; i < nums.length; i++){
            int x = i % 2;
            int y = (i - 1) % 2;

            dp[x][0] = Math.max(Math.max(nums[i] * dp[y][1], nums[i] * dp[y][0]), dp[y][0]);
            dp[x][1] = Math.min(Math.min(nums[i] * dp[y][1], nums[i] * dp[y][0]), dp[y][0]);

            max = Math.max(dp[x][0],max);
        }

        return max;
    }
    
}
toryang (Toryang )- 2019-06-17 0 人
LeetCode 152. 乘积最大子序列
class Main {
	public static void main(String[] args) {
		System.out.println("Hello JSRUN!   \n\n         - from JAVA .");
	}

    private int[] origin,  res;
    public Main(int[] nums) {
        this.origin = nums;
        this.res = new int[nums.length];
        for(int i = 0; i < nums.length; i++){
            res[i] = nums[i];
        }
    }

    public int[] reset() {
        for(int i = 0; i < origin.length; i++){
            res[i] = origin[i];
        }
        return res;
    }
    
    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        List<Integer> list = new ArrayList<>();
        for(int i : origin){
            list.add(i);
        }

        for(int i = 0; i < res.length; i++){
            int index = (int)(Math.random()*list.size());
            res[i] = list.remove(inde);
        }
        return res;
    }
}
toryang (Toryang )- 2019-06-18 0 人
LeetCode 384. 打乱数组
class Main {
	public static void main(String[] args) {
		System.out.println("Hello JSRUN!   \n\n         - from JAVA .");
	}

    public void moveZeroes(int[] nums) {
        int count = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] != 0 ){
                nums[count] = nums[i];
                count ++;
            }
        }

        for(int j = count; j < nums.length; j++){
            nums[j] = 0;
        }
    }
}
toryang (Toryang )- 2019-06-17 0 人
LeetCode 283. 移动零
class Main {
	public static void main(String[] args) {
		System.out.println("Hello JSRUN!   \n\n         - from JAVA .");
	}
}
toryang (Toryang )- 2019-06-15 0 人
LeetCode 217. 存在重复元素
class Main {
	public static void main(String[] args) {
		System.out.print("2  + 2 = " + getSum(2,2));
	}

    public int getSum(int a, int b) {
        int sum = a ^ b;
        int carry = (a & b) << 1;

        while ((sum & carry) > 0){
            int newCarry = sum & carry;
            sum ^= carry;
            carry = newCarry << 1;
        }
        return sum ^ carry;
    }
}
toryang (Toryang )- 2019-06-15 0 人
LeetCode 371. 两整数之和
没有了
1/2 下一页