日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[LeetCode] Plus One - 整数字符转换相加

發布時間:2024/5/28 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [LeetCode] Plus One - 整数字符转换相加 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目概述:
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.


題目解析:
給你一個int型數組存儲一個非負整數,對整數加1后輸出一個int型數組。注意幾點:
? ? ? ? 1.可能存在進位操作,增加一位,如999+1=1000;
? ? ? ? 2.數組存儲如234=[2, 3, 4],它進行加1操作時從數組的高位(4)到低位(2);
? ? ? ? 3.輸出時也需要轉置[0, 0, 0, 1]轉成1000;
? ? ? ? 4.C語言代碼*returnSize是一維數組,注意賦值否則提示“超時異常”。

我的代碼:
/*** Return an array of size *returnSize.* Note: The returned array must be malloced, assume caller calls free().* 899+1=900 存儲時digits[3]=[8,9,9] 從高位到地位 result[3]=[0,0,9]需要轉置 * digitsSize數組長度 *returnSize為返回數組長度*/ int* plusOne(int* digits, int digitsSize, int* returnSize) { //初始時加1操作 后為進位數字0或1int add=1; int i,j=0;int temp;//申請空間 初始化操作int *result=(int*)malloc(sizeof(int)*(digitsSize+1));memset(result, 0 , sizeof(int)*(digitsSize + 1));for(i=digitsSize-1; i>=0; i--) {result[j]=(digits[i]+add)%10; //個位數字add=(digits[i]+add)/10; //進位操作j++;}//最后如果add==1表示位數加1 如99+1=100if(add==1) {result[digitsSize]=1;*returnSize=digitsSize+1; //注意它是一維數組//輸出數組倒置for(i=0,j=digitsSize;i<j;i++,j--) {temp=result[i];result[i]=result[j];result[j]=temp;}}else {*returnSize=digitsSize;//輸出數組倒置for(i=0,j=digitsSize-1;i<j;i++,j--) {temp=result[i];result[i]=result[j];result[j]=temp;}}return result; }
推薦代碼:
C語言代碼 參考:http://www.tonzoc.info/?p=688

/*** Return an array of size *returnSize.* Note: The returned array must be malloced, assume caller calls free().*/ void reverse(int* digits, int start, int end) {int temp;for (int i = start; i <= (start + end) >> 1; ++i) {temp = digits[i];digits[i] = digits[end + start - i];digits[end + start - i] = temp;} }int* plusOne(int* digits, int digitsSize, int* returnSize) {int num = 1;int* result = (int*)malloc(sizeof(int) * (digitsSize + 1));memset(result, 0, sizeof(int) * (digitsSize + 1));for (int i = digitsSize - 1; i >= 0; --i) {result[digitsSize - 1 - i] = (digits[i] + num) % 10;num = (digits[i] + num) / 10;}if (num) {*returnSize = digitsSize + 1;result[digitsSize] = num;reverse(result, 0, digitsSize);} else {*returnSize = digitsSize;reverse(result, 0, digitsSize - 1);}return result; } C++代碼
vector<int> plusOne(vector<int> &digits) { int carry=1, sum=0; vector<int> result(digits.size(),0); for(int i=digits.size()-1;i>=0;i--){ sum = carry+digits[i]; carry = sum/10; result[i] = sum%10; } if(carry>0){ //進位 result.insert(result.begin(),carry); } return result; }
同類題目:
二進制字符串加法 https://leetcode.com/problems/add-binary/

PS:需要注意轉換方法 ((a[i]-'0')+(b[j]-'0')+add)%2+'0'當前結果和進位數字add=((a[i]-'0')+(b[j]-'0')+add)/2;同時需要注意字符串倒置的方法和對齊判斷即可。

(By:Eastmount 2015-9-9 凌晨2點 ??http://blog.csdn.net/eastmount/)


與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的[LeetCode] Plus One - 整数字符转换相加的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。