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不同的位。

  1. class Solution {
  2. /**
  3. *@param a, b: Two integer
  4. *return: An integer
  5. */
  6. public static int bitSwapRequired(int a, int b) {
  7.  
  8. /* int getNum(int n)
  9. {
  10. if(n==0)
  11. {
  12. return 0;
  13. }
  14. int count=0;
  15. while(n)
  16. {
  17. n &= (n-1);
  18. count++;
  19. }
  20. return count;
  21. } */
  22. int count = 0;
  23. int c = a & b;
  24. int d = a | b;
  25. int n = c ^ d;
  26. if(n == 0)
  27. {
  28. return 0;
  29. }
  30. while(n != 0)
  31. {
  32. n &= (n-1);
  33. count++;
  34. }
  35.  
  36. return count;
  37. }
  38.  
  39. };

2.dp

  1. class Solution {
  2. public:
  3. int minPathSum(vector<vector<int>>& grid) {
  4. //use vector to represent 2 dimension array
  5. //hero num
  6. int m = grid.size(), n = grid[].size();
  7. vector<vector<int>> dp(m, <vector<int> (n));
  8. for(int i = ; i < m; i++){
  9. for(int j = ; j < n; j++){
  10. if(i == ){
  11. if(j == ){
  12. dp[i][j] = grid[i][j];
  13. }else{
  14. dp[i][j] = dp[i][j-] + grid[i][j];
  15. }
  16. }else if(j == ){
  17. dp[i][j] = dp[i-][j] + grid[i][j];
  18. }else{
  19. dp[i][j] = min(dp[i][j-], dp[i-][j]) + grid[i][j];
  20. }
  21. }
  22. }
  23. return dp[m-][n-];
  24. }
  25. };

3、问题描述:给定一个区间集合,合并有重叠的区间  解题思路:先对区间进行排序,按开始点进行排序,再一个一个进行合并

Total Accepted: 49133 Total Submissions: 211712 Difficulty: Hard

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].

  1. /**
  2. * Definition for an interval.
  3. * public class Interval {
  4. * int start;
  5. * int end;
  6. * Interval() { start = 0; end = 0; }
  7. * Interval(int s, int e) { start = s; end = e; }
  8. * }
  9. */
  10. public class Solution {
  11. public List<Interval> merge(List<Interval> intervals) {
  12.  
  13. List<Interval> result = new ArrayList<Interval>();
  14.  
  15. if(null == intervals || intervals.size() <= 0){
  16. return result;
  17. }
  18.  
  19. Collections.sort(intervals, new Comparator<Interval>(){
  20. public int compare(Interval arg0, Interval arg1){
  21. return arg0.start - arg1.start;
  22. }
  23. });
  24.  
  25. Interval prev = null;
  26. for(Interval item: intervals){
  27. if(null == prev || prev.end < item.start){
  28. result.add(item);
  29. prev = item;
  30. }else if(prev.end < item.end){
  31. prev.end = item.end;
  32. }
  33. }
  34.  
  35. return result;
  36.  
  37. }
  38. }

附:上面采用java实现算法,其中排序使用了Collections.sort方法实现,这里研究一下对该方法实现排序的两种方法:
方法1:对列表对象实现Comparable接口

  1. import java.util.*;
  2.  
  3. public class collection_sort{
  4. public static void main(String[] args){
  5.  
  6. User user = new User();
  7. user.setName("jack");
  8. user.setOrder(3);
  9.  
  10. User user1 = new User();
  11. user1.setName("randy");
  12. user1.setOrder(2);
  13.  
  14. List<User> list = new ArrayList<User>();
  15. list.add(user);
  16. list.add(user1);
  17. Collections.sort(list);
  18. for(User u: list){
  19. System.out.println(u.getName());
  20. }
  21. }
  22. }
  23.  
  24. class User implements Comparable<User>{
  25. private String name;
  26. private Integer order;
  27.  
  28. public String getName(){
  29. return name;
  30. }
  31. public void setName(String name){
  32. this.name = name;
  33. }
  34. public Integer getOrder(){
  35. return order;
  36. }
  37. public void setOrder(Integer order){
  38. this.order = order;
  39. }
  40.  
  41. public int compareTo(User arg0){
  42. return this.getOrder().compareTo(arg0.getOrder());
  43. }
  44. }

