一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 1309|回复: 3
收起左侧

新鲜电面

[复制链接] |试试Instant~ |关注本帖
han275 发表于 2015-11-10 04:22:50 | 显示全部楼层 |阅读模式

2015(10-12月) 码农类 硕士 全职@Snapchat - 猎头 - 技术电面 |Other在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
刚刚面完第二轮电面,原题 BigInt, 不过把详细一点的放在这里,和我的答案。// This is the text editor interface.
// Anything you type or change here will be seen by the other person in real time.

/*
* class BigInt, to represent non-negative integers of arbitrary size
        * constructor accept a String, representing this non-neg int (e.g. "50"), assume valid input   
        * needs to be able to add to another BigInt, and return their sum as a new BigInt object
        * immutable.鐣欏璁哄潧-涓浜-涓夊垎鍦
            * new BigInt("20").add(new BigInt("30")) --> BigInt("50")



public class BigInt{
    String num;. From 1point 3acres bbs

    public BigInt(String num){
        this.num = num;
    }. from: 1point3acres.com/bbs

    public String getNum(){
        return this.num;. 1point3acres.com/bbs
    }
. more info on 1point3acres.com
    public BigInt add(BigInt num){. visit 1point3acres.com for more.
        String num1 = this.num;
        String num2 = num.num;

        int len1 = num1.length();
        int len2 = num2.length();

        StringBuilder builder1 = new StringBuilder(num1);
        StringBuilder builder2 = new StringBuilder(num2);

        int len = len1;

        if(len1 > len2){
            for(int i = 0; i< len1 - len2; i++){
                builder2.insert(0,"0");-google 1point3acres
            }
        }else if(len1 < len2){
            len = len2;
            for(int i = 0; i< len2- len1; i++){. from: 1point3acres.com/bbs
                builder1.insert(0,"0");
            }
        }

        builder1 = builder1.reverse();
        builder2 = builder2.reverse();
. more info on 1point3acres.com
        StringBuilder result = new StringBuilder(); 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
        int carry = 0;
        for(int i = 0; i < len; i++){
            int cur = (builder1.charAt(i) - '0') + (builder2.charAt(i) - '0') + carry;
            carry = cur / 10;
            cur = cur % 10;
            result.append(cur);
        }

        if(carry != 0){
            result.append(carry);
        }

        result = result.reverse();
        return new BigInt(result.toString());
. visit 1point3acres.com for more.
    }


    public static void main(String args[]){.鏈枃鍘熷垱鑷1point3acres璁哄潧
        BigInt num1 = new BigInt("123");
        BigInt num2 = new BigInt("59");

        System.out.println(num1.add(num2).getNum());


        BigInt num3 = new BigInt("7");. Waral 鍗氬鏈夋洿澶氭枃绔,
        BigInt num4 = new BigInt("2");

        System.out.println(num4.add(num3).getNum());. From 1point 3acres bbs


. visit 1point3acres.com for more.
    }. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
}

评分

1

查看全部评分

bentison90 发表于 2016-2-24 08:48:38 | 显示全部楼层
class BigInt{
public:
    string add(string& num1, string& num2) {
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());
        string res;. 鍥磋鎴戜滑@1point 3 acres
        int alen = num1.size();
        int blen = num2.size();. 鍥磋鎴戜滑@1point 3 acres
        int carry = 0;.鐣欏璁哄潧-涓浜-涓夊垎鍦
        for(int i = 0, j = 0; i < alen || j < blen; i++, j++) {
.鏈枃鍘熷垱鑷1point3acres璁哄潧            int val1 = i < alen ? num1[i] - '0' : 0;
            int val2 = j < blen ? num2[j] - '0' : 0;
            int sum = val2 + val1 + carry;
            res += char((sum % 10) + '0');
            carry = sum / 10;.鐣欏璁哄潧-涓浜-涓夊垎鍦
        }
        if(carry) res += "1";
        reverse(res.begin(), res.end());
        return res;. more info on 1point3acres.com
    }
  }
}
回复 支持 反对

使用道具 举报

pawprinter 发表于 2016-9-9 04:13:40 | 显示全部楼层
immutable的话,num成员变量至少是个private吧
回复 支持 反对

使用道具 举报

ericlee27 发表于 2016-10-4 07:28:31 | 显示全部楼层
不需要补齐0和reverse直接从后面走就行了吧
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-4 22:25

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表