开发工具分享
  • 首页
  • 计算科学
  • 文化旅游
  • 项目和网站
    • OSSEZ 计算技术
    • USRealEstate 社区
    • 地区文化
    • CWIKI.US
    • BUG.OSSEZ.COM
    • RSS.OSSEZ.COM
CWIKIUS.CN
一个有独立思考和温度的清新站
Computer Science

Robot Movement(机器人移动)

中文标题【机器人移动】 这个题目是 Kayak 发布的代码挑战题目。 我认为题目本身描述的不是十分清楚,方法需要返回结果,但是结果没有说明是机器人最后的坐标位置,还是最后的坐标位置距离原点的距离。同时,机器人的初始化方向等都没有十分明确的定义。 根据测试数据,机器人应该是从下往上(初始化的方向)。因为如果初始化的方向不确定的话,最后的坐标值可能会不一致。 我这里假设程序应该返回的是机器人的最后坐标位置,并且初始化的位置为 (0,0),方向为从下往上。 英文描述 A robot lands on Mars, which happens to be a cartesian grid; assuming that we hand the robot these instructions, such as LFFFRFFFRRFFF, where "L" is a "turn 90 degrees left", "R" is a "turn 90 degrees right", and "F" is "go forward one space。 please write control code for the robot such that it ends up at the appropriate-and-correct destination, and include unit tests. Here is an example output with command "FF": [0, 2] 中文描述 这里我不按照原文一字一字的翻译,但是尽量按照题目的要求把题目解释清楚。 这里题目的要求是,假设一个机器人降落到火星上了,我们现在需要给机器人发布指令。指令包括有 L,R,F 3 个。 L 表示的意思是机器人向左转 90 度,R 的意思表示的是机器人向右转 90 度,F 表示的是机器人向前移动一个坐标单位。 题目的表达并是是十分明确也清晰,比如说 LFFFRFFFRRFFF 应该返回是什么没有说清楚。假设 指令 FF ,返回的结果是 [0, 2],我默认的是程序需要返回机器人最后的坐标位置,0 表示的是 X 坐标,2 表示的是 Y 坐标。 思路和点评 这个问题的思路,首先你需要明白几个点。如果需要进行坐标计算的话,请注意 L 和 F 是不会改变当前机器人的坐标位置的。 只有 F 的操作才会改变机器人的位置。考虑设置一个坐标系,那么这里需要存储 2 个信息,第一个信息为 F 移动时候机器人的位置,另外就是 L 和 F 对机器人方向的控制了。 所以你需要在程序中初始化一个二维数组,这个数组用于存储 F 操作时候的坐标变化。 同时你还需要存储一个 dir 变量,通常这个变量为每一次 L 和 R 操作的时候方向的变化。 F 存储的路径数组为:int[][] move = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; 通过这个数组,你就明白为什么我在这个 WIKI 页面前面说的,初始化方向很重要,请参考下面的图(因为不太好用计算机画图,我们用手画了一个图)。 在这个图中比较明确的说明了,我们定义的初始化方向为从下往上,Dir 等于 0 。在 Dir 等于 0 的时候坐标数组为 int[][] move = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; 按照顺时针的方向。 在图中,Dir 有 4 个反向,按照顺时针方向,分别为上,右,下,左,那么方向对应的值就分别为 0,1,2,3 。 当方向为 L 的时候,需要将方向值减 1 ,当方向为 R 的时候,需要将值 +1。 这里有个问题为循环,比如说,方向值为 RR,,dir 的值应该为 2。 如果方向为 RRRRRR,那么值应该也为 2。所以在算法中我们使用了  dir = (dir + 4) % 4;, 对方向进行取 余数。你可以看到 当你旋转 RRRRRR 后,dir 的值还是为 2。 针一次转向 + 移动的操作,不管你转向多少次,调整的方方向无非就是调整 X 或者 Y 的坐标系,在下一次移动的时候应该是 + 还是 - 所以到这里方法就相对简单了。一次移动的时候,都会改变 X 和 Y 坐标的值,前提是你是希望怎么加减而已。 源代码 源代码和有关代码的更新请访问 GitHub: https://github.com/cwiki-us/codebank-algorithm/blob/master/src/main/java/com/ossez/codebank/interview/KayakRobotMovement.java 测试类请参考: https://github.com/cwiki-us/codebank-algorithm/blob/master/src/test/java/com/ossez/codebank/interview/tests/KayakTest.java 代码思路请参考: package com.ossez.codebank.interview; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * https://www.cwiki.us/display/ITCLASSIFICATION/Robot+Movement * * @author YuCheng *…

