leetcode-month2-week5

Add Binary

package ygy.test.week5;

/**
 * Created by guoyao on 2017/10/1.
 */
public class AddBinary {

    public static void main(String[] args) {
        System.out.println(addBinary("1","111"));

    }

    /**
     * Given two binary strings, return their sum (also a binary string).
     For example,
     a = "11"
     b = "1"
     Return "100".
     */
    public static String addBinary(String a, String b) {
        StringBuilder stringBuilder=new StringBuilder();
        int z = 0 ;
        int n=Math.max(a.length(), b.length());
        for (int i = 0 ; i< n;i ++ ) {
            int x = 0 ;
            if ((a.length() - 1 - i) > -1) {
                x=Integer.valueOf(a.substring(a.length() - 1 - i,a.length()- i));
            }
            int y= 0 ;
            if ((b.length() - 1 - i) > -1) {
                y =Integer.valueOf(b.substring(b.length() - 1 - i,b.length()- i));
            }
            if ((x + y + z) > 1) {
                stringBuilder.append((x + y +z - 2));
                z=1;
            } else {
                stringBuilder.append((x + y + z));
                z=0;
            }
        }
        if (z > 0) {
            stringBuilder.append(z);
        }
        return stringBuilder.reverse().toString();
    }

    //leetcode answer
    public String addBinary_2(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int i = a.length() - 1, j = b.length() -1, carry = 0;
        while (i >= 0 || j >= 0) {
            int sum = carry;
            if (j >= 0) sum += b.charAt(j--) - '0';
            if (i >= 0) sum += a.charAt(i--) - '0';
            sb.append(sum % 2);
            carry = sum / 2;
        }
        if (carry != 0) sb.append(carry);
        return sb.reverse().toString();
    }
}

Climbing Stairs

package ygy.test.week5;

/**
 * Created by guoyao on 2017/10/2.
 */
public class ClimbingStairs {


    public static void main(String[] args) throws InterruptedException {
        System.out.println(climbStairs(5));
    }


    /**
     * You are climbing a stair case. It takes n steps to reach to the top.
     Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
     Note: Given n will be a positive integer.
     */
    public static int climbStairs(int n) {
        // 最后一步可以采用1步,也可以采用两步
        //所以可能性是原始的f(n-1) 和 f(n-2)之和
        //所以总数为两者之和
        if(n == 0 || n == 1 || n == 2){return n;}
        int[] temp = new int[n];
        temp[0] = 1;
        temp[1] = 2;
        for(int i = 2; i < n; i++){
            temp[i] = temp[i-1] + temp[i-2];
        }
        return temp[n-1];
    }
}

Length of Last Word

package ygy.test.week5;

/**
 * Created by guoyao on 2017/10/1.
 */
public class LengthofLastWord {

    public static void main(String[] args) {

        System.out.println(lengthOfLastWord("hello "));
    }

    public static int lengthOfLastWord(String s) {
        return s.trim().length()-s.trim().lastIndexOf(" ")-1;
    }
}

Sqrt

package ygy.test.week5;

/**
 * Created by guoyao on 2017/10/2.
 */
public class Sqrt {

    public static void main(String[] args) {
        System.out.println(mySqrt(2));

    }

    /**
     * Implement int sqrt(int x).
     Compute and return the square root of x.
     */
    //leetcode答案
    public static int mySqrt(int x) {
        long r = x;
        while (r*r > x)
            r = (r + x/r) / 2;
        return (int) r;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值