lintcode做题总结, Sept 18
来源:互联网 发布:vmware fusion 8 mac 编辑:程序博客网 时间:2024/04/29 15:59
1. Number of 1 Bits My Submissions Question Solution
这道题本来很简单的,但是在测试中参数int的值会越界,我也不知道是怎么穿进去的
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int count = 0; for(int i=1; i<33; i++){ if(((n>>i) & (1)) != 0){ count++; } } return count; }}
2. House Robber 这道题用的是一维DP
public class Solution { public int rob(int[] num) { if(num==null || num.length==0) return 0; int n = num.length; int[] dp = new int[n+1]; dp[0]=0; dp[1]=num[0]; for (int i=2; i<n+1; i++){ dp[i] = Math.max(dp[i-1], dp[i-2]+num[i-1]); } return dp[n]; }}
public class Solution { public boolean isHappy(int n) { HashSet<Integer> set = new HashSet<Integer>(); while(!set.contains(n)){ set.add(n); n = sum(getDigits(n)); if (n == 1) return true; } return false; } public int sum(int[] arr){ int sum = 0; for(int i: arr){ sum = sum + i*i; } return sum; } public int[] getDigits(int n){ String s = String.valueOf(n); int[] result = new int[s.length()]; int i=0; while(n>0){ int m = n%10; result[i++] = m; n = n/10; } return result; }}
4. Remove Linked List Elements 链表删除元素
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode removeElements(ListNode head, int val) { ListNode helper = new ListNode(0); helper.next = head; ListNode p = helper; while(p.next != null){ if(p.next.val == val){ ListNode next = p.next; p.next = next.next; }else{ p = p.next; } } return helper.next; }}
0 0
- lintcode做题总结, Sept 18
- lintcode做题总结, Sept 15
- lintcode做题总结, Sept 16
- lintcode做题总结, Sept 30
- lintcode做题总结, Oct 01
- lintcode做题总结, Oct 05
- lintcode做题总结, Oct 07
- lintcode做题总结, Oct 31 A
- lintcode做题总结, Oct 31 B
- lintcode做题总结, 题目Rotate String 8
- LintCode数组题总结
- LintCode数据结构题总结
- LintCode字符串题总结
- LintCode排序题总结
- LintCode堆栈题总结
- LintCode图论&搜索题总结
- LintCode位运算题总结
- LintCode二分查找题总结
- 360与酷派和解,增持奇酷比例升至75%
- c++ primer plus第十二章-动态内存和类、复制构造函数
- WPF--窗口之间的值传递
- 基于xmpp客户端与服务器的快速重连方法、终端和服务器
- 校园网络 120 (数组模拟+技巧)
- lintcode做题总结, Sept 18
- ORA-12737: Instant Client Light: unsupported server character set CHS16GBK
- Java知识整理(一)之Basic
- 【排序算法】冒泡排序
- 使用Universal-Image-Loader加载系统图片并点击放大显示
- 图解Linux命令之--cp命令
- Objective-c 可变参数列表缺陷
- Need to read papers
- 南阳OJ 成绩转换