随笔分类 -  算法题

摘要:自从JavaAPI&RegExp用熟练了之后就变得越来越任性越来越懒了):public class Solution { public int strStr(String haystack, String needle) { return haystack.indexOf(ne... 阅读全文
posted @ 2015-04-03 10:52 Pickle 阅读(142) 评论(0) 推荐(0)
摘要:线性表的删除操作,这里可以用ArrayList实现简单的完成。(偷懒)public class Solution { public int removeElement(int[] A, int elem) { ArrayList arr = new ArrayL... 阅读全文
posted @ 2015-04-03 10:42 Pickle 阅读(178) 评论(0) 推荐(0)
摘要:删除数组里重复的元素,返回数组的元素个数。可以利用set集合元素不重复的特点来保存。public class Solution { public int removeDuplicates(int[] A) { java.util.SortedSet set = ... 阅读全文
posted @ 2015-04-03 10:31 Pickle 阅读(151) 评论(0) 推荐(0)
摘要:依然是链表的简单操作,把两个链表按大小顺序和成一个链表,但是还是要注意细节。下面是效率不高但是简单易懂的一种解法。需要注意两个链表都为空的情况。/** * Definition for singly-linked list. * public class ListNode { * int ... 阅读全文
posted @ 2015-04-02 23:51 Pickle 阅读(175) 评论(0) 推荐(0)
摘要:题目意思非常简单,判断所给字符串中的括号是否匹配,需要注意的问题:)(这样是不匹配的。 public class Solution { public boolean isValid(String s) { Stack stack = new Stack(); ... 阅读全文
posted @ 2015-04-02 20:43 Pickle 阅读(174) 评论(0) 推荐(0)
摘要:删除链表中倒数第n个节点,需要考虑的细节:链表为空时,链表只有一个节点时,n=1时。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ... 阅读全文
posted @ 2015-04-02 20:13 Pickle 阅读(173) 评论(0) 推荐(0)
摘要:题目虽然简单但是要高效准确还是要很细心才是,粗心是BUG的最大制造者。public class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length == 0) ... 阅读全文
posted @ 2015-04-01 19:42 Pickle 阅读(159) 评论(0) 推荐(0)
摘要:题目没有太大意思if else就能过了。public class Solution { public int romanToInt(String s) { int num = 0, i; for(i=0; i<s.length(); i++) ... 阅读全文
posted @ 2015-04-01 19:06 Pickle 阅读(150) 评论(0) 推荐(0)
摘要:用Java刷这道题没有太大意义。public class Solution { public boolean isPalindrome(int x) { boolean ans = false; StringBuilder sb = new Str... 阅读全文
posted @ 2015-04-01 18:48 Pickle 阅读(204) 评论(0) 推荐(0)
摘要:把所给的字符串按照规定转化成相应的数字。要考虑溢出的情况,含有非法字符的情况,数字前有空格的情况。但是还是比较简单的。public class Solution { public int atoi(String str) { StringBuilder s = ... 阅读全文
posted @ 2015-04-01 18:31 Pickle 阅读(174) 评论(0) 推荐(0)
摘要:反转数字,考虑溢出的情况。直接返回零(好坑啊)。public class Solution { public int reverse(int x) { if(x == 0) return 0; StringBuilder sb = new St... 阅读全文
posted @ 2015-04-01 00:38 Pickle 阅读(167) 评论(0) 推荐(0)
摘要:二进制转换和字符串逆序。要考虑int的范围,测试数据是有溢出的。Math.pow是有精度损失的,最好写成整数的。public class ReverseBits { public static int reverseBits(int n) { StringBuilder... 阅读全文
posted @ 2015-03-31 23:45 Pickle 阅读(189) 评论(0) 推荐(0)
摘要:Z字形排列,找规律。public class Solution { public String convert(String s, int nRows) { StringBuilder[] sbArr = new StringBuilder[nRows]; for(... 阅读全文
posted @ 2015-03-31 12:21 Pickle 阅读(197) 评论(0) 推荐(0)
摘要:和另一道题相反,把英文字母的26进制转换成10进制。思路非常清晰。public class Solution { public int titleToNumber(String s) { int ans = 0; for(int i=s.length()-1; i... 阅读全文
posted @ 2015-03-30 22:57 Pickle 阅读(215) 评论(0) 推荐(0)
摘要:时隔几分钟又来写一个题,这个应该算个水题。public class Solution { public String convertToTitle(int n) { StringBuilder ans = new StringBuilder(); ... 阅读全文
posted @ 2015-03-29 23:11 Pickle 阅读(186) 评论(0) 推荐(0)
摘要:参考了别人的想法,思路简洁,效率高。虽然是在充分理解别人的思路后写出的代码,但是还是发现了自己的不足之处。以后还是要多思考多写。加油吧!public class Solution { public int majorityElement(int[] num) { ... 阅读全文
posted @ 2015-03-29 22:42 Pickle 阅读(174) 评论(0) 推荐(0)
摘要:数列递推,求出公式计算按要求输出即可。 代码 : 阅读全文
posted @ 2014-12-30 11:33 Pickle 阅读(269) 评论(0) 推荐(1)
摘要:这个题比较长,如果按照题目要求一步一步做的话。也不是很难。但是如果想提高代码精简度和可读性高效性确实不容易,想轻松AC还是用C++,C语言的输入输出。Java很容易超时。普通算法要10s左右。 Java代码 : 来自互联网的C++代码 : 阅读全文
posted @ 2014-12-29 11:28 Pickle 阅读(660) 评论(0) 推荐(0)
摘要:描述 : 输入打印任务及关注的任务在输入中的位置,从0开始。如果当前任务的优先级不是最高的则把当前任务加在队列最后。计算当前任务打印完成的时刻。每个任务的打印都是需要1分钟。 思路 : 类似于优先级队列,但是不一样。设置一个数组记录优先级并由大到小排序,然后模拟执行。 代码 : 阅读全文
posted @ 2014-12-29 00:04 Pickle 阅读(366) 评论(0) 推荐(0)
摘要:统计国家代号出现的次数,如果去的和来的相等就输出YES即可。 阅读全文
posted @ 2014-12-26 22:10 Pickle 阅读(236) 评论(0) 推荐(0)