2018年12月25日 0Comments 1019Browse 0Like Read more
Computer Science

Binary Gap(二进制空白)

中文标题【二进制空白】 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps. Write a function: class Solution { public int solution(int N); } that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap. For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..2,147,483,647]. 中文描述 这里我不按照原文一字一字的翻译,但是尽量按照题目的要求把题目解释清楚。 这里题目的要求是,将 N 为一个整数类型的数据,转换为一个 2 进制的字符串,然后在返回的字符串中返回最大的 0 的间隔数字。 例如 529 转换为 2 进制的字符串为:1000010001,在这里,将会存在以 1 为分割的字符串  0000 和 000,这 2 个字符串的长度分别为 4 和 3。 我们的算法需要返回的值诶 4。 思路和点评 这个题目的思路其实比较简单,你需要首先将 N 这个整数,转换为 0 和 1 的字符串。然后在转换成功的字符串中返回以 1 分分割的 0 的长度。 这里可能需要考虑下面的几种情况。 情况 结果 11 这个情况应该返回的长度为 0 10 这个情况因为没有被 1 这个字符串封闭,因此应该返回长度为 0 传统的思路应该是采取字符串分割的方式,进行遍历后获得结果。 我们在这里采取一种相对不是非常常规的方式,例如在 10000010001 字符串中插入 #,将字符串变为 #1#00000#1#000#1#。…

2018年12月23日 0Comments 1464Browse 0Like Read more
文化旅游

USVisaTrack

USVisaTrack 为我们开发的一个用于跟踪签证状态的小工具。最开始的主要用途是因为我自己的签证被 Check 了,而且深陷其中。能够理解被 Check 的人的心情。既然签证被 Check 了,也暂时没有办法走,于是就各种收集相关的信息,发现有时候还是有点技巧和判断可言的。 于是我们就开发了这个小工具,希望能够帮助到深陷其中的的同学和朋友。 如果你有什么问题,建议或者希望扩展其他的一些功能,请发送电子邮件到yuchenghu@ossez.com,谢谢。 小工具的界面如下,你可以在界面中查看签证被 Check 的状态。 关注我们的公众号:CWIKIUS,更多精彩等你来。

2018年12月23日 0Comments 1197Browse 0Like Read more
Computer Science

潇湘猪手

