package test.leecode.math; import org.junit.Assert; import org.junit.Test; import cn.fansunion.leecode.todo.CanPlaceFlowers; /** * @author wen.lei@brgroup.com * * 2022-2-25 */ public class CanPlaceFlowersTest { //方法1,不改变数组 @Test public void testError() { CanPlaceFlowers test = new CanPlaceFlowers(); //没通过的 //[0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0] //17 int [] nums= new int [] { 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 }; System.out.println(nums.length); Assert.assertFalse(test.canPlaceFlowerError(nums, 17 )); int [] nums1= new int [] { 0 , 1 , 0 , 0 , 0 }; Assert.assertFalse(test.canPlaceFlowerError(nums1, 2 )); //canPlaceFlowers2,第1次没通过 int [] nums11= new int [] { 1 , 0 , 0 , 0 , 0 , 1 }; Assert.assertTrue(test.canPlaceFlowerError(nums11, 1 )); Assert.assertFalse(test.canPlaceFlowerError(nums11, 2 )); int [] nums0= new int [] { 1 , 0 , 1 , 0 , 1 }; Assert.assertTrue(test.canPlaceFlowerError(nums0, 0 )); Assert.assertFalse(test.canPlaceFlowerError(nums0, 1 )); Assert.assertTrue(test.canPlaceFlowerError(nums1, 1 )); int [] nums2= new int [] { 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowerError(nums2, 2 )); Assert.assertFalse(test.canPlaceFlowerError(nums2, 3 )); int [] nums21= new int [] { 1 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowerError(nums21, 1 )); Assert.assertFalse(test.canPlaceFlowerError(nums21, 4 )); int [] nums22= new int [] { 0 , 0 , 1 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowerError(nums22, 2 )); Assert.assertFalse(test.canPlaceFlowerError(nums22, 3 )); int [] nums3= new int [] { 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowerError(nums3, 1 )); Assert.assertFalse(test.canPlaceFlowerError(nums3, 4 )); int [] nums4= new int [] { 0 , 0 , 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowerError(nums4, 4 )); Assert.assertFalse(test.canPlaceFlowerError(nums4, 5 )); int [] nums5= new int [] { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowerError(nums5, 5 )); Assert.assertFalse(test.canPlaceFlowerError(nums5, 6 )); int [] nums51= new int [] { 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowerError(nums51, 5 )); Assert.assertFalse(test.canPlaceFlowerError(nums51, 6 )); int [] nums52= new int [] { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowerError(nums52, 5 )); Assert.assertFalse(test.canPlaceFlowerError(nums52, 6 )); } @Test public void test() { CanPlaceFlowers test = new CanPlaceFlowers(); //canPlaceFlowers2,第1次没通过 int [] nums11= new int [] { 1 , 0 , 0 , 0 , 0 , 1 }; Assert.assertTrue(test.canPlaceFlowers(nums11.clone(), 1 )); Assert.assertFalse(test.canPlaceFlowers(nums11.clone(), 2 )); int [] nums0= new int [] { 1 , 0 , 1 , 0 , 1 }; Assert.assertTrue(test.canPlaceFlowers(nums0.clone(), 0 )); Assert.assertFalse(test.canPlaceFlowers(nums0.clone(), 1 )); int [] nums1= new int [] { 0 , 1 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums1.clone(), 1 )); Assert.assertFalse(test.canPlaceFlowers(nums1.clone(), 2 )); int [] nums2= new int [] { 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums2.clone(), 2 )); Assert.assertFalse(test.canPlaceFlowers(nums2.clone(), 3 )); int [] nums21= new int [] { 1 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums21.clone(), 1 )); Assert.assertFalse(test.canPlaceFlowers(nums21.clone(), 4 )); int [] nums22= new int [] { 0 , 0 , 1 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums22.clone(), 2 )); Assert.assertFalse(test.canPlaceFlowers(nums22.clone(), 3 )); int [] nums3= new int [] { 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums3.clone(), 1 )); Assert.assertFalse(test.canPlaceFlowers(nums3.clone(), 4 )); int [] nums4= new int [] { 0 , 0 , 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums4.clone(), 4 )); Assert.assertFalse(test.canPlaceFlowers(nums4.clone(), 5 )); int [] nums5= new int [] { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums5.clone(), 5 )); Assert.assertFalse(test.canPlaceFlowers(nums5.clone(), 6 )); int [] nums51= new int [] { 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums51.clone(), 5 )); Assert.assertFalse(test.canPlaceFlowers(nums51.clone(), 6 )); int [] nums52= new int [] { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; Assert.assertTrue(test.canPlaceFlowers(nums52.clone(), 5 )); Assert.assertFalse(test.canPlaceFlowers(nums52.clone(), 6 )); } } |