Array 2D - 如何查找连续的对角线

时间:2017-11-06 16:12:01

标签: java arrays algorithm

我可以在以下相同的行中找到连续符号:

componentDidMount() { 
   var subscription = Notifications.addListener(this.handleNotification);
   registerForPushNotificationsAsync();
}

handleNotification(notification) { 

   const { navigate } = this.props.navigation;
   navigate('MainScreen', { data: notification })
}

连续出现在同一列中:

int count = 0;
for (int column = 0; column < list[currentRow].length; column++) 
     if (list[currentRow][column].equals(currentValue))
         count++;

如何在同一对角线上找到连续?

我的尝试:

for (int row = currentRow; row < list.length; row++) 
        if (list[row][currentColumn].equals(currentValue))
            count++;

for (int majorDiagonal = currentRow + 1, column = currentColumn; majorDiagonal < list.length; majorDiagonal++, column++) {
        if (list[majorDiagonal][column].equals(currentValue))
            countMajorDiagonal++;
    }

3 个答案:

答案 0 :(得分:0)

说明您当前的值是(i, j),为了现在如果此值上有连续对角线,您必须检查此值是否等于(i-1, j-1)(i+1, j-1)处的值,(i-1, j+1)(i+1, j+1)

例如,在边境时不要忘记处理特殊情况。

如果你想浏览整个数组,你可以只检查对角线的正确和底部,因为左边和顶部已经完成了之前的迭代。

编辑: 查找连续符对您意味着什么?因为查找连续函数的函数只会计算与当前值相同的项目数,所以在返回循环之前应该将当前值更新为下一个。