潇湘猪手是一道湖南名菜,属于湘菜系。肉酥烂,色鲜艳,汁浓味厚,蹄中的胶原蛋白是人体皮肤的主要成分之一,也有促进女性激素合成的作用,有美容养颜之功效。 猪手,又叫猪脚、猪蹄,含有丰富的胶原蛋白质,脂肪含量也比肥肉低,并且不含胆固醇,在对老年人衰老原因的研究中发现,人体中胶原蛋白质缺乏,是人衰老的一个重要因素。它能防治皮肤干瘪起皱、增强皮肤弹性和韧性,对延缓衰老和促进儿童生长发育都具有物殊意义。为此,人们把猪蹄称为“美容食品”和“类似于熊掌的美味佳肴”。 猪蹄中含有大量的胶原蛋白质,是人体皮肤的主要成分之一,它在烹调过程中可转化成明胶。明胶具有网状空间结构,它能结合许多水,增强细胞生理代谢,有效地改善机体生理功能和皮肤组织细胞的储水功能,使细胞得到滋润,保持湿润状态,防止皮肤过早褶皱,延缓皮肤的衰老过程。猪蹄对于经常性的四肢疲乏、腿部抽筋、麻木、消化道出血、失血性休克胶缺血性脑患者有一定辅助疗效。也适用于大手术后及重病恢复期间的老人食用。有助于青少年生长发育和减缓中老年妇女骨质疏松的速度。传统医学认为,猪蹄有壮腰补膝和通乳之功,可用于肾虚所致的腰膝酸软和产妇产后缺少乳汁之症。 辣椒为茄属植物,也称辣子。含有丰富的维生素C,可以控制心脏病及冠状动脉硬化,降低胆固醇;含有较多抗氧化物质,可预防癌症及其他慢性疾病;可以使呼吸道畅通,用以治疗咳嗽、感冒。吃饭不香、饭量减少时,在菜里放上一些辣椒就能改善食欲,增加饭量。 现代医学研究辣椒素是辣椒中最重要成分,它会刺激胃肠蠕动,促进消化液分泌,抑制肠道内异常细菌的发酵。消除肠内的积气,所吃辣椒可增加食欲,促进消化功能。辣椒素具有强烈的促进血液循环的作用,使心跳加快,血管扩张,缓解手足发凉、怕冷、血管性头痛等症状。这些功能中医称为温中散寒辛温解表,医治风寒感冒。它含有一种特殊物质,能加速新陈代谢以达到燃烧体内脂肪的效果显著,从而起到减肥作用。这种物质还可以促进荷尔蒙分泌,对皮肤有很好的美容保健作用,是女性的“补品”。

2018年12月22日 0Comments 1060Browse 0Like Read more
Computer Science

私房小厨菜单(2018-12)

新增加湖南肉丝米粉仅售 $8.99 V.201812

2018年12月22日 0Comments 1133Browse 0Like Read more
Computer Science

[LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3], [9,20], [15,7] ] 挑战 挑战1:只使用一个队列去实现它 挑战2:用BFS算法来做 代码 GitHub 的源代码,请访问下面的链接: https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0069LevelOrderTest.java   package com.ossez.lang.tutorial.tests.lintcode; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ossez.lang.tutorial.models.TreeNode; /** * <p> * 69 * <ul> * <li>@see <a href= * "https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Tree+Level+Order+Traversal">https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Tree+Level+Order+Traversal</a> * <li>@see<a href= * "https://www.lintcode.com/problem/binary-tree-level-order-traversal">https://www.lintcode.com/problem/binary-tree-level-order-traversal</a> * </ul> * </p> * * @author YuCheng * */ public class LintCode0069LevelOrderTest { private final static Logger logger = LoggerFactory.getLogger(LintCode0069LevelOrderTest.class); /** * */ @Test public void testMain() { logger.debug("BEGIN"); String data = "{3,9,20,#,#,15,7}"; TreeNode tn = deserialize(data); System.out.println(levelOrder(tn)); } /** * Deserialize from array to tree * * @param data * @return */ private TreeNode deserialize(String data) { // NULL CHECK if (data.equals("{}")) { return null; } ArrayList<TreeNode> treeList = new ArrayList<TreeNode>(); data = data.replace("{", ""); data = data.replace("}", ""); String[] vals = data.split(","); // INSERT ROOT TreeNode root = new TreeNode(Integer.parseInt(vals[0])); treeList.add(root); int index = 0; boolean isLeftChild = true; for (int i = 1; i < vals.length; i++) { if (!vals[i].equals("#")) { TreeNode node = new TreeNode(Integer.parseInt(vals[i])); if (isLeftChild) { treeList.get(index).left = node; } else { treeList.get(index).right = node; } treeList.add(node); } // LEVEL if (!isLeftChild) { index++; } // MOVE TO RIGHT OR NEXT LEVEL isLeftChild = !isLeftChild; } return root; } private List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); List<List<Integer>> rs = new ArrayList<List<Integer>>(); // NULL CHECK if (root == null) { return rs; } queue.offer(root); while (!queue.isEmpty()) { int length = queue.size(); List<Integer> list = new ArrayList<Integer>(); for (int…

2018年12月16日 0Comments 854Browse 0Like Read more
Computer Science

[LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)

描述 设计一个算法,并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”。 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构。 对二进制树进行反序列化或序列化的方式没有限制,LintCode将您的serialize输出作为deserialize的输入,它不会检查序列化的结果。 样例 给出一个测试数据样例, 二叉树{3,9,20,#,#,15,7},表示如下的树结构: 3 / \ 9 20 / \ 15 7 我们的数据是进行 BFS 遍历得到的。当你测试结果 wrong answer时,你可以作为输入调试你的代码。 你可以采用其他的方法进行序列化和反序列化。 代码 GitHub 的源代码,请访问下面的链接: https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0007SerializeAndDeserialize.java package com.ossez.lang.tutorial.tests.lintcode; import java.util.ArrayList; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ossez.lang.tutorial.models.TreeNode; /** * <p> * 7 * <ul> * <li>@see <a href= * "https://www.cwiki.us/display/ITCLASSIFICATION/Serialize+and+Deserialize+Binary+Tree">https://www.cwiki.us/display/ITCLASSIFICATION/Serialize+and+Deserialize+Binary+Tree</a> * <li>@see<a href= * "https://www.lintcode.com/problem/serialize-and-deserialize-binary-tree">https://www.lintcode.com/problem/serialize-and-deserialize-binary-tree</a> * </ul> * </p> * * @author YuCheng * */ public class LintCode0007SerializeAndDeserialize { private final static Logger logger = LoggerFactory.getLogger(LintCode0007SerializeAndDeserialize.class); /** * */ @Test public void testMain() { logger.debug("BEGIN"); String data = "{3,9,20,#,#,15,7}"; System.out.println(serialize(deserialize(data))); } /** * Deserialize from array to tree * * @param data * @return */ private TreeNode deserialize(String data) { // NULL CHECK if (data.equals("{}")) { return null; } ArrayList<TreeNode> treeList = new ArrayList<TreeNode>(); data = data.replace("{", ""); data = data.replace("}", ""); String[] vals = data.split(","); // INSERT ROOT TreeNode root = new TreeNode(Integer.parseInt(vals[0])); treeList.add(root); int index = 0; boolean isLeftChild = true; for (int i = 1; i < vals.length; i++) { if (!vals[i].equals("#")) { TreeNode node = new TreeNode(Integer.parseInt(vals[i])); if (isLeftChild) { treeList.get(index).left = node; } else { treeList.get(index).right = node; } treeList.add(node); } // LEVEL if (!isLeftChild) { index++; } // MOVE TO RIGHT OR NEXT LEVEL isLeftChild = !isLeftChild; } return root; } /** * * @param root * @return */ public String serialize(TreeNode root) { // write your code here if (root == null) { return "{}"; } ArrayList<TreeNode> queue = new ArrayList<TreeNode>(); queue.add(root); for (int i = 0; i < queue.size(); i++) { TreeNode node = queue.get(i); if (node == null) { continue;…

2018年12月16日 0Comments 1132Browse 0Like Read more
Computer Science

[LintCode] Number of Islands(岛屿个数)

描述 给一个01矩阵,求不同的岛屿的个数。 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ] 中有 3 个岛。   代码 GitHub 的源代码,请访问下面的链接: https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0433NumIslandsTest.java package com.ossez.lang.tutorial.tests.lintcode; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * <p> * 433 * <ul> * <li>@see <a href= * "https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands">https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands</a> * <li>@see<a href="https://www.lintcode.com/problem/number-of-islands/">https://www.lintcode.com/problem/number-of-islands/</a> * </ul> * </p> * * @author YuCheng * */ public class LintCode0433NumIslandsTest { private final static Logger logger = LoggerFactory.getLogger(LintCode0433NumIslandsTest.class); /** * */ @Test public void testMain() { logger.debug("BEGIN"); // INIT GRID boolean[][] grid = { { true, true, false, false, false }, { false, true, false, false, true }, { false, false, false, true, true }, { false, false, false, false, false }, { false, false, false, false, true } }; // NULL CHECK if (grid.length == 0 || grid[0].length == 0) { System.out.println("NULL"); // return 0; } // GET SIZE int n = grid.length; int m = grid[0].length; // ARRAY FOR VISITED LOG boolean[][] visited = new boolean[n][m]; int count = 0; // LOOP FOR GRID for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] && !visited[i][j]) { numIslandsDFS(grid, visited, i, j); count++; } } } System.out.println(count); } /** * * @param grid * @param visited * @param x * @param y */ public void numIslandsDFS(boolean[][] grid, boolean[][] visited, int x, int y) { if (x < 0 || x >= grid.length) { return; } if (y < 0 || y >=…

2018年12月16日 0Comments 925Browse 0Like Read more
Computer Science

[LintCode] Linked List Cycle(带环链表)

描述 给定一个链表,判断它是否有环。 样例 给出 -21->10->4->5, tail connects to node index 1,返回 true。 这里解释下,题目的意思,在英文原题中,tail connects to node index 1 表示的是节点 5 还要链接回索引号 为 1 的节点。 一个典型的带环链表如下: 挑战 不要使用额外的空间 代码 GitHub 的源代码,请访问下面的链接: https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0102HasCycleTest.java package com.ossez.lang.tutorial.tests.lintcode; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ossez.lang.tutorial.models.ListNode; /** * <p> * 102 * <ul> * <li>@see <a href= * "https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle">https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle</a> * <li>@see<a href= "https://www.lintcode.com/problem/linked-list-cycle/">https://www.lintcode.com/problem/linked-list-cycle/</a> * </ul> * </p> * * @author YuCheng * */ public class LintCode0102HasCycleTest { private final static Logger logger = LoggerFactory.getLogger(LintCode0102HasCycleTest.class); /** * */ @Test public void testMain() { logger.debug("BEGIN"); // INIT LINKED LIST ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); head.next.next.next = new ListNode(4); // CREATE A LOOP head.next.next.next.next = head.next.next.next; boolean retResult = false; // LIKED LIST MAY NULL: if (!(head == null || head.next == null)) { ListNode s = head; ListNode f = head.next; while (f.next != null && f.next.next != null) { s = s.next; f = f.next.next; if (f == s) { retResult = true; break; } } } System.out.println(retResult); } }     点评 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)和O(1)。 使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。 在计算机科学中,链表作为一种基础的数据结构可以用来生成其它类型的数据结构。链表通常由一连串节点组成,每个节点包含任意的实例数据(data fields)和一或两个用来指向上一个/或下一个节点的位置的链接(”links”)。链表最明显的好处就是,常规数组排列关联项目的方式可能不同于这些数据项目在记忆体或磁盘上顺序,数据的访问往往要在不同的排列顺序中转换。而链表是一种自我指示数据类型,因为它包含指向另一个相同类型的数据的指针(链接)。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。链表有很多种不同的类型:单向链表,双向链表以及循环链表。 要判断一个链表中是否有循环,可以借助额外的存储空间,将链表插入到 HashSet 中。创建一个以节点ID为键的HashSet集合,用来存储曾经遍历过的节点。然后同样是从头节点开始,依次遍历单链表的每一个节点。每遍历到一个新节点,就用新节点和HashSet集合当中存储的节点作比较,如果发现HashSet当中存在相同节点ID,则说明链表有环,如果HashSet当中不存在相同的节点ID,就把这个新节点ID存入HashSet,之后进入下一节点,继续重复刚才的操作。 这个方法在流程上和方法一类似,本质的区别是使用了HashSet作为额外的缓存。 假设从链表头节点到入环点的距离是D,链表的环长是S。而每一次HashSet查找元素的时间复杂度是O(1), 所以总体的时间复杂度是1*(D+S)=D+S,可以简单理解为O(N)。而算法的空间复杂度还是D+S-1,可以简单地理解成O(N)。 也可以采用指针的方式。 首先创建两个指针1和2(在java里就是两个对象引用),同时指向这个链表的头节点。然后开始一个大循环,在循环体中,让指针1每次向下移动一个节点,让指针2每次向下移动两个节点,然后比较两个指针指向的节点是否相同。如果相同,则判断出链表有环,如果不同,则继续下一次循环。 例如链表A->B->C->D->B->C->D,两个指针最初都指向节点A,进入第一轮循环,指针1移动到了节点B,指针2移动到了C。第二轮循环,指针1移动到了节点C,指针2移动到了节点B。第三轮循环,指针1移动到了节点D,指针2移动到了节点D,此时两指针指向同一节点,判断出链表有环。 此方法也可以用一个更生动的例子来形容:在一个环形跑道上,两个运动员在同一地点起跑,一个运动员速度快,一个运动员速度慢。当两人跑了一段时间,速度快的运动员必然会从速度慢的运动员身后再次追上并超过,原因很简单,因为跑道是环形的。 https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle

2018年12月15日 0Comments 1158Browse 0Like Read more
Computer Science

[LintCode] Dot Product(点积)

描述 给出两个数组,求它们的点积。(Wikipedia)   如果没有点积则返回-1 样例 Input:A = [1,1,1] B = [2,2,2] Output:6 代码 请单击下面链接在 GitHub 上查看最新的源代码: https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode1480DotProductTest.java package com.ossez.lang.tutorial.tests.lintcode; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * <p> * 1480 * <ul> * <li>@see * <a href= "https://www.cwiki.us/display/ITCLASSIFICATION/Dot+Product">https://www.cwiki.us/display/ITCLASSIFICATION/Dot+Product</a> * <li>@see<a href= "https://www.lintcode.com/problem/dot-product/">https://www.lintcode.com/problem/dot-product/</a> * </ul> * </p> * * @author YuCheng * */ public class LintCode1480DotProductTest { private final static Logger logger = LoggerFactory.getLogger(LintCode1480DotProductTest.class); /** * */ @Test public void testMain() { logger.debug("BEGIN"); int[] A = { 1, 1, -1 }; int[] B = { 2147483647, 1, 3 }; int retStatus = 0; // LENGTH CHECK if (A.length == 0 || B.length == 0 || A.length != B.length) retStatus = -1; // ADDED if (retStatus != -1) { for (int i = 0; i < A.length; i++) { retStatus = retStatus + A[i] * B[i]; } } System.out.println(retStatus); } }   点评 这个问题的关键是要了解点积是如何进行计算的。 对输入的数组还需要进行 NULL 的校验和计算。 摘录点积的计算方法如下: 向量是由n个实数组成的一个n行1列(n*1)或一个1行n列(1*n)的有序数组; 向量的点乘,也叫向量的内积、数量积,对两个向量执行点乘运算,就是对这两个向量对应位一一相乘之后求和的操作,点乘的结果是一个标量。 点乘公式 对于向量 a 和向量 b: a和b的点积公式为:     要求一维向量 a 和向量 b 的行列数相同。 因为要求向量 a 和 b 的行列数相同,因此在程序中需要先进行判断才可以。

2018年12月14日 0Comments 960Browse 0Like Read more
12345…7
Archives
  • May 2026
  • April 2026
  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
Categories
  • Computer Science (2,364)
    • Confluence (663)
    • Gradle (12)
  • U.S. (502)
  • 文化旅游 (146)

COPYRIGHT © 2020 CWIKIUS. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

湘ICP备2020018253号-1