This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func longestCommonPrefix(strs []string) string { | |
if len(strs) == 0 { | |
return "" | |
} | |
sort.Slice(strs, func(i, j int) bool { | |
return len(strs[i]) < len(strs[j]) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func countAndSay(n int) string { | |
if n == 1 { | |
return "1" | |
} | |
base := "1" | |
baseTmpArr := make([][]string, 0) | |
for i := 2; i <= n; i++ { //n회 반복 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func strStr(haystack string, needle string) int { | |
return strings.Index(haystack, needle) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"golang.org/x/text/language" | |
) | |
func main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func myAtoi(s string) int { | |
s = strings.TrimSpace(s) //좌우 여백을 삭제 | |
s = strings.TrimRightFunc(s, func(r rune) bool { | |
return !unicode.IsNumber(r) | |
}) //우측은 숫자가 나올때까지 추가로 삭제 | |
stringArr := make([]string, 0) //정답을 넣을 배열을 생성 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isPalindrome(s string) bool { | |
//문자와 숫자만 리턴해주는 함수를 정의 | |
remove := func(c rune) bool { | |
return !unicode.IsLetter(c) && !unicode.IsNumber(c) | |
} | |
//FieldsFunc를 이용해 문자와 숫자를 없애주고, Join으로 스트링으로 변환해주고, lowercase로 변환 | |
s = strings.ToLower(strings.Join(strings.FieldsFunc(s, remove), "")) | |
r := reverseString(s) //s 문자열을 반대로 돌려줌 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isAnagram(s string, t string) bool { | |
//배열의 길이가 다르면 false | |
if len(s) != len(t) { | |
return false | |
} | |
for _, r := range s { | |
val := string(r) //현재 값 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Compare(a, b string) int //두 문자열을 알파벳으로 비교해서 int로 반환 | |
func Contains(s, substr string) bool //s에 substr에 속해 있는지 확인 | |
func ContainsAny(s, chars string) bool //chars가 s에 하나라도 속해 있는지 확인 ContainsAny("test", "exxxxx") // true | |
func ContainsRune(s string, r rune) bool //s 스트링 안에 rune 타입으로 contains 됐는지 확인 | |
func Count(s, substr string) int //s 문자열 안에, substr이 몇번이나 속해 있는지 확인 (중복갯수 확인) | |
func EqualFold(s, t string) bool //대소문자 구분없이 똑같은지 확인 "GO", "go"는 true | |
func Fields(s string) []string //스트링이 하나 이상의 화이트스페이스(스페이스 공백)으로 되어 있는 경우 배열로 분할함 | |
func FieldsFunc(s string, f func(rune) bool) []string //함수 조건식을 포함하여 split하는 기능 strings.FieldsFunc("!@#!#foo1;bar2,baz3...", f)) //["foo1" "bar2" "baz3"] | |
func HasPrefix(s, prefix string) bool //s가 prefix로 시작하는지 여부 | |
func HasSuffix(s, suffix string) bool //s가 suffix로 끝나는지 여부.. 대소문자 구분함 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func firstUniqChar(s string) int { | |
tmpArr := strings.Split(s, "") | |
pastArr := make([]string, 0) | |
for i := 0; i < len(tmpArr); i++ { | |
curVal := tmpArr[i] | |
//이미 했던 값이면 확인하지 말자. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func reverse(x int) int { | |
sign := x >= 0 // sign | |
abVal := math.Abs(float64(x)) //절대값 | |
t := strconv.Itoa(int(abVal)) // toString | |
return reverseString(t, sign) // reverse string and get int value | |
} | |
func reverseString(s string, sign bool) int { |