图片 7

Leetcode – Add Digits

By admin in 编程 on 2019年10月8日

图片 1Paste_Image.png

图片 2Paste_Image.png

图片 3Paste_Image.png

My code:

My code:

My code:

public class Solution { public int addDigits { if (num < 0) return 0; else if (num < 10) return num; else return  % 9 + 1; }}
import java.util.ArrayList;import java.util.List;public class Solution { public List<Integer> spiralOrder(int[][] matrix) { ArrayList<Integer> result = new ArrayList<Integer>(); if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return result; int row = matrix.length; int col = matrix[0].length; for (int i = 0; i < Math.min; i++) { int temp = spiralOrder(i, row - 2 * i, col - 2 * i, matrix, result); if (temp == -1) break; } return result; } private int spiralOrder(int begin, int rowCount, int colCount, int[][] matrix, ArrayList<Integer> result) { if (rowCount <= 0 || colCount <= 0) return -1; else if (colCount == 1) { for (int i = 0; i < rowCount; i++) result.add(matrix[i + begin][begin]); return -1; } else if (rowCount == 1) { for (int i = 0; i < colCount; i++) result.add(matrix[begin][i + begin]); return -1; } int row = matrix.length - 1 - (matrix.length - rowCount) / 2; int col = matrix[0].length - 1 - (matrix[0].length - colCount) / 2; for (int i = begin; i <= col; i++) result.add(matrix[begin][i]); for (int i = begin + 1; i <= row; i++) result.add(matrix[i][col]); for (int i = col - 1; i >= begin; i--) result.add(matrix[row][i]); for (int i = row - 1; i > begin; i--) result.add(matrix[i][begin]); return 1; } public static void main(String[] args) { Solution test = new Solution(); int[][] a = new int[3][3]; a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[1][0] = 4; a[1][1] = 5; a[1][2] = 6; a[2][0] = 7; a[2][1] = 8; a[2][2] = 9; System.out.println(test.spiralOrder; }}
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode { val = x; } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode temp = head; ListNode post = temp.next; while (temp != null) { while (post != null && post.val == temp.val) post = post.next; temp.next = post; temp = post; } return head; }}

My test result:

My test result:

My test result:

图片 4

图片 5Paste_Image.png

图片 6Paste_Image.png

这道标题大概比较简单的。一伊始想用循环大概递归,但题意不供给这么做,要
O的复杂度。于是留意研商了下,果然有规律可循。

这一次难题本人的代码运转素的十分的快,预计原因便是因为自身设计了子函数,而精神上那道难点是没有要求子函数的。所以速度会快非常多。但那道题自己并未有啥样长处,就像此呢。

那道标题相比较轻易,没什么好深入分析的。

图片 7Paste_Image.png

Anyway, Good luck, Richardo!

代码写的太丑了。更新如下:

**总结: Math**

My code:

public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode p = head; while (p.next != null) { if (p.next.val == p.val) p.next = p.next.next; else p = p.next; } return head; }

Anyway, Good luck, Richardo!

public class Solution { public List<Integer> spiralOrder(int[][] matrix) { ArrayList<Integer> ret = new ArrayList<Integer>(); if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return ret; int width = matrix[0].length; int height = matrix.length; int i = 0; int j = 0; /** traversal every existed circle */ while (j + width - 1 < matrix[0].length && i + height - 1 < matrix.length) { traversal(j, i, width, height, matrix, ret); i++; j++; width = width - 2; height = height - 2; if (width <= 0 || height <= 0) // if width or height <= 0, quit break; } return ret; } /** traverse this circle */ private void traversal(int beginCol, int beginRow, int width, int height, int[][] matrix, ArrayList<Integer> ret) { for (int i = beginCol; i < beginCol + width; i++) ret.add(matrix[beginRow][i]); if (height == 1) return; for (int i = beginRow + 1; i < beginRow + height; i++) ret.add(matrix[i][beginCol + width - 1]); if (width == 1) return; for (int i = beginCol + width - 2; i >= beginCol; i--) ret.add(matrix[beginRow + height - 1][i]); for (int i = beginRow + height - 2; i >= beginRow + 1; i--) ret.add(matrix[i][beginCol]); }}

**总结: linked list remove duplicates**

My code:

那道题目简单,可是很烦。妈的。。。以为第一遍写的要精简一些。

Anyway, Good luck, Richardo!

public class Solution { public int addDigits { if (num <= 0) { return num; } while (num >= 10) { num = transfer; } return num; } private int transfer { String s = String.valueOf; int sum = 0; for (int i = 0; i < s.length { char curr = s.charAt; sum +=  (curr - 48); } return sum; }}

Anyway, Good luck, Richardo!

My code:

一最初这么写的。后来找到了规律,这么写:My code:

My code:

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode { val = x; } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode pre = head; ListNode curr = head.next; while (curr != null) { if (curr.val == pre.val) { pre.next = curr.next; curr = curr.next; } else { pre = curr; curr = curr.next; } } return head; }}
public class Solution { public int addDigits { if (num <= 0) { return num; } return num % 9 == 0 ? 9 : num % 9; }}
public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> ret = new ArrayList<Integer>(); if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return ret; } int rowBegin = 0; int rowEnd = matrix.length - 1; int colBegin = 0; int colEnd = matrix[0].length - 1; while (rowBegin <= rowEnd && colBegin <= colEnd) { for (int i = colBegin; i <= colEnd; i++) { ret.add(matrix[rowBegin][i]); } rowBegin++; for (int i = rowBegin; i <= rowEnd; i++) { ret.add(matrix[i][colEnd]); } colEnd--; if (rowBegin <= rowEnd) { for (int i = colEnd; i >= colBegin; i--) { ret.add(matrix[rowEnd][i]); } rowEnd--; } if (colBegin <= colEnd) { for (int i = rowEnd; i >= rowBegin; i--) { ret.add(matrix[i][colBegin]); } colBegin++; } } return ret; }}

以为自己本次写的笔触就很鲜明啊。轻易题,没什么好说的。

题目没什么难的。

第三次知道,恶心的主题材料得看论坛,都有最优解!现在每道标题,都看下论坛投票最高的解法。那些解法就很自然,也便于记住,制止了成千上万corner
case同一时候,记住这里,必要判断if (rowBegin <= rowEnd)if (colBegin <=
colEnd)

Anyway, Good luck, Richardo!

Anyway, Good luck, Richardo! — 08/27/2016

参考:

Anyway, Good luck, Richardo! — 08/11/2016

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 澳门新葡亰官网app 版权所有