没有测试很多case,只是把idea表达了出来 感觉面试的时候现写,只能跪了 string afterPoint(int a, int b) { string ret; unordered_map m; if (a==0) { ret += "(0)"; return ret; }
int p=0; while(true) { m[a] = p; a *= 10; int d = a/b; int r = a%b; ret += char(d+'0'); if ( m.find( r)!= m.end() ) // find the cycle { ret.insert(m[r], "("); ret += ')'; return ret; } p++; a = r; } } string Solution(int a, int b) { string ret; int d = a/b; ret = to_string(d); ret += "."; ret += afterPoint(a%b, b); return ret; }
【在 w**********o 的大作中提到】 : This is cool: : public class Solution { : public int[] plusOne(int[] digits) { : // Start typing your Java solution below : // DO NOT write main() function : int carry=1; : for(int i=digits.length-1;i>=0;i--){ : if(carry==0) : break; : if(digits[i]==9){
【在 p*****2 的大作中提到】 : 练练第一道题 : def plusOne(num) : c=1 : for i in -1.downto(-num.length) : c+=num[i] : num[i]=c%10 : c/=10 : end : : if c>0
o*d
76 楼
还有个问题,似乎我看大家一般都用index来loop stl container,but normally, we are suggested to use iterator, right? do you guys think that will be a problem when we write the code onsite/at phone interview? tks
还有个问题,似乎我看大家一般都用index来loop stl container,but normally, we are suggested to use iterator, right? do you guys think that will be a problem when we write the code onsite/at phone interview? tks
第二题练了练 def divide(a,b) if b==0 return end print("#{a/b}.") a=(a%b)*10 arr=[] hm={} while hm[a]==nil hm[a]=arr.length if(a!=0 && a a*=10; arr.push(0) else arr.push(a/b) a=(a%b)*10 end end
for i in 0..hm[a]-1 print(arr[i]) end
print("(") for i in hm[a]..arr.length-1 print(arr[i]) end print(")\n") end