NC359. 大数相减
描述
示例1
输入:
"100000000","1"
输出:
"99999999"
示例2
输入:
"100000000","1000000000"
输出:
"-900000000"
C++ 解法, 执行用时: 5ms, 内存消耗: 1456KB, 提交时间: 2022-07-28
static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: string substring(string num1, string num2) { int z=0; if(num1.size()<num2.size()||num1.size()==num2.size()&&num1<num2) { swap(num1,num2); z=1; } int l1=num1.size()-1; int l2=num2.size()-1; string r; int b=0; while(l1>=0||l2>=0) { int n1=l1>=0?num1[l1--]-'0':0; int n2=l2>=0?num2[l2--]-'0':0; int temp=n1-n2-b; b=0; if(temp<0) { b=1; temp+=10; } r+=temp+'0'; } reverse(r.begin(),r.end()); r.erase(0,r.find_first_not_of('0')); if(r.size()==0) return "0"; if(z==0) return r; else r='-'+r; return r; } };
C++ 解法, 执行用时: 6ms, 内存消耗: 1444KB, 提交时间: 2022-08-06
static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num1 string字符串 * @param num2 string字符串 * @return string字符串 */ string substring(string num1, string num2) { // write code here int z=0; if(num1.size()<num2.size()||num1.size()==num2.size()&&num1<num2) { swap(num1,num2); z=1; } int l1=num1.size()-1; int l2=num2.size()-1; string r; int b=0; while(l1>=0||l2>=0) { int n1=l1>=0?num1[l1--]-'0':0; int n2=l2>=0?num2[l2--]-'0':0; int temp=n1-n2-b; b=0; if(temp<0) { b=1; temp+=10; } r+=temp+'0'; } reverse(r.begin(),r.end()); r.erase(0,r.find_first_not_of('0')); if(r.size()==0) return "0"; if(z==0) return r; else r='-'+r; return r; } };
Go 解法, 执行用时: 7ms, 内存消耗: 2860KB, 提交时间: 2022-07-04
package main import "strings" func substring(num1 string, num2 string) string { f := cmp(num1, num2) if !f { num1, num2 = num2, num1 } H, h := len(num1)-1, len(num2)-1 w := 0 z := strings.Builder{} for H >= 0 || h >= 0 { u, v := 0, 0 if H >= 0 { u = int(num1[H] - '0') } if h >= 0 { v = int(num2[h] - '0') } if u-w >= v { z.WriteByte(byte(u - w - v + '0')) w = 0 } else { z.WriteByte(byte(u - w - v + 10 + '0')) w = 1 } H-- h-- } str := z.String() sh := len(str) - 1 for sh > 0 { if str[sh] != '0' { break } sh-- } str = str[:sh+1] str = reverse(str) if !f { str = "-" + str } return str } func cmp(a, b string) bool { if len(a) == len(b) { return a >= b } else if len(a) > len(b) { return true } return false } func reverse(str string) string { s := []byte(str) for i := 0; i <= (len(s)-1)/2; i++ { s[i], s[len(s)-1-i] = s[len(s)-1-i], s[i] } return string(s) }
C++ 解法, 执行用时: 8ms, 内存消耗: 1416KB, 提交时间: 2022-04-17
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num1 string字符串 * @param num2 string字符串 * @return string字符串 */ bool compare(string num1, string num2){ if(num1.length()<num2.length()) return false; else if (num1.length()>num2.length()) { return true; } else { return num1 > num2; } } string substring(string num1, string num2) { // write code here char sign = '+'; if(!compare(num1,num2)){ sign = '-'; swap(num1,num2); } int p1 = num1.length()-1; int q1 = num2.length()-1; string res; int borrow = 0; while (p1 >=0 || q1>=0) { int n1=p1>=0?(num1[p1--]-'0'):0; int n2=q1>=0?(num2[q1--]-'0'):0; int num = n1 - n2 - borrow; borrow = 0; if(num < 0){ borrow = 1; num += 10; } res+=num+'0'; } reverse(res.begin(),res.end()); res.erase(0,res.find_first_not_of('0')); if(res.length()==0) return "0"; if(sign=='+') return res; res = sign + res; return res; } };
Go 解法, 执行用时: 8ms, 内存消耗: 2516KB, 提交时间: 2022-03-29
package main import ( "strings" // "fmt" ) /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num1 string字符串 * @param num2 string字符串 * @return string字符串 */ func substring( num1 string , num2 string ) string { flag := cmp(num1, num2) if !flag { num1, num2 = num2, num1 } index1, index2 := len(num1) - 1, len(num2) - 1 borrow := 0 res := strings.Builder{} for index1 >= 0 || index2 >= 0 { upper, lower := 0, 0 if index1 >= 0 { upper = int(num1[index1] - '0')} if index2 >= 0 { lower = int(num2[index2] - '0')} if upper - borrow >= lower { res.WriteByte(byte(upper-borrow-lower + '0')) borrow = 0 } else { res.WriteByte(byte(upper-borrow-lower+10 + '0')) borrow = 1 } index1-- index2-- } rr := res.String() pos := len(rr)-1 for pos > 0 { if rr[pos] != '0' { break } pos-- } rr = rr[:pos+1] rr = reverse(rr) // fmt.Println(rr) if !flag { rr = "-" + rr } return rr } func cmp(a, b string) bool { if len(a) == len(b) { return a >= b } else if len(a) > len(b) { return true } return false } func reverse(rr string) string { r := []byte(rr) for i := 0; i <= (len(r) - 1)/2; i++ { r[i], r[len(r)-1-i] = r[len(r)-1-i], r[i] } return string(r) }