[字符串与数组] Q:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0. 题目:写一个算法,如果一个 MxN矩阵中某个元素为0,则将该元素所在的行.列都置为0. 解答: 方法一:错误的解法,思维误区.依次遍历二维数组(矩阵),遇到一个0,就将这个0所在的行和列全部置为0,咋看一下没问题,仔细一想不对啊,这样做后,很有可能操作完后,这个二维数…
[链表] Q:Write code to remove duplicates from an unsorted linked list      FOLLOW UP      How would you solve this problem if a temporary buffer is not allowed? 题目:编码实现从无序链表中移除重复项.          如果不能使用临时缓存,你怎么编码实现? 解答: 方法一:不使用额外的存储空间,直接在原始链表上进行操作.首先用一个指针指向链…
python 判断矩阵中每行非零个数的方法: # -*- coding: utf-8 -*- # @Time : 2018/5/17 15:05 # @Author : Sizer # @Site : # @File : test.py # @Software: PyCharm import time import numpy as np # data = np.array([ # [5.0, 3.0, 4.0, 4.0, 0.0], # [3.0, 1.0, 2.0, 3.0, 3.0], #…
//将3*5矩阵中第k列的元素左移到第0列,第k列以后的每列元素依次左移,原来左边的各列依次绕到右边. #include <stdio.h> #define M 3 #define N 5 void fun(int (*a)[N],int k) { int i,j,p,temp; /**********found**********/ ; p<= k; p++) ; i<M; i++)//总共三行,三次循环 { temp=a[i][];//每一行记录第一个元素 /*********…
/*===================================== 将n阶方阵左下半三角中的元素值置0. 0<n<10. ======================================*/ #include<stdio.h> int main() { int n; ][]; int i,j; scanf("%d",&n); ;i<n;i++) { ;j<n;j++) { a[i][j]=; } } ;i<n;i…
[字符串与数组] Q:Write a method to replace all spaces in a string with ‘%20’ 题目:写一个算法将一个字符串中的空格替换成%20 解答: 很直观的解法,首先统计出字符串中空格个数,然后分配新的内存空间,依次从头到尾复制原字符串到新字符串中,遇到空格,则复制%20这三个字符.还有没有其他更好点的方法呢?? char* replace(char* str){ int len=strlen(str); int spaceNum=0; int…
[链表] Q:Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node EXAMPLE Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothing is returned, but the new linked list looks like…
创建链表.往链表中插入数据.删除数据等操作,以单链表为例. 1.使用C语言创建一个链表: typedef struct nd{ int data; struct nd* next; } node; //初始化得到一个链表头节点 node* init(void){ node* head=(node*)malloc(sizeof(node)); if(head==NULL) return NULL; head->next=NULL; return head; } //在链表尾部插入数据 void i…
[字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i e , “waterbottle” is a rotation of “e…
[字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees Can you do this in place? 题目:假定一幅图像能用NxN的矩阵表示,每个像素是四字节.写一个算法将图像旋转90度,你能否在原地进行操作(也即不分配额外的存储空间)? 解答: 我们不知…