随笔分类 - 算法题
摘要:Description:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, "A man, a plan, ...
阅读全文
摘要:Description:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3, Return [1,3,3,1].public class Solution { public...
阅读全文
摘要:Description:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3...
阅读全文
摘要:Description: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 whi...
阅读全文
摘要:Description:Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop(...
阅读全文
摘要:Description:The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11. 1...
阅读全文
摘要:Description:Invert a binary tree. 4 / \ 2 7/ \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1递归invert就好了。/** * Definition for a binary tree ...
阅读全文
摘要:Description: Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i...
阅读全文
摘要://求一个整数的二进制串中1的个数public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_count = 0; for(int i=0; i<b_str.length(...
阅读全文
摘要:判断产品版本号的大小。public class Solution { public int compareVersion(String version1, String version2) { String[] v1 = version1.split("\\."); ...
阅读全文
摘要:从右向左旋转数组,一共k次。k可以是任意数字。public class Solution { public void rotate(int[] nums, int k) { if(k == 0 || nums.length==0 || nums.length==1) ret...
阅读全文
摘要:题意很好理解,判断两个字符串的结构是否相同。测试数据里有?{}【】等符号啊,果断扩大到300.public class Solution { public static boolean isIsomorphic(String s, String t) { if(isIsomorphicOn...
阅读全文
摘要:反转链表,用了一个比较笨的方法。public class Solution { public ListNode reverseList(ListNode head) { if(head == null || head.next == null) retur...
阅读全文
摘要:找出n!中零的个数。 对n!做质因数分解n!=2x*3y*5z*... 显然0的个数等于min(x,z),并且min(x,z)==z 证明: 对于阶乘而言,也就是1*2*3*...*n [n/k]代表1~n中能被k整除的个数 那么很显然 [n/2] > [n/5] (左边是逢2增1,右边是逢5增1...
阅读全文
摘要:求二叉树的最小深度。/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) ...
阅读全文
摘要:判断一颗二叉树中是否存在一天路径(从根节点到叶子节点)的val值之和等于给定的sum。注意判断root为null的时候。 成也递归败也递归。。。。。/** * Definition for binary tree * public class TreeNode { * int val; *...
阅读全文
摘要:判断小于n数中素数的个数,如果用普通的判断方法会超时,这里使用筛选法。 具体请参考:http://blog.csdn.net/liukehua123/article/details/5482854public class Solution { public int countPrimes(i...
阅读全文
摘要:删除链表中的指定元素。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } ...
阅读全文
摘要:把上一个类似的题目中的list反转就可以了。/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * Tree...
阅读全文
摘要:递归求二叉树的最大深度。/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x...
阅读全文
浙公网安备 33010602011771号