matrix

自我监督刷题记录处4

情到浓时终转凉″ 提交于 2020-03-01 08:32:52
自我监督刷题记录处4 第4天。我订购了某位大神土著作者的python辅导账号,希望我能坚持看完所有他的文章。。之后。。。能写点诗句,摆脱python八股文的模式。> k <…非常抱歉现在只能写不好看的代码,我尽量努力🤐 Leetcode 189 Rotate Image (Medium Level) class Solution : def rotate ( self , matrix : List [ List [ int ] ] ) - > None : """ Do not return anything, modify matrix in-place instead. """ n = len ( matrix ) for i in range ( n ) : for j in range ( i , n ) : #先转置 matrix [ i ] [ j ] , matrix [ j ] [ i ] = matrix [ j ] [ i ] , matrix [ i ] [ j ] #后水平翻转 for i in matrix : i . reverse ( ) 2048也可以用同样的转置方法来做。 Leetcode 136 Single Number (Easy Level) class Solution : def singleNumber ( self , nums

leetcode-48--旋转图像

天涯浪子 提交于 2020-02-28 14:31:21
题目描述:   给定一个 n × n 的二维矩阵表示一个图像,将图像顺时针旋转 90 度。 说明:   你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], 原地旋转输入矩阵,使其变为: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ] 解题思路1:   按照以下旋转方式,先是淡黄色块,然后是浅蓝色块,依次替换位置 代码1: def rotate ( matrix ) : if matrix == [ ] or matrix [ 0 ] == [ ] : return 0 n = len ( matrix ) def get_rotate_xy ( i , j ) : return ( j , n - i - 1 ) for i in range ( n // 2 ) : for j in range ( i , n -

Vector Transformation with Matrix

廉价感情. 提交于 2020-02-28 08:49:07
问题 I'm working on a code to do a software skinner (bone/skin animation), and I'm at the "optimization" phase (the skinner works pretty well and skin a 4900 triangles mesh with 22 bones in 1.09 ms on a Core Duo 2 Ghz (notebook)). What I need to know is : 1) Can someone show me the way (maybe with pseudocode) to transform a float3 (array of 3 float) (representing a coordinate) against a float4x3 matrix? 2) Can someone show me the way (maybe with pseudocode) to transform a float3 (array of 3 float)

Vector Transformation with Matrix

好久不见. 提交于 2020-02-28 08:48:07
问题 I'm working on a code to do a software skinner (bone/skin animation), and I'm at the "optimization" phase (the skinner works pretty well and skin a 4900 triangles mesh with 22 bones in 1.09 ms on a Core Duo 2 Ghz (notebook)). What I need to know is : 1) Can someone show me the way (maybe with pseudocode) to transform a float3 (array of 3 float) (representing a coordinate) against a float4x3 matrix? 2) Can someone show me the way (maybe with pseudocode) to transform a float3 (array of 3 float)

Leetcode 搜索二维矩阵 II

