剑指offer(一)

数组中的重复数字

题目描述

在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个数字重复几次。请找出数组中任意一个重复的数字。例如,如果输入长度为 7 的数组 {2, 3, 1, 0, 2, 5},那么对应的输出是第一个重复的数字 2。

要求复杂度为 O(N) + O(1),也就是时间复杂度 O(N),空间复杂度 O(1)。因此不能使用排序的方法,也不能使用额外的标记数组。牛客网讨论区这一题的首票答案使用 nums[i] + length 来将元素标记,这么做会有加法溢出问题。

解题思路

这种数组元素在 [0, n-1] 范围内的问题,可以将值为 i 的元素放到第 i 个位置上。

以 (2, 3, 1, 0, 2, 5) 为例:

position-0 : (2,3,1,0,2,5) // 2 <-> 1
             (1,3,2,0,2,5) // 1 <-> 3
             (3,1,2,0,2,5) // 3 <-> 0
             (0,1,2,3,2,5) // already in position
position-1 : (0,1,2,3,2,5) // already in position
position-2 : (0,1,2,3,2,5) // already in position
position-3 : (0,1,2,3,2,5) // already in position
position-4 : (0,1,2,3,2,5) // nums[i] == nums[nums[i]], exit

c++代码

#include <iostream>
using namespace std;
bool duplicate(int nums[], int length, int duplication[])
{
    if(nums==NULL||length<=0)
        return false;
    for(int i = 0;i<length;i++)
    {
        while(nums[i]!=i)
        {
            if(nums[i] == nums[nums[i]]){
                duplication[0] = nums[i];
                return true;
            }
            swap(nums[i],nums[nums[i]]);
            //这边不能在java里面那样写。
        }
    }
    return false;
}

int main() {
    int res[] = {1};
    int arr[] = {2,3,1,0,2,5};
    if(duplicate(arr,6,res))
        cout<<res[0];
    return 0;
}

Java

public class Duplication {
    private void swap(int[] nums, int i, int j) {
        int t = nums[i]; nums[i] = nums[j]; nums[j] = t;
    }
    public boolean duplicate(int[] nums, int length, int[] duplication)
    {
        if (nums == null || length <= 0)
            return false;
        for (int i = 0; i < length; i++) {
            while (nums[i] != i) {
                if (nums[i] == nums[nums[i]]) {
                    duplication[0] = nums[i];
                    return true;
                }
                swap(nums,i,nums[i]);

            }
        }
        return false;
    }
    public static void main(String[] args)
    {
        int [] arr = {2,3,1,0,2,5};
        int [] res = {0};
        Duplication a = new Duplication();
        if(a.duplicate(arr,6,res))
            System.out.println(res[0]);
    }
}

二维数组中的查找

题目描述

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

Consider the following matrix:
[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]

Given target = 5, return true.
Given target = 20, return false.

解题思路

从右上角开始查找。因为矩阵中的一个数,它左边的数都比它小,下边的数都比它大。因此,从右上角开始查找,就可以根据 target 和当前元素的大小关系来缩小查找区间。

复杂度:O(M + N) + O(1)

C++

#include <iostream>
#include <vector>
using namespace std;
bool findT(int target, vector<vector<int>> array)
{
    if(array.size()!= 0)
    {
        int row = 0;
        int col = array[0].size()-1;
        while(row < array.size() && col >= 0)
        {
            if(array[row][col] == target)
                return true;
            else if(array[row][col] > target)
                --col;
            else
                ++row;
        }

    }
    return false;

}

int main() {
    vector<vector<int>> array ={{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
    if(findT(12,array))
        cout<<"find"<<endl;
    return 0;
}

Java

public boolean Find(int target, int[][] matrix) {
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
        return false;
    int rows = matrix.length, cols = matrix[0].length;
    int r = 0, c = cols - 1; // 从右上角开始
    while (r <= rows - 1 && c >= 0) {
        if (target == matrix[r][c])
            return true;
        else if (target > matrix[r][c])
            r++;
        else 
            c--;
    }
    return false;
}

替换空格

题目描述

请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为 We Are Happy. 则经过替换之后的字符串为 We%20Are%20Happy。

解题思路

在字符串尾部填充任意字符,使得字符串的长度等于字符串替换之后的长度。因为一个空格要替换成三个字符(%20),因此当遍历到一个空格时,需要在尾部填充两个任意字符。

令 P1 指向字符串原来的末尾位置,P2 指向字符串现在的末尾位置。P1 和 P2从后向前遍历,当 P1 遍历到一个空格时,就需要令 P2 指向的位置依次填充 02%(注意是逆序的),否则就填充上 P1 指向字符的值。

从后向前遍是为了在改变 P2 所指向的内容时,不会影响到 P1 遍历原来字符串的内容。

复杂度:O(N) + O(1)

C++

class Solution {
public:
    void replaceSpace(char *str,int length) {
         if(str==NULL)
             return ;
         int CountOfBlanks=0;
         int Originallength=0;
         for(int i=0;str[i]!='\0';i++)
             {
             Originallength++;
             if(str[i]==' ')
                 ++CountOfBlanks;
         }
         int len =Originallength+2*CountOfBlanks;
         if(len+1>length)
             return ;

         char*pStr1=str+Originallength;//复制结束符‘\0’
         char*pStr2=str+len;
        while(pStr1<pStr2)
            {
            if(*pStr1==' ')
                {
                *pStr2--='0';
                *pStr2--='2';
                *pStr2--='%';    
            }
            else
             {
                 *pStr2--=*pStr1;
            }
            --pStr1;
        }
    }
};

Python

class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        # write code here
        s = list(s)
        count=len(s)
        for i in range(0,count):
            if s[i]==' ':
                s[i]='%20'
        return ''.join(s)

Java

public String replaceSpace(StringBuffer str) {
    int oldLen = str.length();
    for (int i = 0; i < oldLen; i++)
        if (str.charAt(i) == ' ')
            str.append("  ");

    int P1 = oldLen - 1, P2 = str.length() - 1;
    while (P1 >= 0 && P2 > P1) {
        char c = str.charAt(P1--);
        if (c == ' ') {
            str.setCharAt(P2--, '0');
            str.setCharAt(P2--, '2');
            str.setCharAt(P2--, '%');
        } else {
            str.setCharAt(P2--, c);
        }
    }
    return str.toString();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值