在接受采访时我问过你.你给我的矩阵填充了"1"和"0"."1"表示你可以使用单元格,"0"表示单元格被阻挡.你可以向4个方向移动左,右,上,下每次你向上或向下移动它都会花费你1美元.向左或向右移动不会增加成本.所以你必须编写一个代码来检查是否可以达到目的地索引( x,y)从(0,0)开始.如果是,则打印最低成本,否则打印"-1".
我无法计算成本.这是我的代码
public class Test {
/**
* @param args the command line arguments
*/
static int dest_x = 0;
static int dest_y = 0;
static int cost = 0;
static int m = 0;
static int n = 0;
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
Test rat = new Test();
int maze[][] = {
{1, 1, 1, 1},
{1, 1, 1, 0},
{1, 1, 1, 1},
{0, …Run Code Online (Sandbox Code Playgroud) 我有以下代码来计算字符串中最长的回文子字符串.在线评判接受O(n ^ 2)解决方案,但我不知道为什么它不接受我的解决方案,虽然看起来我的算法复杂度为O(n ^ 2).
class Ideone {
public static void main(String args[]) {
Ideone ob = new Ideone();
String s = "sds";
System.out.println(ob.longestPalindrome(s));
}
public String longestPalindrome(String s) {
int maxlength = 1;
String ps = s.charAt(0) + "";
if (s.length() == 1)
return s;
for (int i = 0; i < s.length() - 1; i++) {
int j = (s.substring(i + 1)).indexOf(s.charAt(i)) + i + 1;
while (j < s.length() && j > i) {
if (j - …Run Code Online (Sandbox Code Playgroud)