LeetCode算法题解
1、给定两个正整数(二进制形式表示)A和B,问把A变为B需要改变多少位(bit)?也就是说,整数A和B的二进制表示中有多少位是不同的?(181)
解法一:举例说明,为了减少复杂度,就使用八位二进制吧。设 A = 0010 1011, B = 0110 0101.
1. C = A & B = 0010 0001;
2. D = A | B = 0110 1111;
3. E = C ^ D = 0100 1110;
4. 结果E中有4个1,那么也就是说将A变成B,需要改变4位(bit)。
至于如何判断E的二进制表示中有几个1,可以采用快速移位与方法。
算法原理如下:
1. A & B,得到的结果C中的1的位表明了A和B中相同的位都是1的位;
2. A | B, 得到的结果D中的1的位表明了A和B在该位至少有一个为1的位,包含了A 与 B 都是1的位数,
经过前两步的位运算,,C 中1的位表明了A 和 B在该位都是1,D中为0的位表明了A 和 B 在该位都是0 ,所以进行第三步。
3. C ^ D,E 中为1的位表明了A 和 B不同的位。
- class Solution {
- /**
- *@param a, b: Two integer
- *return: An integer
- */
- public static int bitSwapRequired(int a, int b) {
- /* int getNum(int n)
- {
- if(n==0)
- {
- return 0;
- }
- int count=0;
- while(n)
- {
- n &= (n-1);
- count++;
- }
- return count;
- } */
- int count = 0;
- int c = a & b;
- int d = a | b;
- int n = c ^ d;
- if(n == 0)
- {
- return 0;
- }
- while(n != 0)
- {
- n &= (n-1);
- count++;
- }
- return count;
- }
- };
2.dp
- class Solution {
- public:
- int minPathSum(vector<vector<int>>& grid) {
- //use vector to represent 2 dimension array
- //hero num
- int m = grid.size(), n = grid[].size();
- vector<vector<int>> dp(m, <vector<int> (n));
- for(int i = ; i < m; i++){
- for(int j = ; j < n; j++){
- if(i == ){
- if(j == ){
- dp[i][j] = grid[i][j];
- }else{
- dp[i][j] = dp[i][j-] + grid[i][j];
- }
- }else if(j == ){
- dp[i][j] = dp[i-][j] + grid[i][j];
- }else{
- dp[i][j] = min(dp[i][j-], dp[i-][j]) + grid[i][j];
- }
- }
- }
- return dp[m-][n-];
- }
- };
3、问题描述:给定一个区间集合,合并有重叠的区间 解题思路:先对区间进行排序,按开始点进行排序,再一个一个进行合并
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18]
,
return [1,6],[8,10],[15,18]
.
- /**
- * Definition for an interval.
- * public class Interval {
- * int start;
- * int end;
- * Interval() { start = 0; end = 0; }
- * Interval(int s, int e) { start = s; end = e; }
- * }
- */
- public class Solution {
- public List<Interval> merge(List<Interval> intervals) {
- List<Interval> result = new ArrayList<Interval>();
- if(null == intervals || intervals.size() <= 0){
- return result;
- }
- Collections.sort(intervals, new Comparator<Interval>(){
- public int compare(Interval arg0, Interval arg1){
- return arg0.start - arg1.start;
- }
- });
- Interval prev = null;
- for(Interval item: intervals){
- if(null == prev || prev.end < item.start){
- result.add(item);
- prev = item;
- }else if(prev.end < item.end){
- prev.end = item.end;
- }
- }
- return result;
- }
- }
附:上面采用java实现算法,其中排序使用了Collections.sort方法实现,这里研究一下对该方法实现排序的两种方法:
方法1:对列表对象实现Comparable接口
- import java.util.*;
- public class collection_sort{
- public static void main(String[] args){
- User user = new User();
- user.setName("jack");
- user.setOrder(3);
- User user1 = new User();
- user1.setName("randy");
- user1.setOrder(2);
- List<User> list = new ArrayList<User>();
- list.add(user);
- list.add(user1);
- Collections.sort(list);
- for(User u: list){
- System.out.println(u.getName());
- }
- }
- }
- class User implements Comparable<User>{
- private String name;
- private Integer order;
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
- public Integer getOrder(){
- return order;
- }
- public void setOrder(Integer order){
- this.order = order;
- }
- public int compareTo(User arg0){
- return this.getOrder().compareTo(arg0.getOrder());
- }
- }
方法2:根据Collections.sort方法重载实现
- public class collection_sort{
- public static void main(String[] args){
- User user = new User();
- user.setName("jack");
- user.setOrder(3);
- User user1 = new User();
- user1.setName("randy");
- user1.setOrder(2);
- List<User> list = new ArrayList<User>();
- list.add(user);
- list.add(user1);
- Collections.sort(list, new Comparator<User>(){
- public int compare(User arg0, User arg1){
- return arg0.getOrder().compareTo(arg1.getOrder());
- }
- });
- for(User u: list){
- System.out.println(u.getName());
- }
- }
- }
- class User{
- private String name;
- private Integer order;
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
- public Integer getOrder(){
- return order;
- }
- public void setOrder(Integer order){
- this.order = order;
- }
- }
LeetCode算法题解的更多相关文章
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- 【算法】LeetCode算法题-Merge Two Sorted List
这是悦乐书的第148次更新,第150篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第7题(顺位题号是21).合并两个已排序的链表并将其作为新链表返回. 新链表应该通过拼接 ...
- 【算法】LeetCode算法题-Valid Parentheses
这是悦乐书的第147次更新,第149篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第6题(顺位题号是20),给定一个只包含字符'(',')','{','}','['和'] ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- Leetcode算法【34在排序数组中查找元素】
在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...
- Leetcode算法【114. 二叉树展开为链表】
上周通过一位小伙伴,加入了一个氛围很好的小群,人不多,但是大家保持着对知识的渴望,让我很感动. 我自己也有一个群,人数也不多,但是能真正互动起来一起学习,一起进步的,还是太少.所以,现在也在学习如何让 ...
- ACM金牌选手讲解LeetCode算法《栈和队列的高级应用》
大家好,我是编程熊,双非逆袭选手,字节跳动.旷视科技前员工,ACM金牌,保研985,<ACM金牌选手讲解LeetCode算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...
- ACM金牌选手讲解LeetCode算法《哈希》
大家好,我是编程熊. 往期文章介绍了<线性表>中的数组.链表.栈.队列,以及单调栈和滑动窗口. ACM金牌选手讲解LeetCode算法<线性表> ACM金牌选手讲解LeetCo ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
随机推荐
- ThinkPHP留后门技巧
原文链接:https://www.leavesongs.com/PENETRATION/thinkphp-callback-backdoor.html 90sec上有人问,我说了还有小白不会用.去年我 ...
- db2数据库新手可能碰到的问题及详解(部分内容来自网络搜索)
一.db2安装好之后出现乱码,菜单栏呈现方框状,此时选择菜单第五项,点击选择下拉菜单中的最后一项,打开选择标签卡的第三项(字体),如果是无衬线都改为有衬线,如果是有衬线改为无衬线.乱码即可解决(网上一 ...
- mac下使用glew库,方法
mac下使用glew库,方法 分类: OpenGL2015-01-15 15:52 210人阅读 评论(0) 收藏 举报 目录(?)[+] 主要参考http://www.cnblogs.com ...
- Chrome开发,debug的使用方法。
怎样打开Chrome的开发者工具? 你可以直接在页面上点击右键,然后选择审查元素: 或者在Chrome的工具中找到: 或者,你直接记住这个快捷方式: Ctrl+Shift+I (或者Ctrl+Shif ...
- window常见事件
<script type="text/javascript"> /*onunload = function(){ alert("onunload run&qu ...
- 【iCore3 双核心板】例程二十:LAN_TCPC实验——以太网数据传输
实验指导书及代码包下载: http://pan.baidu.com/s/1pJY5uXH iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 【iCore3 双核心板_FPGA】实验二十二:Niosii——固化程序到 EPCS 里
实验指导书及代码包下载: http://pan.baidu.com/s/1c2lyNQS iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- ThinkPHP 3.2.3 关联模型的使用
关于关联模型 ThinkPHP 3.2.3 的关联模型(手册地址)一般处理关联数据表的 CURD 操作,例如关联读取.关联写入.关联删除等. 实例 博客管理模块关于博客有 4 张数据表:博客表 crm ...
- java json与对象或者集合互转
package open_exe; public class User { private int id; private String name; private String gender; pu ...
- jquery 图片本地预览
uploadPreview.js /* *名称:图片上传本地预览插件 v1.1 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *参数说明: I ...