
參考了這個網址 解決問題
https://blog.csdn.net/weixin_45625639/article/details/122288602
程式小試身手 發表在 痞客邦 留言(0) 人氣(1)
第一次想這題時方向不太對
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
//index[m] 走0~n.length-1格
//index[m+1] 走 最後(n-1)
//index[m+2] 走 n-1~ 0
//回到index[m+1]走 0~n-2
int m=matrix.length;
int n=matrix[0].length;
int i=0;
int r_index=0;
List<Integer> result= new ArrayList<Integer>();
while(i<m){//>字型走法
if(i==0){//第一列
int j=0;
while(j<n){
result.add(matrix[i][j++]);
}
}else if(i==m-1){//最後一列
int j=n-1;
while(j>=0){
result.add(matrix[i][j--]);
}
}else{//中間先走每一個matrix[i]最後一個index
result.add(matrix[i][n-1]);
}
i++;
r_index++;
}
//走中間每一個matrix[i]第一個index
i=0;
while(i<m&&m>2){
if(i!=0&&i!=m-1){
result.add(matrix[i][0]);
}
i++;
}
//....裡面的就要在循環一次>
return result;
}
}
程式小試身手 發表在 痞客邦 留言(0) 人氣(0)
第一個想法,暴力破解
class Solution {
public double myPow(double x, int n) {
int i=1;
int round= Math.abs(n);//絕對值
double result=0;
result=x;
while(i<round){
result=x*result;
i++;
}
if(n>0){
return result;
}else{
return 1/result;
}
}
}
程式小試身手 發表在 痞客邦 留言(0) 人氣(0)
class Solution {
public boolean isMatch(String str, String pattern) {
int s = 0, p = 0, match = 0, starIdx = -1;
while (s < str.length()){
// advancing both pointers
if (p < pattern.length() && (pattern.charAt(p) == '?' || str.charAt(s) == pattern.charAt(p))){
s++;
p++;
}
// * found, only advancing pattern pointer
else if (p < pattern.length() && pattern.charAt(p) == '*'){
starIdx = p;
match = s;
p++;
}
// last pattern pointer was *, advancing string pointer
else if (starIdx != -1){
p = starIdx + 1;
match++;
s = match;
}
//current pattern pointer is not star, last patter pointer was not *
//characters do not match
else return false;
}
//check for remaining characters in pattern
while (p < pattern.length() && pattern.charAt(p) == '*')
p++;
return p == pattern.length();
}
}
程式小試身手 發表在 痞客邦 留言(0) 人氣(2)

把問題簡單化的話
程式小試身手 發表在 痞客邦 留言(0) 人氣(0)
[1,2,3] 降冪題, 由尾來看 i=1, 3<2的話則往下比 , j=2, 2<1的話往下比。
swap([1,2,3],1,2]) --->[1,3,2]
程式小試身手 發表在 痞客邦 留言(0) 人氣(0)
解答中的分治法 (Divide-and-conquer algorithm)
class Solution {
public static ListNode mergeKLists(ListNode[] lists){
return partion(lists,0,lists.length-1);
}
程式小試身手 發表在 痞客邦 留言(0) 人氣(1)
一個"("必定有另一個")" 所以n=1時2*1, n=2時2*2,n=3時2*3, 總結就是2*n個符號
不可能有")"先開始,所以驗證時 "( "balance 只會為正或為 0,<0 直接 return false
程式小試身手 發表在 痞客邦 留言(0) 人氣(0)
這題用stack真是太妙了
string "(" 則 stack放 ")" ,下一個如果是")" 就符合剛放進stack的
程式小試身手 發表在 痞客邦 留言(0) 人氣(0)
未按任何號碼時,字串回傳空
按一個時回傳該數字對應的字串
程式小試身手 發表在 痞客邦 留言(0) 人氣(1)