当前位置:网站首页>[Topic] add two numbers
[Topic] add two numbers
2022-07-28 21:06:00 【xiaoxigua_ cs】
Title Description
Here are two for you Non empty The linked list of , Represents two nonnegative integers . Each of them is based on The reverse Stored in , And each node can only store a Numbers .
Please add up the two numbers , And returns a linked list representing sum in the same form . You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
Input :l1 = [2,4,3], l2 = [5,6,4]
Output :[7,0,8]
explain :342 + 465 = 807.
Solution
Scheme 1
Insert a code chip here
Option two
There is accuracy problem , Not recommended at present
public class Test {
public static void main(String[] args) {
int[] a = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
int[] b = {
5, 6, 4};
System.out.println(addTwoNumbers(arrayToNode(a), arrayToNode(b)));
}
/** * Array to linked list method */
public static ListNode arrayToNode(int[] arr) {
ListNode head = new ListNode(arr[0]);
ListNode other = head;
for (int i = 1; i < arr.length; i++) {
ListNode temp = new ListNode(arr[i]);
other.next = temp;
other = other.next;
}// Printing nodes here can easily lead to head The change of
return head;
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ArrayList<Long> nums1 = getNumsByLinkedList(l1);
long n1 = getReversalNum(nums1);
ArrayList<Long> nums2 = getNumsByLinkedList(l2);
long n2 = getReversalNum(nums2);
long sum = n1 + n2;
String result = reverse(sum);
// Head node
ListNode headNode = new ListNode(Character.getNumericValue(result.charAt(0)));
// The cursor pointer points to the header node
ListNode cursor = headNode;
for (int i = 1; i < result.length(); i++) {
cursor.next = new ListNode(Character.getNumericValue(result.charAt(i)));
cursor = cursor.next;
}
cursor = headNode;
return cursor;
}
/** * Number reversal * * @param x The value of the input * @return Flipped value */
public static String reverse(long x) {
return new StringBuffer(String.valueOf(x)).reverse().toString();
}
/** * According to the linked list , Get the array in the linked list * * @param l Linked list * @return The array inside */
private static ArrayList<Long> getNumsByLinkedList(ListNode l) {
ArrayList<Long> nums = new ArrayList<>();
while (true) {
nums.add((long) l.val);
if (l.next == null) {
break;
}
l = l.next;
}
return nums;
}
/** * Get the inverted number according to the array * for example [1, 2, 3] -> 321 * * @param nums A collection of numbers */
private static long getReversalNum(ArrayList<Long> nums) {
// Input verification
long result = 0;
int length = nums.size();
for (int i = length - 1; i >= 0; i--) {
result += nums.get(i) * Math.pow(10, i);
}
return result;
}
}
Title source : Power button - Addition of two numbers
边栏推荐
- 58岁安徽人,干出瑞士今年最大IPO 投资界
- Unity foundation 5-optimization strategy
- C # basic 3-value type and reference type, packing and unpacking
- Random talk on GIS data (VI) - projection coordinate system
- BUUCTF做题Upload-Labs记录pass-01~pass-10
- Explain mesh Collider in unity
- [C language brush questions] explanation of linked list application
- Explain the imported 3D model in unity
- 九鑫智能正式加入openGauss社区
- Space shooting Lesson 13: explosion effect
猜你喜欢

Why on earth is it not recommended to use select *?

Unity3d tutorial notes - unity initial 02

程序员最大的浪漫~

C language final review questions

Deit: attention can also be distilled

【input 身份证号】星号 代替,input 切割成 多个 小格格(类似)

Ask if you don't understand, and quickly become an advanced player of container service!

MoCo V2:MoCo系列再升级

The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
Looking at SQL optimization from the whole process of one query
随机推荐
Space shooting Lesson 13: explosion effect
Lazada店铺如何产号高效补单?(测评自养号技术详解篇)
Unity - Fundamentals of 3D mathematics
Thinking and summary of R & D Efficiency
小程序容器技术,让移动研发效率提升500%
有奖征文 | 2022 云原生编程挑战赛征稿活动开启
C # basic 3-value type and reference type, packing and unpacking
Explain prefabrication in unity in detail
Mobilevit: challenge the end-to-side overlord of mobilenet
High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
C foundation 8-reflection and dependency injection
[server data recovery] HP StorageWorks series storage RAID5 two disk failure offline data recovery case
Space game Lesson 12: shield
Unity knowledge points summary (1)
What is "security"? Volvo tells you with its unique understanding and action
source insight 使用快捷键
Moco V2: further upgrade of Moco series
Unity3d tutorial notes - unity initial 02
Baklib|为什么说企业需要重视客户体验?
SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计