A = [0, 0, 1, 1, 1, 0, 1, 0, 1, 0]上,如果5在循环之前,则代码将currentValue = 0; 4如果currentValue获取第一个值,则会package dk.cp3.anchorsafe; import com.codename1.ui.Button; import com.codename1.ui.CheckBox; import com.codename1.ui.Display; import com.codename1.ui.Form; import com.codename1.ui.plaf.UIManager; import com.codename1.ui.util.Resources; import com.codename1.ui.Command; import com.codename1.ui.Dialog; import com.codename1.ui.FontImage; import com.codename1.ui.Image; import com.codename1.ui.Label; import com.codename1.ui.NavigationCommand; import com.codename1.ui.RadioButton; import com.codename1.ui.Slider; import com.codename1.ui.TextField; import com.codename1.ui.Toolbar; import com.codename1.ui.events.ActionEvent; import com.codename1.ui.events.ActionListener; import com.codename1.ui.layouts.BoxLayout; /** * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose * of building native mobile applications using Java. */ public class AnchorSafely { private Form current; private Resources theme; private Form home; public void init(Object context) { theme = UIManager.initFirstTheme("/theme"); // Enable Toolbar on all Forms by default Toolbar.setGlobalToolbar(true); // Pro only feature, uncomment if you have a pro subscription // Log.bindCrashProtection(true); } public void start() { if (current != null) { current.show(); return; } //create and build the home Form home = new Form("Home"); home.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); home.addComponent(new Label("This is a Label")); home.addComponent(new Button("This is a Button")); TextField txt = new TextField(); txt.setHint("This is a TextField"); home.addComponent(txt); home.addComponent(new CheckBox("This is a CheckBox")); RadioButton rb1 = new RadioButton("This is a Radio Button 1"); rb1.setGroup("group"); home.addComponent(rb1); RadioButton rb2 = new RadioButton("This is a Radio Button 2"); rb2.setGroup("group"); home.addComponent(rb2); final Slider s = new Slider(); s.setText("50%"); s.setProgress(50); s.setEditable(true); s.setRenderPercentageOnTop(true); home.addComponent(s); Button b1 = new Button("Show a Dialog"); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { Dialog.show("Dialog Title", "Dialog Body", "Ok", "Cancel"); } }); home.addComponent(b1); //Create Form1 and Form2 and set a Back Command to navigate back to the home Form Form form1 = new Form("Form1"); setBackCommand(form1); Form form2 = new Form("Form2"); setBackCommand(form2); //Add navigation commands to the home Form NavigationCommand homeCommand = new NavigationCommand("Home"); homeCommand.setNextForm(home); home.getToolbar().addCommandToSideMenu(homeCommand); NavigationCommand cmd1 = new NavigationCommand("Form1"); cmd1.setNextForm(form1); home.getToolbar().addCommandToSideMenu(cmd1); NavigationCommand cmd2 = new NavigationCommand("Form2"); cmd2.setNextForm(form2); home.getToolbar().addCommandToSideMenu(cmd2); //Add Edit, Add and Delete Commands to the home Form context Menu Image im = FontImage.createMaterial(FontImage.MATERIAL_MODE_EDIT, UIManager.getInstance().getComponentStyle("Command")); Command edit = new Command("Edit", im) { @Override public void actionPerformed(ActionEvent evt) { System.out.println("Editing"); } }; home.getToolbar().addCommandToOverflowMenu(edit); im = FontImage.createMaterial(FontImage.MATERIAL_LIBRARY_ADD, UIManager.getInstance().getComponentStyle("Command")); Command add = new Command("Add", im) { @Override public void actionPerformed(ActionEvent evt) { System.out.println("Adding"); } }; home.getToolbar().addCommandToOverflowMenu(add); im = FontImage.createMaterial(FontImage.MATERIAL_DELETE, UIManager.getInstance().getComponentStyle("Command")); Command delete = new Command("Delete", im) { @Override public void actionPerformed(ActionEvent evt) { System.out.println("Deleting"); } }; home.getToolbar().addCommandToOverflowMenu(delete); home.show(); } protected void setBackCommand(Form f) { Command back = new Command("") { @Override public void actionPerformed(ActionEvent evt) { home.showBack(); } }; Image img = FontImage.createMaterial(FontImage.MATERIAL_ARROW_BACK, UIManager.getInstance().getComponentStyle("TitleCommand")); back.setIcon(img); f.getToolbar().addCommandToLeftBar(back); f.getToolbar().setTitleCentered(true); f.setBackCommand(back); } public void stop() { current = Display.getInstance().getCurrent(); } public void destroy() { } }

答案 1 :(得分:0)

尝试将对角线视为网格:

  0 1 2 3 4 5
0 o o o o o o
1 o o o o o o
2 x o o o o o
3 o x o o o o
4 o o x o o o
5 o o o x o o  
6 o o o o x o

对于由x突出显示的对角线,数组的索引为

list[0][2]
list[1][3]
list[2][4]
list[3][5]
list[4][6]

您可以在两个指数之间看到什么关系?它们始终遵循y = x + 2的格式,其中2是前2个索引之间的差异。

每当做这些对角线问题时,你只需将它们视为数学问题,弄清楚你是使用-x还是+x(如在对角线或向上对角线上),然后相应地增加你的计数器。

所以你的for循环可能看起来像:

int initial_col = // the x coordinate that you're starting from ;
int initial_row = // the y coordinate that you're starting from ;
for( int x = initial_col ; x < list.length && x + (initial_col - initial_row) < list[0].length ; i++){
  System.out.println("x index: " + x); //Check it for debugging
  int y = x + (initial_col - initial_row); //This is the y = x + b problem we looked at earlier
  System.out.println("y index: " + y); //Also for debugging
  list[x][y] ... // What you want to do with that point
}

这可能需要稍微调整一下,具体取决于您的边缘情况等,但我们的想法是弄清楚您的指数之间的关系并将其解决为数学问题。

答案 2 :(得分:0)

// test for dups hor, vert and diag
for(int x = 0; x < 5; x++){
    for(int y = 0; y < 5; y++){
        System.out.println("row:"+x+" col:"+y+" = "+ma[x][y]);
        // test for consecutives in a row if y is vert
            if(y < 4){
                if(ma[x][y] == ma[x][y+1]){
                    System.out.println("Found Consecutive ROW on:"+"row:"+x+" col:"+y+" of "+ma[x][y]);
                }
              }
              // test for consecutives in a col if x is col
               if(x < 4){
                 if(ma[x][y] == ma[x+1][y]){
                  System.out.println("Found Consecutive COL on:"+"row:"+x+" col:"+y+" of "+ma[x][y]);
                 }
               }
              //test vert left to right
               if(x < 4 && y < 4){
                if(ma[x][y] == ma[x+1][y+1]){
                  System.out.println("Found Consecutive Vertical Left to Right on:"+"row:"+x+" col:"+y+" of "+ma[x][y]);
                }
               }
              //test vert right to left
               if(x < 4 && y > 0){
                 if(ma[x][y] == ma[x+1][y-1]){
                  System.out.println("Found Consecutive Vertical Right to Left on:"+"row:"+x+" col:"+y+" of "+ma[x][y]);
                 }
               }
             }
            }