编程题训练3
来源:互联网 发布:输入框获取焦点js 编辑:程序博客网 时间:2024/06/07 03:40
编程题训练3
链表求和
- 你有两个用链表代表的整数,其中每个节点包含一个数字。数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头。写出一个函数将两个整数相加,用链表形式返回和。
样例:
给出两个链表 3->1->5->null 和 5->9->2->null,返回 8->0->8->null
public class Solution { /** * @param l1: the first list * @param l2: the second list * @return: the sum list of l1 and l2 */ public ListNode addLists(ListNode l1, ListNode l2) { ListNode listNode = new ListNode(0); ListNode r = listNode; int carry = 10; int carryVal = 0; ListNode t1 = l1; ListNode t2 = l2; while (t1 != null || t2 != null) { boolean t1IsNull = t1 == null; boolean t2IsNull = t2 == null; int val1 = t1IsNull ? 0 : t1.val; int val2 = t2IsNull ? 0 : t2.val; int i = (val1 + val2 + carryVal); //是否进位 int i1 = i - carry; boolean isCarry = i1 >= 0; //进位下次默认+1计算 carryVal = isCarry ? 1 : 0; //收集结果 ListNode node = new ListNode(isCarry ? i1 : i); r.next = node; r = node; t1 = t1IsNull ? null : t1.next; t2 = t2IsNull ? null : t2.next; } //计算结束,有进位默认前面 + 1 if (carryVal == 1) { r.next = new ListNode(1); } return listNode.next; }}
二叉树最大深度
- 给定一个二叉树,找出其最大深度。
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /** * @param root: The root of binary tree. * @return: An integer. */ public int maxDepth(TreeNode root) { // write your code here if(root==null){ return 0; } int left=maxDepth(root.left); int right=maxDepth(root.right); return left> right? left+1:right+1; }}
翻转链表
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
图解反转列表
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The head of linked list. * @return: The new head of reversed linked list. */ public ListNode reverse(ListNode head) { // write your code here if (head == null) { return head; } ListNode pre = head; ListNode cur = head.next; ListNode next = null; while(cur != null){ next = cur.next; cur.next = pre; pre = cur; cur = next; } head.next = null; head = pre; return head; }}
转换字符串到整数
- 实现atoi这个函数,将一个字符串转换为整数。如果没有合法的整数,返回0。如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-2147483648)如果是负整数。
样例
“10” =>10
“-1” => -1
“123123123123123” => 2147483647
“1.0” => 1
public class Solution { /** * @param str: A string * @return An integer */ public int atoi(String str) { // write your code here //去除空格 str = str.trim(); int result = 0; boolean isPos = true; for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); if(i == 0 && (c =='-'||c =='+')){ isPos = c=='+'?true:false; } else if (c>='0' && c<='9'){ // 检查溢出情况,不能放在后面,因为 resulte 是int 值 if( result > ( Integer.MAX_VALUE - (c - '0') ) / 10){ return isPos? Integer.MAX_VALUE : Integer.MIN_VALUE; } //进一位 result *= 10; //将c 转换为数据再加 result += c - '0'; } else { //有小数点的情况,直接返回结果 return isPos?result:-result; } } return isPos?result:-result; }}
0 0
- 编程题训练3
- 编程题训练1
- 编程题训练4
- 编程题训练5
- JS编程训练 | 题3:移除数组中的元素
- 编程训练
- 编程训练
- 有趣的逻辑思维训练编程题
- JS编程训练 | 题2:数组求和
- JS编程训练 | 题8:数组合并
- JS编程训练 | 题9:添加元素
- JS编程训练 | 题10:计数
- JS编程训练 | 题16:计时器
- Java集合编程题训练(一)
- OpenCV编程->Haar训练(3)
- C++编程训练资源
- 编程训练矩阵问题
- 编程训练新郎新娘
- 疯狂Java笔记:3.2 标识符和关键字
- 【小结】机器学习中的正则化范数 -- L1范数与L2范数
- 从0到1:我是如何在一年内无师自通机器学习的?
- 设计模式笔记十:装饰器模式
- PAT 1098-Insertion or Heap Sort (25)
- 编程题训练3
- MongoDB文档翻译-聚集-聚集管道优化
- 【解题报告】Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)(先占个位置,里面暂时什么都没有)
- 编程题训练4
- mvc:annotation-driven 元素,HttpMessageConverter,RequestMappingHandlerAdapter
- winform中treeview绑定数据库的数
- sklearn学习-SVM例程总结2(特征选择——单因素方差分析(方差分析anova ))
- Hibernate与 MyBatis的比较
- CSS3 Box-sizing