数组
剑指offer面试题3 数组中重复的数字__牛客网 (nowcoder.com)
剑指代码,称J1:
class Solution {
public:// Parameters:// numbers: an array of integers// length: the length of array numbers// duplication: (Output) the duplicated number in the array number// Return value: true if the input is valid, and there are some duplications in the array number// otherwise falsebool duplicate(int numbers[], int length, int* duplication) {if (numbers == nullptr){return false;}for (int i = 0; i < length; i++){if (numbers[i]<0 || numbers[i]>length - 1)return false;}for (int i = 0; i < length; ++i) {while (numbers[i] != i) {if (numbers[i] == numbers[numbers[i]]) {*duplication = numbers[i];return true;}int tmp = numbers[i];// numbers[i] = numbers[numbers[i]];// numbers[numbers[i]] = tmp;numbers[i] = numbers[tmp];numbers[tmp] = tmp;}}return false;}
};
通过案列的代码,称为T1:
class Solution {public:// Parameters:// numbers: an array of integers// length: the length of array numbers// duplication: (Output) the duplicated number in the array number// Return value: true if the input is valid, and there are some duplications in the array number// otherwise falsebool duplicate(int numbers[], int length, int* duplication) {if (numbers == nullptr || length == 0) return false;for (int i = 0 ; i < length - 1; i++) {for (int j = i + 1 ; j < length; j++) {if (numbers[i] == numbers[j]) {duplication[0] = numbers[i];return true;}}}return false;}};
这道题用J1不能完全通过案列,因为题目要返回第一个重复的值:
用J1不能完全通过测试用例,J1会返回全部重复的值,并且在最后一个测试用列中把第二个重复的值返回了。
剑指offer面试题4 240. 搜索二维矩阵 II - 力扣(LeetCode)
代码:
class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {if(!matrix.size() && !matrix[0].size()) return false;int j=matrix[0].size()-1;int i=0;while(i<matrix.size()&&j>=0 ){if(matrix[i][j] == target) return true;else if( matrix[i][j] < target) i++; //排除一行else if( matrix[i][j] > target) j--; //排除一列}return false;}};
字符串
剑指offer面试题5 替换空格_牛客题霸_牛客网 (nowcoder.com)
class Solution {
public:void replaceSpace(char *str,int length) {//养成好习惯,判空
if(str==nullptr&&length<=0)
{return ;
}//空格
int cut=0;//判断空格
for(int i=0;i<length;i++)
{if(str[i]==' ')++cut;
}if(!cut) return ;
//创建一个新指针
int newlength=length+2*cut;//遍历
for(int i=length;i>=0;i--)
{if(str[i]==' '){str[newlength--]='0';str[newlength--]='2';str[newlength--]='%';}else {str[newlength--]=str[i];}
}}
};
注意:
否则会越界报错,str能访问的只有自身那么多,只有当newlength减减到了str的范围内才可以通过str[newlength]去访问。
链表
剑指offer面试题6 从尾到头打印链表__牛客网 (nowcoder.com)
我本来的思路是找到尾节点,然后找到尾节点前面的节点p1,然后让p1的next指向p1前面的节点p2,以此类推,最后指向头节点,但是太麻烦了,没写完。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:vector<int> printListFromTailToHead(ListNode* head) {while(head->next!=nullptr){head++;}ListNode* last=head;while(head->next!=last){head++;}ListNode* prev=head;}
};
通过 “栈”:因为栈是后进先出的,所以我们把链表最后进去的数据最先放出来就是倒置链表了。
最后要求的是通过数组返回,我们可以写一个vector,通过vector返回,也可以通过数组返回:
class Solution {public:vector<int> printListFromTailToHead(ListNode* head) {vector<int> values;stack<int > node;ListNode* tail = head;while (tail != nullptr) {node.push(tail->val);tail=tail->next;}while (!node.empty()) {values.push_back(node.top());node.pop();}
return values;}
};
递归: