1232:缀点成线

This commit is contained in:
轩辕龙儿 2022-04-20 15:52:51 +08:00
parent cda50449c9
commit 2b01957cb2
3 changed files with 102 additions and 2 deletions

View File

@ -30,7 +30,7 @@ public class TwoArray {
int yIndex = 0;
String iStr = "";
for (char ch : str.toCharArray()) {
if (Character.isDigit(ch)) {
if (Character.isDigit(ch) || ch == '-') {
iStr += ch;
} else {
if (!iStr.equals("")) {
@ -51,7 +51,7 @@ public class TwoArray {
String[] strings = str.split("],\\[");
arr = new int[strings.length][];
for (int i = 0; i < strings.length; i++) {
if("".equals(strings[i])){
if ("".equals(strings[i])) {
arr[i] = new int[0];
continue;
}

View File

@ -0,0 +1,67 @@
//<p>给定一个数组&nbsp;<code>coordinates</code>&nbsp;其中&nbsp;<code>coordinates[i] = [x, y]</code>&nbsp;<meta charset="UTF-8" />&nbsp;<code>[x, y]</code>&nbsp;表示横坐标为 <code>x</code>纵坐标为 <code>y</code>&nbsp;的点请你来判断这些点是否在该坐标系中属于同一条直线上</p>
//
//<p>&nbsp;</p>
//
//<p><strong>示例 1</strong></p>
//
//<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/19/untitled-diagram-2.jpg" /></p>
//
//<pre>
//<strong>输入</strong>coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
//<strong>输出</strong>true
//</pre>
//
//<p><strong>示例 2</strong></p>
//
//<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/19/untitled-diagram-1.jpg" /></strong></p>
//
//<pre>
//<strong>输入</strong>coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
//<strong>输出</strong>false
//</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示</strong></p>
//
//<ul>
// <li><code>2 &lt;=&nbsp;coordinates.length &lt;= 1000</code></li>
// <li><code>coordinates[i].length == 2</code></li>
// <li><code>-10^4 &lt;=&nbsp;coordinates[i][0],&nbsp;coordinates[i][1] &lt;= 10^4</code></li>
// <li><code>coordinates</code>&nbsp;中不含重复的点</li>
//</ul>
//<div><div>Related Topics</div><div><li>几何</li><li>数组</li><li>数学</li></div></div><br><div><li>👍 112</li><li>👎 0</li></div>
package leetcode.editor.cn;
import com.code.leet.entiy.TwoArray;
// 1232:缀点成线
public class CheckIfItIsAStraightLine {
public static void main(String[] args) {
Solution solution = new CheckIfItIsAStraightLine().new Solution();
// TO TEST
TwoArray twoArray = new TwoArray("[[0,1],[1,3],[-4,-7],[5,11]]",true);
System.out.println(solution.checkStraightLine(twoArray.getArr()));
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
if (coordinates.length == 2) {
return true;
}
int x = coordinates[1][0] - coordinates[0][0];
int y = coordinates[1][1] - coordinates[0][1];
for (int i = 2; i < coordinates.length; i++) {
int tx = coordinates[i][0] - coordinates[i - 1][0];
int ty = coordinates[i][1] - coordinates[i - 1][1];
if (x * ty != y * tx) {
return false;
}
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,33 @@
<p>给定一个数组&nbsp;<code>coordinates</code>&nbsp;,其中&nbsp;<code>coordinates[i] = [x, y]</code>&nbsp;<meta charset="UTF-8" />&nbsp;<code>[x, y]</code>&nbsp;表示横坐标为 <code>x</code>、纵坐标为 <code>y</code>&nbsp;的点。请你来判断,这些点是否在该坐标系中属于同一条直线上。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/19/untitled-diagram-2.jpg" /></p>
<pre>
<strong>输入:</strong>coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
<strong>输出:</strong>true
</pre>
<p><strong>示例 2</strong></p>
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/19/untitled-diagram-1.jpg" /></strong></p>
<pre>
<strong>输入:</strong>coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
<strong>输出:</strong>false
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;=&nbsp;coordinates.length &lt;= 1000</code></li>
<li><code>coordinates[i].length == 2</code></li>
<li><code>-10^4 &lt;=&nbsp;coordinates[i][0],&nbsp;coordinates[i][1] &lt;= 10^4</code></li>
<li><code>coordinates</code>&nbsp;中不含重复的点</li>
</ul>
<div><div>Related Topics</div><div><li>几何</li><li>数组</li><li>数学</li></div></div><br><div><li>👍 112</li><li>👎 0</li></div>