方法2:根据Collections.sort方法重载实现

  1. public class collection_sort{
  2.  
  3. public static void main(String[] args){
  4.  
  5. User user = new User();
  6. user.setName("jack");
  7. user.setOrder(3);
  8.  
  9. User user1 = new User();
  10. user1.setName("randy");
  11. user1.setOrder(2);
  12.  
  13. List<User> list = new ArrayList<User>();
  14. list.add(user);
  15. list.add(user1);
  16.  
  17. Collections.sort(list, new Comparator<User>(){
  18.  
  19. public int compare(User arg0, User arg1){
  20. return arg0.getOrder().compareTo(arg1.getOrder());
  21. }
  22. });
  23.  
  24. for(User u: list){
  25. System.out.println(u.getName());
  26. }
  27. }
  28. }
  29.  
  30. class User{
  31. private String name;
  32. private Integer order;
  33.  
  34. public String getName(){
  35. return name;
  36. }
  37. public void setName(String name){
  38. this.name = name;
  39. }
  40. public Integer getOrder(){
  41. return order;
  42. }
  43. public void setOrder(Integer order){
  44. this.order = order;
  45. }
  46. }

LeetCode算法题解的更多相关文章

  1. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  2. 【算法】LeetCode算法题-Merge Two Sorted List

    这是悦乐书的第148次更新,第150篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第7题(顺位题号是21).合并两个已排序的链表并将其作为新链表返回. 新链表应该通过拼接 ...

  3. 【算法】LeetCode算法题-Valid Parentheses

    这是悦乐书的第147次更新,第149篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第6题(顺位题号是20),给定一个只包含字符'(',')','{','}','['和'] ...

  4. Leetcode 简略题解 - 共567题

    Leetcode 简略题解 - 共567题     写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...

  5. Leetcode算法【34在排序数组中查找元素】

    在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...

  6. Leetcode算法【114. 二叉树展开为链表】

    上周通过一位小伙伴,加入了一个氛围很好的小群,人不多,但是大家保持着对知识的渴望,让我很感动. 我自己也有一个群,人数也不多,但是能真正互动起来一起学习,一起进步的,还是太少.所以,现在也在学习如何让 ...

  7. ACM金牌选手讲解LeetCode算法《栈和队列的高级应用》

    大家好,我是编程熊,双非逆袭选手,字节跳动.旷视科技前员工,ACM金牌,保研985,<ACM金牌选手讲解LeetCode算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...

  8. ACM金牌选手讲解LeetCode算法《哈希》

    大家好,我是编程熊. 往期文章介绍了<线性表>中的数组.链表.栈.队列,以及单调栈和滑动窗口. ACM金牌选手讲解LeetCode算法<线性表> ACM金牌选手讲解LeetCo ...

  9. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

随机推荐

  1. ThinkPHP留后门技巧

    原文链接:https://www.leavesongs.com/PENETRATION/thinkphp-callback-backdoor.html 90sec上有人问,我说了还有小白不会用.去年我 ...

  2. db2数据库新手可能碰到的问题及详解(部分内容来自网络搜索)

    一.db2安装好之后出现乱码,菜单栏呈现方框状,此时选择菜单第五项,点击选择下拉菜单中的最后一项,打开选择标签卡的第三项(字体),如果是无衬线都改为有衬线,如果是有衬线改为无衬线.乱码即可解决(网上一 ...

  3. mac下使用glew库,方法

    mac下使用glew库,方法 分类: OpenGL2015-01-15 15:52 210人阅读 评论(0) 收藏 举报   目录(?)[+]   主要参考http://www.cnblogs.com ...

  4. Chrome开发,debug的使用方法。

    怎样打开Chrome的开发者工具? 你可以直接在页面上点击右键,然后选择审查元素: 或者在Chrome的工具中找到: 或者,你直接记住这个快捷方式: Ctrl+Shift+I (或者Ctrl+Shif ...

  5. window常见事件

    <script type="text/javascript"> /*onunload = function(){ alert("onunload run&qu ...

  6. 【iCore3 双核心板】例程二十:LAN_TCPC实验——以太网数据传输

    实验指导书及代码包下载: http://pan.baidu.com/s/1pJY5uXH iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  7. 【iCore3 双核心板_FPGA】实验二十二:Niosii——固化程序到 EPCS 里

    实验指导书及代码包下载: http://pan.baidu.com/s/1c2lyNQS iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  8. ThinkPHP 3.2.3 关联模型的使用

    关于关联模型 ThinkPHP 3.2.3 的关联模型(手册地址)一般处理关联数据表的 CURD 操作,例如关联读取.关联写入.关联删除等. 实例 博客管理模块关于博客有 4 张数据表:博客表 crm ...

  9. java json与对象或者集合互转

    package open_exe; public class User { private int id; private String name; private String gender; pu ...

  10. jquery 图片本地预览

    uploadPreview.js /* *名称:图片上传本地预览插件 v1.1 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *参数说明: I ...