每天一刷20200621

问题
输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数。
输入描述:

输入一个整数(int类型)

输出描述:

这个数转换成2进制后,输出1的个数
思路
这是个简单题,其实就是模拟十进制转二进制的过程,对其中的1进行技术
还有一种思路是利用位运算t&(t-1)来计数
代码

#include<iostream>

using namespace std;

int main(){
    int input,count;
    while(cin >> input){
        count = 0;
        while(input){
            if(input%2) ++count;
            input /= 2;
        }
        cout<<count<<endl;
    }
    return 0;
}
链接:https://www.nowcoder.com/questionTerminal/440f16e490a0404786865e99c6ad91c9?f=discussion
来源:牛客网

#include<iostream>
using namespace std;
int main()
{
    int a;
    while(cin>>a){
        int count=0;
        while(a){
            a&=a-1;//判断二进制有多少个1
            count++;
        }
        cout<<count<<endl;
    }
    return 0;
}

问题

153. 寻找旋转排序数组中的最小值

假设按照升序排序的数组在预先未知的某个点上进行了旋转。

( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

请找出其中最小的元素。

你可以假设数组中不存在重复元素。

示例 1:

输入: [3,4,5,1,2]
输出: 1

示例 2:

输入: [4,5,6,7,0,1,2]
输出: 0

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
二分查找,但要找到那个拐点。
代码

class Solution {
public:
    int findMin(vector<int>& nums) {
        if(nums.size()==1) return nums[0];
        int l = 0,r = nums.size()-1;
        if(nums[r] > nums[0]) return nums[0];
        while(l <= r){
            int mid = l +(r-l)/2;
            if(nums[mid] > nums[mid+1])
                return nums[mid+1];//返回的是数组的元素
            if(nums[mid-1]>nums[mid])
                return nums[mid];
            if(nums[mid] > nums[0])
                l = mid +1;
            else
                r= mid -1;
        }
        return -1;
    }
};

PS:官方题解,注释的很详细,学习了

class Solution {
  public int findMin(int[] nums) {
    // If the list has just one element then return that element.
    if (nums.length == 1) {
      return nums[0];
    }

    // initializing left and right pointers.
    int left = 0, right = nums.length - 1;

    // if the last element is greater than the first element then there is no rotation.
    // e.g. 1 < 2 < 3 < 4 < 5 < 7. Already sorted array.
    // Hence the smallest element is first element. A[0]
    if (nums[right] > nums[0]) {
      return nums[0];
    }
    
    // Binary search way
    while (right >= left) {
      // Find the mid element
      int mid = left + (right - left) / 2;

      // if the mid element is greater than its next element then mid+1 element is the smallest
      // This point would be the point of change. From higher to lower value.
      if (nums[mid] > nums[mid + 1]) {
        return nums[mid + 1];
      }

      // if the mid element is lesser than its previous element then mid element is the smallest
      if (nums[mid - 1] > nums[mid]) {
        return nums[mid];
      }

      // if the mid elements value is greater than the 0th element this means
      // the least value is still somewhere to the right as we are still dealing with elements
      // greater than nums[0]
      if (nums[mid] > nums[0]) {
        left = mid + 1;
      } else {
        // if nums[0] is greater than the mid value then this means the smallest value is somewhere to
        // the left
        right = mid - 1;
      }
    }
    return -1;
  }
}


作者:LeetCode
链接:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/solution/xun-zhao-xuan-zhuan-pai-lie-shu-zu-zhong-de-zui-xi/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值