当前位置:小鱼儿玄机二站 > 编程应用 > Leetcode - Integer to Roman

Leetcode - Integer to Roman

文章作者:编程应用 上传时间:2019-10-08

Question:

图片 1Paste_Image.png

图片 2Paste_Image.png

My code:

My code:

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode { val = x; } * } */public class Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null || k < 0) return head; else if (head.next == null) return head; ListNode dummy = new ListNode(Integer.MIN_VALUE); dummy.next = head; ListNode tail = head; int total = 1; while (tail.next != null) { tail = tail.next; total++; } k = k % total; if  return dummy.next; ListNode curr = dummy; for (int i = 0; i < (total - k); i++) curr = curr.next; if (curr.next == null) return dummy.next; ListNode newHead = curr.next; curr.next = null; tail.next = dummy.next; dummy.next = newHead; return dummy.next; } public static void main(String[] args) { Solution test = new Solution(); ListNode n1 = new ListNode; ListNode n2 = new ListNode; ListNode n3 = new ListNode; ListNode n4 = new ListNode; ListNode n5 = new ListNode; n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; System.out.println(test.rotateRight; }}
public class Solution { public String intToRoman { if (num <= 0 || num > 3999) return null; String result = ""; int numM = num / 1000; for (int i = 0; i < numM; i++) result += 'M'; num = num - (num / 1000) * 1000; //100-999 int numD = 0; int numC = 0; if (num >= 900) { num = num - 900; result += "CM"; } else if (num >= 500){ numD = num / 500; num = num - (num / 500) * 500; numC = num / 100; num = num - (num / 100) * 100; for (int i = 0; i < numD; i++) result += 'D'; for (int i = 0; i < numC; i++) result += 'C'; } else if (num >= 400) { num = num - 400; result += "CD"; } else { numC = num / 100; num = num - (num / 100) * 100; for (int i = 0; i < numC; i++) result += 'C'; } //10-99 int numL = 0; int numX = 0; if (num >= 90) { num = num - 90; result += "XC"; } else if (num >= 50) { numL = num / 50; num = num -  * 50; numX = num / 10; num = num -  * 10; for (int i = 0; i < numL; i++) result += 'L'; for (int i = 0; i < numX; i++) result += 'X'; } else if (num >= 40) { num = num - 40; result += "XL"; } else { numX = num / 10; num = num -  * 10; for (int i = 0; i < numX; i++) result += 'X'; } //1-9 int numV = 0; int numI = 0; if (num >= 9) { num = num - 9; result += "IX"; } else if (num >= 5){ numV = num / 5; num = num -  * 5; numI = num; for (int i = 0; i < numV; i++) result += 'V'; for (int i = 0; i < numI; i++) result += 'I'; } else if (num >= 4) { num = num - 4; result += "IV"; } else { numI = num; for (int i = 0; i < numI; i++) result += 'I'; } return result; } public static void main(String[] args) { Solution test = new Solution(); int a = 2014; System.out.println(test.intToRoman; }}

My test result:

My test result:

图片 3Paste_Image.png

图片 4Paste_Image.png

这一次难题也轻易。先遍历贰回寻找总个数 total然后基于 total - k 找到特别断裂点。断开。在用dummy和新的头接上。原本的头接到tail前边去,就多数了。主要本人是未能精晓, k >= total 是怎么着含义。查了之后察觉,做三个 k = k % total 处理就行了。

此番难题从没怎么看头。便是得先去领略下拉各斯数字的平整。然后写一些if语句就行。主要的境况是,只要那么多少个标识符。

**总结: LinkedList rotate**

图片 5Paste_Image.png

Anyway, Good luck, Richardo!

譬喻说一个数在100-999之间。那么就得分处境。假诺 >= 900, 那么百位上的数就能够用 CM来代表若是 >= 500, 那正是DCCC.....要是 >= 400, 那便是CD....其余, 那就是CCC.....这样之后就足以拿走 两位数, XX然后一样服从 90,50,40,别的的意况来差异。。就那样推算。。。

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode { val = x; } * } */public class Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null) return null; int n = 1; ListNode temp = head; ListNode tail = head; while (temp.next != null) { n++; temp = temp.next; tail = temp; } k = k % n; if  return head; temp = head; for (int i = 1; i < n - k; i++) { temp = temp.next; } ListNode newHead = temp.next; temp.next = null; tail.next = head; return newHead; } public static void main(String[] args) { ListNode n1 = new ListNode; ListNode n2 = new ListNode; ListNode n3 = new ListNode; ListNode n4 = new ListNode; ListNode n5 = new ListNode; n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; Solution test = new Solution(); ListNode head = test.rotateRight; ListNode temp = head; while (temp != null) { System.out.print(temp.val + "->"); temp = temp.next; } }}

顺手叁个胡志明市数字表和命名法规, Good luck, Richardo!

有三个 corner case未有设想到,当k= 0 时,那年是无需旋转的,若是强制旋转, newHead = tail.next -> null. 会出错。

My code:

Anyway, Good luck, Richardo!

public class Solution { public String intToRoman { if (num <= 0) { return null; } int[] values = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; String[] strs = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; StringBuilder sb = new StringBuilder(); for (int i = 0; i < values.length; i++) { while (num >= values[i]) { sb.append; num -= values[i]; } } return sb.toString(); }}

My code:

reference:

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode { val = x; } * } */public class Solution { public ListNode rotateRight(ListNode head, int k) { if (k < 0 || head == null) { return null; } ListNode tail = head; int total = 0; while (tail.next != null) { total++; tail = tail.next; } total++; k = k % total; if  { return head; } int counter = total - k - 1; ListNode curr = head; while (counter > 0) { curr = curr.next; counter--; } ListNode newHead = curr.next; curr.next = null; tail.next = head; return newHead; }}

一贯看的答案给的最优解,记住就行。

k = 0 的时候,无需rotate,直接回到就行。Anyway, Good luck, Richardo! -- 08/16/2016

Anyway, Good luck, Richardo! -- 09/17/2016

本文由小鱼儿玄机二站发布于编程应用,转载请注明出处:Leetcode - Integer to Roman

关键词: