LeetCode刷题-190715-字符串查找-两个int数除法

Implement strStr()

Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = “hello”, needle = “ll”
Output: 2
Example 2:
Input: haystack = “aaaaa”, needle = “bba”
Output: -1
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf().

实现 strStr() 函数,字符串查找函数,返回第一次找到指定匹配字符串的位置。题目中还有说明,如果 needle 为空时,返回 0。我个人的基本思路是:逐位遍历字符串,当发现和 needle[0] 相等的时候,开始和needle比较,是否匹配。

static public int StrStr(string haystack, string needle)
{
    int result = -1;
    if (needle.Length == 0) return 0;
    for (int i = 0; i < haystack.Length; i++)
    {
        if (haystack[i] == needle[0])
        {
            int j = 1;
            for (j = 1; j < needle.Length && i + j < haystack.Length; j++)
            {
                if (haystack[i + j] != needle[j]) break;
            }
            if (j == needle.Length)
            {
                result = i;
                break;
            }
        }
    }
    return result;
}

看官网的讨论里面,发现可以多个判断 if(needle.length>haystack) return -1;

Divide Two Integers

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero.
Example 1:
Input: dividend = 10, divisor = 3
Output: 3
Example 2:
Input: dividend = 7, divisor = -3
Output: -2
Both dividend and divisor will be 32-bit signed integers.
The divisor will never be 0.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

实现两个int型数据的除法,不能使用 乘、除和取模运算,解题的思路可以参考计算机加减乘除的计算原理,具体可以找计算机的相关书籍查看。支持32位的int,计算的时候需要考虑范围 [−231, 231 − 1]。
计算机中的计算是二进制,和常见的10进制不太一样,但是思路是一样。从高位开始,计算结果,只不过是二进制。二进制也有好处,每一位不是1就是0,而十进制是0到9。
写代码的时候发现,c# 的左移操作不会移动符号位。具体代码如下:

static public int Divide(int dividend, int divisor)
{
    int result = 0;
    if (divisor == 1) return dividend;
    else if (dividend == 0) return 0;
    else if (dividend == Int32.MinValue && divisor == -1) return Int32.MaxValue;//只有这种情况会越界
    bool positive = true;
    positive = !((dividend > 0) ^ (divisor > 0));
    if (dividend > 0) dividend = -dividend;
    if (divisor > 0) divisor = -divisor;
    int left_m_time = 0;
    for (; left_m_time < 31;)
    {
        if (divisor >= dividend - divisor)
        {
            divisor = divisor << 1;
            left_m_time++;
        }
        else
        {
            break;
        }
    }
    for (int i = 0; i <= left_m_time; i++)
    {
        result = result << 1;
        if ((dividend - divisor) <= 0)
        {
            result += 1;
            dividend = dividend - divisor;
        }
        if (i < left_m_time)
        {
            divisor = divisor >> 1;
        }
    }

    if (!positive) result = -result;
    return result;
}

因为 int32 的范围是: [−231, 231 − 1],最小值为 −231。本来通过绝对值来计算,有个小问题 −231 的绝对值会越界,在阅读网站中其他人的解法时发现了新的方案,把除数和被除数都转换为负数可以解决这个问题。
转换为负数以后第一个步骤是,找到商的最高位,具体步骤如图所示,以 dividend = 10, divisor = 2 为例:
在这里插入图片描述
然后逐位求商,如下图所示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值