Power button network programming that summarize (a)

analysis:

According meaning of the questions plus one, plus one that's right this is important because it is only a plus of it is possible only in two ways:

    In addition to the 999 number plus one;
    number 999.

Plus a decimal digit gains for the adder 000 does not appear as carry-on operation is over and carry only a.

Therefore there is no need to determine only carry analog and carry out its way, such as add the tens digit 111 is set to 000, and so on until it is determined no longer carry the loop exits return results.

Then there are some special cases that when the digital 999999,999999999 like appearance, the cycle needs to eventually carry, this happens it will need to manually enter a time.

import java.util.Arrays;
class Solution66 {
    public static void main(String[] args){
        int[] digits={9,8,7,6,5,4,3,2,1,0};
        System.out.println(Arrays.toString(plusOne(digits)));
    }
    public static int[] plusOne(int[] digits) {
        int carry=1;
        for(int i=digits.length-1;i>=0;i--){
            int num=digits[i]+carry;
            digits[i]=num%10;
            carry=num/10;
            if(carry==0){
                break;
            }
        }
        if(carry==1){   //只能说明数组当中肯定是全9
            int[] arr=new int[digits.length+1];
            arr[0]=1;
            return arr;
        }
        return digits;
   }
}

 

analysis:

1. The idea: we can exhaust all situations in the square of the level of time to detect each count is not the mode.

  Algorithms: algorithms violence traverse the entire array, and then count the number of times each number appears with another heavy cycle. The number of occurrences add up to more than the number of elements that appear other than the number returned.

2. The idea: if all numbers are monotonically increasing or monotonically decreasing sequence order row, then the mode index is ⌊n2⌋ \ lfloor \ dfrac {n} {2} \ rfloor⌊2n ⌋ (nnn is even when when, subscript ⌊n2⌋ + 1 \ lfloor \ dfrac {n} {2} \ rfloor + 1⌊2n ⌋ + 1)

   Algorithm: For this method, we first nums array is sorted, then return to the above mentioned figures. The following explains why this strategy is effective. FIG consider (The above example is a possible odd number, the following is one possible example of an even number):

In each case, the following line represents the array if the mode is the index of the array to cover the case where the minimum value. The upper line array is an array where the maximum value. Other cases, this line will be in the middle of these two extremes. However, we see that even these two extreme cases, they will be at an index ⌊n2⌋ \ lfloor \ dfrac {n} {2} \ rfloor⌊2n ⌋ where there is overlap. Thus, regardless of how the mode is returned lfloor \ dfrac index value corresponding ⌊n2⌋ \ {n} {2} \ rfloor⌊2n ⌋ are correct.

class Solution169 {
    public int majorityElement(int[] nums) {
        //不使用排序 在O(n)解决
        int m=nums[0];
        int count=1;
        for(int i=1;i<nums.length;i++){
            if(nums[i]==m){
                count++;
            }else{
                count--;
                if(count==0){
                    m=nums[i];
                    count=1;
                }
            }
        }
        return m;

        /*
        Arrays.sort(nums);  //O(n*logn)
        return nums[nums.length/2];
        */
        /*
        Arrays.sort(nums);
        int m=0;
        int mcount=0;
        for(int i=0;i<nums.length;){
            if(i>nums.length/2){
                break;
            }
            int count=1;
            for(int j=i+1;j<nums.length;j++){
                if(nums[j]==nums[i]){
                    count++;
                }else{
                    break;
                }
            }
            if(count>mcount){
                mcount=count;
                m=nums[i];
            }
            i+=count;
        }
        return m;
        */
    }
}

 

 analysis:

想法:  到现在为止,我们都保持子数组的左端点不动去找右端点。其实一旦知道这个位置开始的子数组不会是最优答案了,我们就可以移动左端点。我们用 2 个指针,一个指向数组开始的位置,一个指向数组最后的位置,并维护区间内的和 sum\text{sum}sum 大于等于 sss 同时数组长度最小。

 

class Solution209 {
    public int minSubArrayLen(int s, int[] nums) {
        int len=0;
        int i=0;
        int sum=0;
        for(int j=0;j<nums.length;j++){
            sum+=nums[j];
            while(sum>=s){
                len=len==0?(j-i+1):Math.min(len,j-i+1);
                sum-=nums[i];
                i++;
            }
        }
        return len;
    }
}

 

分析:

这里参考了快速排序的思想,快速排序首先要确定一个待分割的元素做中间点x,然后把所有小于等于x的元素放到x的左边,大于x的元素放到其右边。
这里我们可以用0当做这个中间点,把不等于0(注意题目没说不能有负数)的放到中间点的左边,等于0的放到其右边。
这的中间点就是0本身,所以实现起来比快速排序简单很多,我们使用两个指针i和j,只要nums[i]!=0,我们就交换nums[i]和nums[j]

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布了13 篇原创文章 · 获赞 0 · 访问量 382

Guess you like

Origin blog.csdn.net/weixin_45042315/article/details/104464746