leetcode-month1-week1
来源:互联网 发布:ssc ultimate aero数据 编辑:程序博客网 时间:2024/06/01 16:16
Add Two Numbers
package ygy.test.week1;/** * Created by guoyao on 2017/9/3. */public class AddTwoNumbers { /** * You are given two non-empty linked lists representing two non-negative integers. * The digits are stored in reverse order and each of their nodes contain a single digit. * Add the two numbers and return it as a linked list. * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) * Output: 7 -> 0 -> 8 */ public static void main(String[] args) throws Exception { ListNode l1 = new ListNode(9); //ListNode node2 = new ListNode(8); //ListNode node3 = new ListNode(9); ListNode headNode=new ListNode(0); ListNode temp = headNode ; ListNode l2 = new ListNode(1); for(int i = 0 ; i < 9 ;i ++ ) { temp.next=new ListNode(9); temp = temp.next ; } l2.next = headNode.next ; ListNode listNode=addTwoNumbers(l1, l2); do { System.out.println(listNode.val); listNode=listNode.next; }while (listNode != null ); } //正解 public ListNode addTwoNumbers_lettCode_answer(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0); ListNode p = l1, q = l2, curr = dummyHead; int carry = 0; while (p != null || q != null) { int x = (p != null) ? p.val : 0; int y = (q != null) ? q.val : 0; int sum = carry + x + y; carry = sum / 10; curr.next = new ListNode(sum % 10); curr = curr.next; if (p != null) p = p.next; if (q != null) q = q.next; } if (carry > 0) { curr.next = new ListNode(carry); } return dummyHead.next; } //会超出int 和long的界限 public static ListNode addTwoNumbers(ListNode l1, ListNode l2) throws Exception { StringBuilder l1Builder=new StringBuilder(); while (l1 != null) { l1Builder.append(l1.val); l1 = l1.next; } l1Builder.reverse(); long l1Num=Long.valueOf(l1Builder.toString()); l1Builder=new StringBuilder(); while (l2 != null) { l1Builder.append(l2.val); l2 = l2.next; } l1Builder.reverse(); long l2Num=Long.valueOf(l1Builder.toString()); long l3Num=l1Num + l2Num; //将l3转化为 listNode char[] chars=String.valueOf(l3Num).toCharArray(); ListNode tempNode=new ListNode( 0); ListNode l3 = tempNode ; for(int i = chars.length - 1 ;i >= 0 ; i --) { l3.next=new ListNode(Integer.valueOf(""+chars[i])); l3 = l3.next; } return tempNode.next ; }} class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}
Two Sum
package ygy.test.week1;import java.util.Arrays;/** * Created by guoyao on 2017/9/3. */public class TwoSum { /** * Given nums = [2, 7, 11, 15], target = 9, * Because nums[0] + nums[1] = 2 + 7 = 9, * return [0, 1]. */ public static void main(String[] args) { int[] nums = {2, 7, 11, 15} ; System.out.println(Arrays.toString(twoSum(nums,26))); } public static int[] twoSum(int[] nums, int target) { for( int i = 0 ; i < nums.length -1 ; i ++) { // n- 1 for(int j = i + 1; j < nums.length; j ++) { // n int result= nums[i] + nums[j]; if (result == target) { //时间复杂度O(n2) return new int[]{i,j}; } } } return null ; }}
阅读全文
0 0
- leetcode-month1-week1
- leetcode-month1-week2
- leetcode-month1-week3
- leetcode-month1-week4
- leetcode week1
- Leetcode Week1
- LeetCode题解 week1
- week1
- week1
- week1
- week1
- week1
- week1
- Leetcode: 70. Climbing Stairs(Week1, Easy)
- Leetcode:93. Restore IP Addresses(Week1, Medium)
- week1-leetcode #1-Two Sum[Easy]
- 算法设计Week1 LeetCode Algorithms Problem #1 Two Sum
- 算法设计Week1 LeetCode Algorithms Problem #2 Add Two Numbers
- 关于BeanCopier的一些思考
- 如何处理 Validation failed for one or more entities. See 'EntityValidationErrors' 错误
- centOS6.6 源码安装mysql-5.7.19多实例
- Gglib动态代理
- 数据库优化--优化查询语句
- leetcode-month1-week1
- QGC的FlightDisplayView
- ubuntu 16.04 莫名其妙不能连接wifi
- 23种设计模式(11):责任链模式
- CentOS命令行使用shadowsocks代理的方法
- SPI总线(二):驱动分析篇
- (转载)常用的HTTP状态码
- python3爬虫 爬取图片,爬取新闻网站文章并保存到数据库
- BeanCopier系列之三:缓存BeanCopier提升性能