算法题解
Lnho
发表是最好的记忆
展开
-
数独的Java版解法
最近偶尔有玩数独,有的题太复杂了不好解,刚好看到LeetCode上有这样的题,就尝试写了个Java的解法。1. 数独介绍数独盘面是个九宫,每一宫又分为九个小格。在这八十一格中给出一定的已知数字和解题条件,利用逻辑和推理,在其他的空格上填入1-9的数字。使1-9每个数字在每一行、每一列和每一宫中都只出现一次,所以又称“九宫格”。左边是数独的题目,右边是完成后的结果。 2.原创 2017-11-25 15:27:54 · 7600 阅读 · 0 评论 -
Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
题目: You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?翻译: 你在爬一个楼梯。到达顶部有n级阶梯。 每次你可以选择原创 2016-02-27 17:49:25 · 2932 阅读 · 0 评论 -
Leet Code OJ 202. Happy Number [Difficulty: Easy]
题目: Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2016-02-27 18:02:05 · 706 阅读 · 0 评论 -
Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
题目: Given two strings s and t, write a function to determine if t is an anagram of s.For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false.Note: You may assume原创 2016-02-26 12:58:24 · 1346 阅读 · 2 评论 -
Leet Code OJ 263. Ugly Number [Difficulty: Easy]
题目: Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since原创 2016-02-27 17:33:29 · 1002 阅读 · 0 评论 -
Leet Code OJ 169. Majority Element [Difficulty: Easy]
题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element al原创 2016-02-26 18:36:19 · 1691 阅读 · 0 评论 -
Leet Code OJ 2. Add Two Numbers [Difficulty: Medium]
题目: You are given two linked lists representing two non-negative numbers. 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原创 2016-03-23 13:48:25 · 4296 阅读 · 0 评论 -
Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]
题目: Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every elemen原创 2016-02-26 18:16:24 · 1879 阅读 · 0 评论 -
Leet Code OJ 283. Move Zeroes [Difficulty: Easy]
题目: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your原创 2016-02-26 14:36:32 · 1224 阅读 · 0 评论 -
Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目: Invert a binary tree.4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1代码实现:/** * Definition for a binary tree node. * public class TreeNod原创 2016-02-26 14:16:12 · 894 阅读 · 0 评论 -
Leet Code OJ 104. Maximum Depth of Binary Tree [Difficulty: Easy]
题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.代码实现:/** * Definition for a binary tr原创 2016-02-26 14:13:39 · 1672 阅读 · 2 评论 -
Leet Code OJ 258. Add Digits [Difficulty: Easy]
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one dig原创 2016-02-26 14:09:24 · 1581 阅读 · 1 评论 -
Leet Code OJ 231. Power of Two [Difficulty: Easy]
题目: Given an integer, write a function to determine if it is a power of two.分析: 题意是给定一个整数,判断它是不是2的n次方。代码实现:public class Solution { public boolean isPowerOfTwo(int n) { if(n<1){原创 2016-02-26 13:49:20 · 712 阅读 · 0 评论 -
Leet Code OJ 136. Single Number [Difficulty: Medium]
题目: Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using ext原创 2016-02-26 11:56:53 · 682 阅读 · 0 评论 -
Leet Code OJ 100. Same Tree [Difficulty: Easy]
题目: Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.翻译: 给2个二叉树,写一原创 2016-02-27 17:39:05 · 940 阅读 · 0 评论 -
Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
题目: Given a string containing just the characters , determine if the input string is valid.The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.翻译:原创 2016-03-24 13:22:19 · 4863 阅读 · 0 评论 -
Leet Code OJ 112. Path Sum [Difficulty: Easy]
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example: Given the below binary tree and su原创 2016-02-29 19:19:14 · 876 阅读 · 0 评论 -
Leet Code OJ 482. License Key Formatting [Difficulty: Medium]
给定一个非空字符串S,代表一个软件授权秘钥,我们需要格式化这个秘钥。这个字符串由字母、数字(a-z and/or A-Z and/or 0-9)和中划线(-)组成。中划线把字符串S分为几个组(例如有M个中划线,那会被分为M+1个组)。现在,中划线放置的位置可能被放错了。我们希望每个组的字符长度为整数K(除了第一个组,允许少于K,但是至少要有一个字符)。为了满足这个需求,我们需要重新放置中划线。另外,还需要把所有小写原创 2017-02-04 06:28:32 · 1894 阅读 · 0 评论 -
Leet Code OJ 3. Longest Substring Without Repeating Characters
题目Given a string, find the length of the longest substring without repeating characters.Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”, with the原创 2017-02-14 14:16:01 · 1430 阅读 · 0 评论 -
Leet Code OJ 4. Median of Two Sorted Arrays [Difficulty: Hard]
题目There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:nums1 = [1, 3]nums原创 2017-02-10 07:36:39 · 813 阅读 · 0 评论 -
Leet Code OJ 388. Longest Absolute File Path [Difficulty: Medium]
题目Suppose we abstract our file system by a string in the following manner:The string “dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext” represents:dir subdir1 subdir2 file.ext The dir原创 2017-02-06 13:16:33 · 908 阅读 · 1 评论 -
Leet Code OJ 344. Reverse String [Difficulty: Easy]
题目: Write a function that takes a string as input and returns the string reversed. Example: Given s = “hello”, return “olleh”.翻译: 写一个函数,使用字符串作为输入,返回它反转后的结果。 例如,输入”hello”,返回”olleh”。分析: 转为字符数组后,将第一原创 2016-06-23 13:50:55 · 3694 阅读 · 0 评论 -
LeetCode刷题指南(一)
以下是我个人做题过程中的一些体会: 1. LeetCode的题库越来越大,截止到目前,已经有321个问题了。对于大多数人来说,没有时间也没有必要把所有题目都做一遍(时间充裕可以随意)。刷个100题左右应该就差不多了(可以考虑序号为前100多的题目,相对更经典一点)。 2. 从AC率高的开始做,难度从简单->中等,先不要做困难的。 3. 可以按照下文的面试出题频率顺序来做,从频率最高的一批开始。原创 2016-03-23 15:02:40 · 116111 阅读 · 11 评论 -
Leet Code OJ 91. Decode Ways [Difficulty: Medium]
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number原创 2016-06-24 13:57:28 · 5319 阅读 · 0 评论 -
Leet Code OJ 15. 3Sum[Difficulty: Medium]
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: Elements in a triplet (a,b,c) must原创 2016-05-04 13:21:46 · 6529 阅读 · 0 评论 -
Leet Code OJ 28. Implement strStr() [Difficulty: Easy]
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.翻译: 实现一个方法strStr()。返回字符串needle第一次在字符串haystack出现的下标,如果needle不是haysta原创 2016-04-28 16:46:25 · 4420 阅读 · 0 评论 -
Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]
题目: Write a function to find the longest common prefix string amongst an array of strings.翻译: 写一个函数去找到在字符串数组里面的最长的前缀字符串。分析: 首先考虑字符串数组的length为0和为1的情况,为1的情况如果不预先处理,后面判断起来会比较麻烦。然后需要找出这些字符串的最短的长度,超过这个长度原创 2016-03-25 18:36:12 · 1509 阅读 · 0 评论 -
Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car” is not原创 2016-03-25 13:20:39 · 2890 阅读 · 0 评论 -
Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]
题目: Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11’ has binary representation 00000000原创 2016-03-14 15:05:14 · 1005 阅读 · 0 评论 -
Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: But the following is not: Note: Bonus points if you原创 2016-02-29 17:21:39 · 855 阅读 · 0 评论 -
Leet Code OJ 292. Nim Game [Difficulty: Easy]
题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be原创 2016-02-26 12:28:17 · 944 阅读 · 0 评论 -
Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]
题目: Given an index k, return the kth row of the Pascal’s triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space?翻译: 给定一个下标k,返回第原创 2016-03-22 13:54:14 · 1232 阅读 · 2 评论 -
Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]
题目: Given numRows, generate the first numRows of Pascal’s triangle. For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]翻译: 给定一个数numRows,产生前numRows行的原创 2016-03-22 11:32:42 · 1462 阅读 · 0 评论 -
Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addit原创 2016-03-17 13:17:21 · 2751 阅读 · 0 评论 -
Leet Code OJ 66. Plus One [Difficulty: Easy]
题目: Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.翻译: 给定一个非负数,它是有数字的数组组成,原创 2016-03-04 16:41:04 · 1283 阅读 · 0 评论 -
Leet Code OJ 58. Length of Last Word [Difficulty: Easy]
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defin原创 2016-03-04 13:53:36 · 1215 阅读 · 0 评论 -
Leet Code OJ 27. Remove Element [Difficulty: Easy]
题目: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn’t matter what you leave beyond the new length.翻译:原创 2016-03-04 13:40:30 · 2447 阅读 · 0 评论 -
Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]
题目: Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.翻译: 给定一个排序号的链表,删除所有的重复元素,保原创 2016-03-04 13:15:36 · 1259 阅读 · 0 评论 -
Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.翻译: 合并2个已经排序的链表,并且返回一个新的链表。这个新的链表应该由前面提到的2个链表的节点所组成。分析原创 2016-03-04 13:04:41 · 3853 阅读 · 0 评论 -
Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]
题目: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2016-03-16 14:02:13 · 1689 阅读 · 0 评论