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

 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         
37         return count;
38     }
39     
40  
41 };

 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[0].size();
 7         vector<vector<int>> dp(m, <vector<int> (n));
 8         for(int i = 0; i < m; i++){
 9             for(int j = 0; j < n; j++){
10                 if(i == 0){
11                     if(j == 0){
12                         dp[i][j] = grid[i][j];
13                     }else{
14                         dp[i][j] = dp[i][j-1] + grid[i][j];
15                     }
16                 }else if(j == 0){
17                     dp[i][j] = dp[i-1][j] + grid[i][j];
18                 }else{
19                     dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + grid[i][j];
20                 }
21             }
22         }
23         return dp[m-1][n-1];
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 
18         Collections.sort(list, new Comparator<User>(){
19 
20             public int compare(User arg0, User arg1){
21                 return arg0.getOrder().compareTo(arg1.getOrder());
22             }
23         });
24 
25         for(User u: list){
26             System.out.println(u.getName());
27         }
28     }
29 }
30 
31 class User{
32     private String name;
33     private Integer order;
34 
35     public String getName(){
36         return name;
37     }
38     public void setName(String name){
39         this.name = name;
40     }
41     public Integer getOrder(){
42         return order;
43     }
44     public void setOrder(Integer order){
45         this.order = order;
46     }
47 }
原文地址:https://www.cnblogs.com/CoolRandy/p/4428706.html