We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fb86e87 commit 630aa85Copy full SHA for 630aa85
剑指Offer_新版_java/JZ14.java
@@ -82,4 +82,37 @@ public int cutRope(int target) {
82
return (int) Math.pow(3, y);
83
}
84
85
+}
86
+
87
88
+/*
89
+快速幂
90
+ */
91
+public class Solution {
92
+ public int cutRope(int target) {
93
+ if (target == 2 || target == 3) {
94
+ return target - 1;
95
+ }
96
+ int x = target % 3;
97
+ int y = target / 3;
98
+ if (x == 1) {
99
+ return pow(3, y - 1) * 4;
100
+ } else if (x == 2) {
101
+ return pow(3, y) * 2;
102
+ } else {
103
+ return pow(3, y);
104
105
106
107
+ private int pow(int base, int num) {
108
+ int res = 1;
109
+ while (num > 0) {
110
+ if ((num & 1) == 1) {
111
+ res *= base;
112
113
+ base *= base;
114
+ num >>= 1;
115
116
+ return res;
117
118
0 commit comments