Stay button --- 2020.3.12

1071. GCD string

  • Euclidean algorithm: with a larger divided by the smaller number, then the remainder appears (the first residue) removing the divisor, then the remainder appears (the second residue) removing the first residue, and so forth, until the last until the remainder is zero. If you are seeking the greatest common divisor of two numbers, the last two numbers of the divisor is the greatest common divisor.
class Solution {
    public String gcdOfStrings(String str1, String str2) {
        // 假设str1是N个x,str2是M个x,那么str1+str2肯定是等于str2+str1的。
        if (!(str1 + str2).equals(str2 + str1)) {
            return "";
        }
        // 辗转相除法求gcd。  substring() 方法返回字符串的子字符串
        return str1.substring(0, gcd(str1.length(), str2.length()));
    }

    private int gcd(int a, int b) {
        return b == 0? a: gcd(b, a % b);
    }
}
class Solution {
    public String gcdOfStrings(String str1, String str2) {
        if (!(str1 + str2).equals(str2 + str1)) {
            return "";
        }
        return gcd(str1,str2);
    }

    //更相减损术
    public String gcd (String s1,String s2){
        if (s1.equals(s2)){
            return s1;
        }
        if (s1.length()>s2.length()){
            s1 = s1.substring(s2.length(), s1.length());
            return gcd(s1,s2);
        }else {
            s2 = s2.substring(s1.length(),s2.length());
            return gcd(s1,s2);
        }
    }
}

1281. integers difference between you and the product

class Solution {
    public int subtractProductAndSum(int n) {
        int res1 = 1;
        int res2 = 0;
        while(n>0){
            res1 *= n%10;
            res2 += n%10;
            n = n/10;
        }
        return res1-res2;
    }
}

1295. Statistical even number of digits to digital

class Solution {
    public int findNumbers(int[] nums) {
        int count=0;
        for(int i=0;i<nums.length;++i){
            if((nums[i]>=10&&nums[i]<100)||(nums[i]>=1000&&nums[i]<10000))
                count++;
        }
        return count;        
    }
}
class Solution {
    public int findNumbers(int[] nums) {
        int res=0;
        for(int i:nums){
            if(String.valueOf(i).length()%2==0){
                res++;
            }
        }
        return res;
    }
}
class Solution {
    public int findNumbers(int[] nums) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            int N = nums[i];
            //记录除10的次数
            int countTen = 0;
            while (N != 0) {
                N/=10;
                countTen++;
            }
            //如果除10的次数是偶数次,则该数为偶数位数
            if (countTen % 2 == 0) {
                count++;
            }
        }
        return count;
    }
}

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

He published 193 original articles · won praise 116 · views 10000 +

Guess you like

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