摘要:
题目链接:205.同构字符串 代码: class Solution { public boolean isIsomorphic(String s, String t) { if(s.length() != t.length()) return false; char[] map = new char 阅读全文
摘要:
题目链接:把数组排成最小的数 思路:对数组进行排序,假设要{x, y}组成最小数,那么通过比较"xy"和"yx"的大小,可以得到最小值,对整个数组按这种比较大小方式进行排序,最好得到的数组就是最小数。(证明见评论讲解) 代码: class Solution { public String minNu 阅读全文
摘要:
题目链接:重建二叉树 思路:根据前序和中序的排列规律,在中序遍历时,根节点的左边是左子树结点,右边是右子树结点,而前序遍历中首先出现根结点,紧接着根结点的是左子树结点,然后是右子树结点。所以,我们只需要确定在前序和中序中根结点的位置,通过根结点可以知道左子树和右子树结点的位置,该问题便转化为根据左右 阅读全文
摘要:
题目链接:数值的整数次方 思路:二分法。对幂次方进行二分,但需要考虑次方运算的特殊情况,这儿,0次方结果都为0,底数为1时结果都为1。 代码: class Solution { public double myPow(double x, int n) { if(x == 1 || x == 0) r 阅读全文
摘要:
博客园样式更改时间12月25日。 今天对博客园的主题进行修改,使用的是开源的博客园主题模板,样式修改方法文档在这 阅读全文
摘要:
题目:24.两两交换链表中的节点 思路:链表交换 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode 阅读全文
摘要:
题目链接:455.分发饼干 思路:排序。 代码: class Solution { public int findContentChildren(int[] g, int[] s) { int cnt = 0; Arrays.sort(g); Arrays.sort(s); for(int i=0, 阅读全文
摘要:
题目链接:387.字符串的第一个唯一字符 代码: class Solution { public int firstUniqChar(String s) { int[] map = new int[26]; for(char c : s.toCharArray()){ map[c - 'a'] ++ 阅读全文
摘要:
题目链接:103.二叉树的锯齿形层序遍历 思路:双栈。 代码: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * Tre 阅读全文
摘要:
题目链接:746使用最小费用爬楼梯 代码: class Solution { public int minCostClimbingStairs(int[] cost) { int fir, sec; fir = sec = 0; for(int i=2; i<=cost.length; i++){ 阅读全文