让人想犯罪 __ 提交于 2020-02-28 05:52:08
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性: 每行的元素从左到右升序排列。 每列的元素从上到下升序排列。 示例: 现有矩阵 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] ] 给定 target = 5 ,返回 true 。 给定 target = 20 ,返回 false 。 思路:先二分target在哪一行,然后在对行进行二分找在哪一列 1 boolean searchMatrix(int[][] matrix, int target) { 2 if(matrix.length==0||matrix[0].length==0) 3 { 4 return false; 5 } 6 7 int l=0,r=matrix.length-1; 8 int mid=0; 9 while(l<=r) 10 { 11 mid=(l+r)/2; 12 if(target==matrix[mid][0]) 13 return true; 14 else if(target>matrix[mid][0]) 15 l=mid+1; 16 else r=mid-1

剑指Offer65:矩阵中的路径(Java)

点点圈 提交于 2020-02-27 12:21:24
参考“我去个地方啊”的牛客解答: https://www.nowcoder.com/questionTerminal/c61c6999eecb4b8f88a98f66b273a3cc?f=discussion(注释很详尽 ) 参考“白马长枪儒雅将”的博客: https://blog.csdn.net/u012429555/article/details/90476001(思路解释的很清晰 ) 思路分析: 建立与matrix对应的boolean数组flag,用来标记这个matrix这位置是否已经来过. 遍历matrix的元素判断与str[k]是否相等, 当不相等或者flag同位置的元素是true,或者越界了返回false。 相等时flag同位置元素设为true,然后递归matrix这个位置上下左右四个元素能否和str[k+1]相等。 k==str.length-1返回true 题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。例如,在下面的3x4的矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后

(Speed Challenge) Any faster method to calculate distance matrix between rows of two matrices, in terms of Euclidean distance?

浪子不回头ぞ 提交于 2020-02-27 09:25:08
问题 First of all, this is NOT the problem of calculating Euclidean distance between two matrices. Assuming I have two matrices x and y , e.g., set.seed(1) x <- matrix(rnorm(15), ncol=5) y <- matrix(rnorm(20), ncol=5) where > x [,1] [,2] [,3] [,4] [,5] [1,] -0.6264538 1.5952808 0.4874291 -0.3053884 -0.6212406 [2,] 0.1836433 0.3295078 0.7383247 1.5117812 -2.2146999 [3,] -0.8356286 -0.8204684 0.5757814 0.3898432 1.1249309 > y [,1] [,2] [,3] [,4] [,5] [1,] -0.04493361 0.59390132 -1.98935170 -1

剑指offer打卡|矩阵中的路径

南笙酒味 提交于 2020-02-26 18:43:41
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。 代码 回溯法,即暴力求解的升级版。回溯法非常适合多个步骤组成的问题,每个步骤有多个选项。且回溯适合用递归来写。 public class Solution { //输入的虽然是个数组,但可以根据后面的rows和cols参数来确定出一个矩阵 public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { int flag[]=new int[matrix.length]; for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ if(backtrack(matrix,rows,cols,i,j,str,0,flag)){ return true; } } } return false; } public boolean backtrack(char[]

【LeetCode】 73. 矩阵置零 空间O(1)

故事扮演 提交于 2020-02-25 02:31:58
题目 题目传送门: 传送门(点击此处) 题解 空间复杂度为O(mn)的解决方案很容易思考出来 空间复杂度为O(m+n)的解决方案也比较容易,创建两个数组长度分别为m和n,用来记录行或者列中出现过的0 空间复杂度为O(1)的解决方案就是在 2 的基础上,把这个数组放在了矩阵中来记录 我的思路 遍历找到第一个 0,并且把这一行和这一列作为记录有 0 的行和列 找到的第一个 0 在第一行,第二列,所以第一行和第二列,就作为记录的行和列 继续向下找,每找到 0 ,就在这一行中进行记录, 记录好了之后,先置零列 再置零行 代码 class Solution { public void setZeroes ( int [ ] [ ] matrix ) { int rows = matrix . length ; int cols = matrix [ 0 ] . length ; int recRow = - 1 ; int recCol = - 1 ; // 把有 0 的行和列都记录到 recRow 和 recCol 中 for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j < cols ; j ++ ) { if ( matrix [ i ] [ j ] == 0 ) { if ( recRow == - 1 && recCol

Converting OpenCV Mat to array (possibly NSArray)

柔情痞子 提交于 2020-02-24 12:38:08
问题 My C/C++ skills are rusty and the documentation for OpenCV is pretty obscure. Is there a way of getting a cv::Mat data attribute converted to array/NSArray? I want to serialize it to JSON, I know I can use the FileStorage utility to convert to YAML/XML, but that's not what I want to do. Mat -> NSArray -> JSON (I want to send intrinsic camera calibration values to the server) Thanks 回答1: you can use: double *d = intrinsics.ptr<double>(0); this will give you an array of 9 doubles. (it's just a