Cracking The Coding Interview2.4

2023-11-30 19:39

本文主要是介绍Cracking The Coding Interview2.4,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

删除前面的linklist,使用node来表示链表
//	You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
//
//	EXAMPLE
//
//Input: (3 -> 1 -> 5), (5 -> 9 -> 2)
//
//Output: 8 -> 0 -> 8#include <iostream>
using namespace std;
struct node
{int data;node *next;
};void init(node *p,int a[], int size)
{if (a==NULL || size<0){return;}for (int i = 0; i < size; i++){node *t = new node;t->data = a[i];p->next = t;p = p->next;}p->next = NULL;
}void print(node *head)
{if (!head){return;}node *p = head->next;while (p){cout<<p->data<<"  ";p = p->next;}cout<<endl;
}node * plus(node *a, node *b)
{node *pa = a->next;node *pb = b->next;node *pc = new node;node *pi = pc;int i = 0;int step = 0;while(pa!=NULL && pb!= NULL){node *t = new node;int temp = pa->data + pb->data +step;step = 0;if (temp >=10){temp %= 10;step = 1;}t->data =temp;pi->next = t;pi = t;pa = pa->next;pb = pb->next;}while(pa!= NULL){node *t = new node;t->data = pa->data + step;step = 0;pi->next = t;pi = t;pa = pa->next;	}while(pb != NULL){node *t = new node;t->data = pb->data + step;step = 0;pi->next = t;pi = t;pb = pa->next;}if (step>0){node *t = new node;t->data = step;pi->next = t;pi = t;}pi->next = NULL;return pc;}int main()
{int a[6] = {1,4,5,9,7,8};node *head = new node;init(head,a,6);print(head);int b[6] = {1,4,5,9,7,8};node *h = new node;init(h,a,6);print(h);node * r = plus(head, h);print(r);return 0;
}


这篇关于Cracking The Coding Interview2.4的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/438292

相关文章

【算法:二分查找】java算法二分查找,如何通过二分查找找到重复元素的第一个,coding

二分查找算法,是基于有序的结果集进行查询的 二分查找的时间复杂度是O(logN) 写一段二分查询的代码: public static void main(String[] args) {int[] data = new int[]{1, 2, 3, 3, 5, 5, 6, 8, 9, 9, 10};int queryData = 5;int index = queryDataIndex(qu

Cracking the Safe

原题链接:https://leetcode.cn/problems/cracking-the-safe/description/ 题目要求的是,某个时刻能够打开保险箱的任一最短密码序列,需要包含所有密码子串。 答案应当是一个字符串,任意长度为n的子串的都是一种密码方案。 对于有n位,每位k种方案的密码串,共有k^n个。 题目要求最短,那么任意位置选出的子串应当是不重复的。 也就是说,一个

《WEB开发-HEXO博客搭建》第4章 同步到Coding

笔者博客地址 1.注册Coding.net账号 Coding官网:https://coding.net/ 【注意】如果不想花钱的话要绑定腾讯云可以免费升级,笔者使用的是绑定腾讯云升级的。 图1 2.新建项目 注意项目名与注册用的账户名一致,这里我用的是ouxiaolong。 图2 图3 3.添加公钥 上面设置完毕之后点击创建项目,然后点击设置->部署公钥->新建

代码规范工具大比拼---Alibaba Java Coding Guidelines

代码规范工具大比拼---Alibaba Java Coding Guidelines   一,序言        对于代码规范的工具,市场上有很多很多, 我们常常说:”工欲善其事必先利其器”, 一个非常强大的代码检查工具, 能让很多代码实践者减少很多多不必要的小错误,尤其是对于一个团队来说,能较好的统一代码规则.   二,详情    1,Sonar

【Git】更新拉取Coding子仓库代码 及 过程中用户名密码输什么 git submodule

背景: 克隆拉取完主仓库,没有初始化子仓库 主仓库目录下有.gitmodules文件,存储了子仓库路径和url [submodule "aa"]path = aaurl = git@e.coding.net:aa.git[submodule "bb"]path = bburl = git@e.coding.net:bb.git 在主仓库目录下输入以下命令拉取更新子仓库代码 git s

sublime text3 安装插件,以及Zen Coding 写法简单了解

参考的文章 1、 http://blog.csdn.net/admin_yi/article/details/53608965 2、 http://www.cnblogs.com/tinyphp/p/3217457.html 安装说明 3、 http://www.cnblogs.com/Rising/p/3741116.html 需要安装的插件的说明 4、 http://www.xia

iOS OC底层面试题(KVC(Key-value coding)

KVC(Key-value coding) -(id)valueForKey:(NSString *)key;-(void)setValue:(id)value forKey:(NSString *)key; KVC就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值。而不需要调用明确的存取方法。这样就可以在运行时动态地访问和修改对象的属性。而不是在编译时

coding algorithm

迪杰斯特拉算法原理Dijkstra - 云+社区 - 腾讯云 (tencent.com)

eclipse开发提高coding 效率

请大家不要忽略编码效率对生产效率的提高影响,有点心得,跟大家分享下。 细节决定成败-6sigma。 btw:请大家不要跟我讨论编码效率的重要性,我同意有很多事情更重要。 麻烦投入门贴的朋友给点建议,谢谢。 写程序是一个创造过程,如同写文章,如果把思路理清楚,剩下的事情就是coding了。 纯coding的过程是很枯燥的,如何想办法把这个过程变得爽一点呢。 几个方面: 1.提

{ Cracking The Coding Interview: 150 programming QA } --- Arrays and Strings

Hashtable, ArrayList (Dynamically resizing array, providing O(1) access), StringBuffer (do string concatenation) 1. 判断string是否有重复字符,不允许使用其他数据结构。 Step 1: 问清楚限制,string的编码是ASCII还是Unicode a. 如果可以用其他数