Leetcode
文章平均质量分 62
筱葭
这个作者很懒,什么都没留下…
展开
-
【Leetcode】459. Repeated Substring Pattern
方法一:思路:(1)次取长度为1,2,3,...的子串,判断是否可以构成整个字符串(2)整个字符串长度要能整除子串才有可能满足public class Solution { public boolean repeatedSubstringPattern(String s) { int len = s.length(); for (int原创 2017-03-19 10:39:17 · 380 阅读 · 0 评论 -
【Leetcode】455. Assign Cookies
思路:(1)先对g和s进行排序。(2)遍历g数组,依次为每个孩子分配蛋糕。(3)对每一个孩子,在s找出第一个不比他最小满意度小的蛋糕,若找着,则计数,并指定下一次从下一个蛋糕开始继续找,否则,以后的孩子最小满意度更大,更不可能找到满足的蛋糕,跳出循环。public class Solution { public int findContentChildren(int[] g原创 2017-03-18 10:37:44 · 325 阅读 · 0 评论 -
【Leetcode】453. Minimum Moves to Equal Array Elements
思路:全部n-1个值加1就是一个值减1。公式:sum - len * min。public class Solution { public int minMoves(int[] nums) { int sum = 0, len = nums.length; int min = Integer.MAX_VALUE; for (int原创 2017-03-16 14:45:11 · 328 阅读 · 0 评论 -
【Leetcode】543. Diameter of Binary Tree
思路:通过递归计算左右子树的深度来计算根节点的diameter(记为temp),通过和类变量 diameter 进行比较,保存较大值。在每一次递归结束后,返回左右子树的深度,将二者相加再加2就是根节点的diameter。/** * Definition for a binary tree node. * public class TreeNode { * int原创 2017-03-20 11:24:48 · 2776 阅读 · 0 评论 -
【Leetcode】532. K-diff Pairs in an Array
思路:成对的值不分先后,所以先对nums进行排序。用一个set存储出现过的值,用于后续判断是否某个值已经有值与其成对。分为两种情况:(1)k==0,即找出值相等的对数。再用一个sameSet存储所有已成对的值,避免同一个值加入结果多次。只有sameSet中不含该值,且set中包含了该值,才能加入结果。(2)k!=0,即找出差的绝对值为k的对数。只有set中不包含该值但包原创 2017-03-05 14:22:06 · 3850 阅读 · 0 评论 -
【Leetcode】400. Nth Digit
思路:1~9 9个数 9*1=9个digit10~99 90个数 90*2=180个digit 100~999 900个数 900*3=2700个digit 10^k ~ k个9连成的数 9*10^k个数 (90*10^k)*k个digit首先确定在几位数之间,如在1000~9999还是在其他之间?然后确定是该区间的哪个数?最后确定是该数字的哪个digit? 注意防止溢出。原创 2017-03-05 09:55:11 · 240 阅读 · 0 评论 -
【Leetcode】389. Find the Difference
方法一:思路:利用一个HashMap,先统计s字符串中每个字符出现的次数,然后遍历t字符串,若该字符未包含在HashMap中或出现次数等于0,立即返回该字符,否则,该字符出现的次数减一。public class Solution { public char findTheDifference(String s, String t) { Map map = n原创 2017-03-04 09:00:30 · 296 阅读 · 0 评论 -
【Leetcode】387. First Unique Character in a String
方法一:思路:Map——O(nlogn)public class Solution { public int firstUniqChar(String s) { int len = s.length(); Map map = new HashMap(); for (int i = 0; i < len; i++) {原创 2017-03-03 09:16:00 · 248 阅读 · 0 评论 -
【Leetcode】383. Ransom Note
方法一:思路:(1)使用HashMap,将magazine读取入HashMap,key是字符,value是对应该字符的个数。(2)遍历ransomNote,对HashMap进行操作,若HashMap中不含有该字符,或当前含有的字符个数个数=0,则说明ransomNote中含有的字符,在magazine中没有覆盖到。public class Solution { public原创 2017-03-02 09:59:51 · 330 阅读 · 0 评论 -
【Leetcode】371. Sum of Two Integers
思路:(1)按照位把ab相加,不考虑进位,结果是 a xor b,即1+1 =0 0+0 = 0 1+0=1,进位的请看下面。(2)计算ab的进位的话,只有二者同为1才进位,因此进位可以标示为 (a and b) (3)于是a+b可以看成 (a xor b)+ ((a and b) public class Solution { public int getSum(in原创 2017-02-28 09:03:45 · 285 阅读 · 0 评论 -
【Leetcode】374. Guess Number Higher or Lower
思路:二分查找,注意溢出问题/* The guess API is defined in the parent class GuessGame. @param num, your guess @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 int guess(i原创 2017-03-01 08:59:53 · 357 阅读 · 0 评论 -
【Leetcode】367. Valid Perfect Square
方法一:思路:二分查找:low high mid均定义为long类型,否则会溢出。public class Solution { public boolean isPerfectSquare(int num) { long low = 1; long high = num; while (low <= high) {原创 2017-02-27 09:18:56 · 436 阅读 · 0 评论 -
【Leetcode】530. Minimum Absolute Difference in BST
思路:(1)先序遍历二叉树,得到的遍历结果存入List;(2)对List进行排序,遍历List,依次判断每相邻两个值的差是否小于最小值result,是则更新result。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; *原创 2017-02-26 15:26:25 · 1687 阅读 · 0 评论 -
【Leetcode】350. Intersection of Two Arrays II
方法一:思路:用flag标记是否nums2中的该元素已经是相交的元素,若不是才加入结果list,并置flag为1然后跳出内层循环继续判断nums1的下一个元素。public class Solution { public int[] intersect(int[] nums1, int[] nums2) { int len1 = nums1.length;原创 2017-02-26 08:26:04 · 329 阅读 · 0 评论 -
【Leetcode】349. Intersection of Two Arrays
方法一:思路:为避免重复,用Set存储后再存入结果数组。遍历nums1,对nums2的每一个元素,若找到与nums1对应元素相等的元素且在Set中未出现,则加入Set。public class Solution { public int[] intersection(int[] nums1, int[] nums2) { Arrays.sort(nums原创 2017-02-25 08:33:27 · 330 阅读 · 0 评论 -
【Leetcode】345. Reverse Vowels of a String
方法一:思路:public class Solution { public String reverseVowels(String s) { StringBuilder sb = new StringBuilder(s); int left = 0, right = s.length() - 1; while (left < right) { if (原创 2017-02-24 10:07:04 · 556 阅读 · 0 评论 -
【Leetcode】344. Reverse String
方法一:思路:逆序遍历,构建逆序字符串。public class Solution { public String reverseString(String s) { StringBuilder sb = new StringBuilder(""); for (int i = s.length() - 1; i >= 0 ; i--)原创 2017-02-23 12:06:09 · 303 阅读 · 0 评论 -
【Leetcode】342. Power of Four
方法一:思路:递归,时间复杂度:O(lg(n)),空间复杂度:O(lg(n))。public class Solution { public boolean isPowerOfFour(int num) { if (num <= 0) return false; if (num == 1) return原创 2017-02-22 07:34:15 · 313 阅读 · 0 评论 -
【Leetcode】326. Power of Three
方法一:思路:递归,时间复杂度:O(lg(n)),空间复杂度:O(lg(n))。public class Solution { public boolean isPowerOfThree(int n) { if (n <= 0) return false; if (n == 1) return true原创 2017-02-20 12:02:52 · 283 阅读 · 0 评论 -
【Leetcode】520. Detect Capital
方法一:思路:将单词转换为大写得到up,将单词转换为小写得到low,若word与up或与low相等,则返回true,否则去掉word的首字母得到last,若last转换为小写后仍与last相等,则返回true,否则返回false。public class Solution { public boolean detectCapitalUse(String word) {原创 2017-02-19 14:18:45 · 950 阅读 · 0 评论 -
【Leetcode】292. Nim Game
思路:1个石子,先手全部拿走;2个石子,先手全部拿走;3个石子,先手全部拿走;4个石子,后手面对的是先手的第1,2,3情况,后手必胜;5个石子,先手拿走1个让后手面对第4种情况,后手必败;6个石子,先手拿走2个让后手面对第4种情况,后手必败;7个石子,先手拿走3个让后手面对第4种情况,后手必败。当n∈[1,3]时,先手必胜。当n == 4时,无论先手第一轮如何选取,下一轮都会转化为n∈[1原创 2017-02-18 10:04:15 · 290 阅读 · 0 评论 -
【Leetcode】290. Word Pattern
方法一:思路:String和char一一对应,建一个map保存String到char的映射,同时用set里面保存char, 避免出现多对1的情况。public class Solution {。 public boolean wordPattern(String pattern, String str) { int pLen = pattern.length原创 2017-02-17 07:20:38 · 242 阅读 · 0 评论 -
【Leetcode】283. Move Zeroes
方法一:思路:(1)index记录非零元素将要放置的位置下标。(2)遍历数组,将非零元素依次移到前面,将要放置的位置下标index加一。(3)最后再将剩余位置元素清零。public class Solution { public void moveZeroes(int[] nums) { int len = nums.length; in原创 2017-02-16 10:02:58 · 216 阅读 · 0 评论 -
【Leetcode】278. First Bad Version
思路:版本号是从1开始,一直到n,属于增序排列,因此可以采用二分查找的策略,减少比较次数。需要注意的是,在取二分查找的中间值时,不要使用(low+high)/2的方式,这样可能会在计算时产生溢出。/* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVers原创 2017-02-16 08:53:30 · 211 阅读 · 0 评论 -
【Leetcode】263. Ugly Number
方法一:思路:将给定数除以2、3、5,直到无法整除,也就是除以2、3、5的余数不再为0时停止。这时如果得到1,说明是所有因子都是2或3或5,如果不是1,则不是丑陋数。public class Solution { public boolean isUgly(int num) { if (num == 0) return false;原创 2017-02-14 09:39:27 · 210 阅读 · 0 评论 -
【Leetcode】258. Add Digits
方法一:思路:public class Solution { public int addDigits(int num) { int result = 0; while(num >= 10) { while (num > 0) { result += num % 10;原创 2017-02-13 10:21:27 · 221 阅读 · 0 评论 -
【Leetcode】504. Base 7
思路:取余数加入到result的前面,除以7,直到小于7,最后再把num加入result的前面。注意负数。public class Solution { public String convertTo7(int num) { String result = ""; int flag = 0; if (num < 0) {原创 2017-02-12 20:37:58 · 1191 阅读 · 0 评论 -
【Leetcode】257. Binary Tree Paths
思路:递归(1)若树为空,直接返回result;遇到叶子节点,返回根节点构造的result。(2)递归遍历左子树,找出所有从左孩子到叶子节点的路径。(3)递归遍历右子树,找出所有从右孩子到叶子节点的路径。(4)在每个路径前面加上根节点。/** * Definition for a binary tree node. * public class TreeNode { *原创 2017-02-11 08:10:06 · 273 阅读 · 0 评论 -
【Leetcode】242. Valid Anagram
方法一:思路:Hash表计数判相等。public class Solution { public boolean isAnagram(String s, String t) { int lenS = s.length(); int lenT = t.length(); if (lenS != lenT)原创 2017-02-10 09:10:07 · 366 阅读 · 0 评论 -
【Leetcode】500. Keyboard Row
思路:(1)用Map存储字母到所在行的映射。(2)变量words,对每一个单词,先做小写字母转换存入word,再对word的每个字母进行遍历。用flag标记是否是第一个字母,用row存储第一个字母所在的行,对后续的字母,判断其所在行是否等于row,若不是,直接跳出内层循环继续判断下一个单词。考虑到数组需要预先设置长度,而数组长度未知,所以判断整个单词完毕后将其县存入List,最后再把Lis原创 2017-02-09 09:18:49 · 3329 阅读 · 0 评论 -
【Leetcode】496. Next Greater Element I
思路:(1)flag标记是否找到待查找元素。(2)遍历findNums,查找带查找元素。一旦找到,就把flag置为1,找到后,一旦该元素大于待查找元素,就把该元素加入result的对应位置。public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { i原创 2017-02-08 09:31:28 · 1277 阅读 · 0 评论 -
【Leetcode】237. Delete Node in a Linked List
思路:(1)从链表中,删除指定的节点,但又没有给出前指针。(2)将当前结点伪装成下一个结点,然后将下一个结点删除即可。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val原创 2017-02-07 10:13:29 · 222 阅读 · 0 评论 -
【Leetcode】235. Lowest Common Ancestor of a Binary Search Tree
思路:二叉查找树或者是一棵空树,或者是具有下列性质的二叉树:若左子树不空,则左子树上所有结点的值均小于它的根结点的值;若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;左、右子树也分别为二叉查找树;没有键值相等的节点。1、如果a、b都比根节点小,则在左子树中递归查找公共节点。2、如果a、b都比根节点大,则在右子树中查找公共祖先节点。3、如果a、b一个比原创 2017-02-06 20:51:53 · 224 阅读 · 0 评论 -
【Leetcode】234. Palindrome Linked List
思路:1、遍历整个链表,将链表每个节点的值记录在数组中,再判断数组是不是一个回文数组,时间复杂度为O(n),但空间复杂度也为O(n),不满足空间复杂度要求。2、利用栈先进后出的性质,将链表前半段压入栈中,再逐个弹出与链表后半段比较。时间复杂度O(n),但仍然需要n/2的栈空间,空间复杂度为O(n)。3、反转链表法,将链表后半段原地翻转,再将前半段、后半段依次比较,判断是否相等,时间复杂原创 2017-02-06 20:44:04 · 250 阅读 · 0 评论 -
【Leetcode】232. Implement Queue using Stacks
思路:入队:栈a若不满,则直接压入栈in。出队:若out空就将in中的所有元素弹出到栈b中,然后out出栈;若out不空就直接从out中弹出元素。public class MyQueue { private Stack in; private Stack out; /** Initialize your data structure here. *原创 2017-02-03 08:52:33 · 200 阅读 · 0 评论 -
【Leetcode】225. Implement Stack using Queues
思路:用两个队列模拟一个堆栈:队列a和b(1)取栈顶元素: 返回有元素的队列的首元素(2)判栈空:若队列a和b均为空则栈空(3)入栈:a队列当前有元素,b为空(倒过来也一样)则将需要入栈的元素先放b中,然后将a中的元素依次出列并入列倒b中。(保证有一个队列是空的)(4)出栈:将有元素的队列出列即可。算法保证在任何时候都有一队列为空。public class MyStac原创 2017-02-03 08:45:08 · 229 阅读 · 0 评论 -
【Leetcode】219. Contains Duplicate II
思路:(1)用Map存储从值到索引的映射。(2)遍历数组,对每一个值,先判断其是否在Map中,若在,则将该值对应的索引减去该值映射到的索引,若所得差不大于k,则返回true。(3)将该值映射到其所在的索引。public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) {原创 2017-02-02 15:56:19 · 192 阅读 · 0 评论 -
【Leetcode】217. Contains Duplicate
方法一:思路:用一个set存储数组中出现过的元素,遍历数组元素,若该元素已存在于set中,则返回true,否则将其加入set。public class Solution { public boolean containsDuplicate(int[] nums) { Set set = new HashSet(); int len = nums.原创 2017-02-01 16:37:50 · 234 阅读 · 0 评论 -
【Leetcode】206. Reverse Linked List
方法一:递归思路:先翻转除去头结点后的链表,再把头结点插到翻转后的链表前面。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */publ原创 2017-01-31 09:05:41 · 294 阅读 · 0 评论 -
【Leetcode】205. Isomorphic Strings
思路:(1)求s和t的长度,分别记为lenS和lenT,若lenS不等于lenT,则返回false。(2)遍历字符串s,用Map记录s中字符到t中字符的映射。(3)对于s中的每个字符,若map中没有该字符对应的键,若map中已经有t中该位置字符对应的值,则返回false,否则存储s中字符到t中该字符的映射;若map中有该字符对应的键,则获取该字符对应的键所对应的值,若该值不等于t中该位原创 2017-01-30 21:42:00 · 291 阅读 · 0 评论